X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-it%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Ftest%2Fsal%2Fbinding%2Fit%2FRoutedServiceTest.java;h=95abee42e54da5db63c86d112a7f30bc8023d9a3;hb=d8d8f731bbe6c58fcbd0e616734e2e230aaf4ab4;hp=ef60a2c15c5c3d2a7e6187ad34697387aa23a7e1;hpb=e41b96adc85177b252b3a47816f9f9f6b5571362;p=controller.git diff --git a/opendaylight/md-sal/sal-binding-it/src/test/java/org/opendaylight/controller/test/sal/binding/it/RoutedServiceTest.java b/opendaylight/md-sal/sal-binding-it/src/test/java/org/opendaylight/controller/test/sal/binding/it/RoutedServiceTest.java index ef60a2c15c..95abee42e5 100644 --- a/opendaylight/md-sal/sal-binding-it/src/test/java/org/opendaylight/controller/test/sal/binding/it/RoutedServiceTest.java +++ b/opendaylight/md-sal/sal-binding-it/src/test/java/org/opendaylight/controller/test/sal/binding/it/RoutedServiceTest.java @@ -1,49 +1,68 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.test.sal.binding.it; -import static org.junit.Assert.*; - -import java.math.BigInteger; -import java.util.Collection; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import com.google.common.util.concurrent.Futures; import org.junit.Before; import org.junit.Test; -import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; +import org.mockito.Mockito; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration; import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer; -import org.opendaylight.controller.sal.binding.api.BindingAwareProvider.ProviderFunctionality; import org.opendaylight.controller.sal.binding.api.BindingAwareProvider; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.OpendaylightTestRoutedRpcService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.TestContext; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.Lists; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnorderedContainer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedList; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListKey; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.binding.RpcService; - -import com.google.inject.Inject; - -import static org.mockito.Mockito.*; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * covers routed rpc creation, registration, invocation, unregistration + */ public class RoutedServiceTest extends AbstractTest { - private SalFlowService first; - private SalFlowService second; + private static final Logger LOG = LoggerFactory + .getLogger(RoutedServiceTest.class); + + protected OpendaylightTestRoutedRpcService odlRoutedService1; + protected OpendaylightTestRoutedRpcService odlRoutedService2; - private SalFlowService consumerService; + protected OpendaylightTestRoutedRpcService consumerService; - private RoutedRpcRegistration firstReg; - private RoutedRpcRegistration secondReg; + protected RoutedRpcRegistration firstReg; + protected RoutedRpcRegistration secondReg; + /** + * prepare mocks + */ @Before - public void setUp() throws Exception { - first = mock(SalFlowService.class,"First Flow Service"); - second = mock(SalFlowService.class,"Second Flow Service"); + public void setUp() { + odlRoutedService1 = mock(OpendaylightTestRoutedRpcService.class, "First Flow Service"); + odlRoutedService2 = mock(OpendaylightTestRoutedRpcService.class, "Second Flow Service"); + Mockito.when(odlRoutedService1.routedSimpleRoute(Mockito.any())) + .thenReturn(Futures.>immediateFuture(null)); + Mockito.when(odlRoutedService2.routedSimpleRoute(Mockito.any())) + .thenReturn(Futures.>immediateFuture(null)); } @Test @@ -51,149 +70,137 @@ public class RoutedServiceTest extends AbstractTest { assertNotNull(getBroker()); - BindingAwareProvider provider1 = new AbstractTestProvider() { - + final BindingAwareProvider provider1 = new AbstractTestProvider() { @Override - public void onSessionInitiated(ProviderContext session) { + public void onSessionInitiated(final ProviderContext session) { assertNotNull(session); - firstReg = session.addRoutedRpcImplementation(SalFlowService.class, first); + firstReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService1); } }; - /** - * Register first provider, which register first implementation of - * SalFlowService - * - */ - getBroker().registerProvider(provider1, getBundleContext()); + LOG.info("Register provider 1 with first implementation of routeSimpleService - service1"); + broker.registerProvider(provider1); assertNotNull("Registration should not be null", firstReg); - assertSame(first, firstReg.getInstance()); - - BindingAwareProvider provider2 = new AbstractTestProvider() { + assertSame(odlRoutedService1, firstReg.getInstance()); + final BindingAwareProvider provider2 = new AbstractTestProvider() { @Override - public void onSessionInitiated(ProviderContext session) { + public void onSessionInitiated(final ProviderContext session) { assertNotNull(session); - secondReg = session.addRoutedRpcImplementation(SalFlowService.class, second); + secondReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService2); } }; - getBroker().registerProvider(provider2, getBundleContext()); + + LOG.info("Register provider 2 with second implementation of routeSimpleService - service2"); + broker.registerProvider(provider2); assertNotNull("Registration should not be null", firstReg); + assertSame(odlRoutedService2, secondReg.getInstance()); assertNotSame(secondReg, firstReg); - - BindingAwareConsumer consumer = new BindingAwareConsumer() { + final BindingAwareConsumer consumer = new BindingAwareConsumer() { @Override - public void onSessionInitialized(ConsumerContext session) { - consumerService = session.getRpcService(SalFlowService.class); + public void onSessionInitialized(final ConsumerContext session) { + consumerService = session.getRpcService(OpendaylightTestRoutedRpcService.class); } }; - broker.registerConsumer(consumer, getBundleContext()); + LOG.info("Register routeService consumer"); + broker.registerConsumer(consumer); - assertNotNull("MD-SAL instance of Flow Service should be returned", consumerService); - assertNotSame("Provider instance and consumer instance should not be same.", first, consumerService); + assertNotNull("MD-SAL instance of test Service should be returned", consumerService); + assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1, consumerService); - NodeRef nodeOne = createNodeRef("foo:node:1"); + final InstanceIdentifier nodeOnePath = createNodeRef("foo:node:1"); + LOG.info("Provider 1 registers path of node 1"); + firstReg.registerPath(TestContext.class, nodeOnePath); /** - * Provider 1 - register itself as provider for SalFlowService - * for nodeOne + * Consumer creates addFlow message for node one and sends it to the + * MD-SAL */ - firstReg.registerPath(NodeContext.class, nodeOne.getValue()); + final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath); + consumerService.routedSimpleRoute(simpleRouteFirstFoo); /** - * Consumer creates addFlow Message for node one and sends - * it to the MD-SAL - * - */ - AddFlowInput firstMessage = createSampleAddFlow(nodeOne,1); - consumerService.addFlow(firstMessage); - - /** - * We verify if implementation of first provider received same + * Verifies that implementation of the first provider received the same * message from MD-SAL. - * - */ - verify(first).addFlow(firstMessage); - - /** - * Verifies that second instance was not invoked with first - * message - * */ - verify(second, times(0)).addFlow(firstMessage); - + verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo); /** - * Second provider registers as provider for nodeTwo - * + * Verifies that second instance was not invoked with first message */ - NodeRef nodeTwo = createNodeRef("foo:node:2"); - secondReg.registerPath(NodeContext.class, nodeTwo.getValue()); - - - /** - * Consumer sends message to nodeTwo for three times. - * Should be processed by second instance. - */ - AddFlowInput secondMessage = createSampleAddFlow(nodeTwo,2); - consumerService.addFlow(secondMessage); - consumerService.addFlow(secondMessage); - consumerService.addFlow(secondMessage); - - /** - * We verify that second was invoked 3 times, with message - * two as parameter, first was invoked 0 times. - * - */ - verify(second, times(3)).addFlow(secondMessage); - verify(first, times(0)).addFlow(secondMessage); - - + verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo); + + LOG.info("Provider 2 registers path of node 2"); + final InstanceIdentifier nodeTwo = createNodeRef("foo:node:2"); + secondReg.registerPath(TestContext.class, nodeTwo); + /** - * First provider unregisters as implementation of FlowService - * for node one - * + * Consumer sends message to nodeTwo for three times. Should be + * processed by second instance. */ - firstReg.unregisterPath(NodeContext.class, nodeOne.getValue()); - - + final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo); + consumerService.routedSimpleRoute(simpleRouteSecondFoo); + consumerService.routedSimpleRoute(simpleRouteSecondFoo); + consumerService.routedSimpleRoute(simpleRouteSecondFoo); + /** - * Second provider registers as implementation for FlowService - * for node one - * + * Verifies that second instance was invoked 3 times with second message + * and first instance wasn't invoked. + * */ - secondReg.registerPath(NodeContext.class, nodeOne.getValue()); - + verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo); + verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo); + + LOG.info("Unregistration of the path for the node one in the first provider"); + firstReg.unregisterPath(TestContext.class, nodeOnePath); + + LOG.info("Provider 2 registers path of node 1"); + secondReg.registerPath(TestContext.class, nodeOnePath); + /** - * Consumer sends third message to Node 1, should be processed - * by second instance. - * + * A consumer sends third message to node 1 */ - AddFlowInput thirdMessage = createSampleAddFlow(nodeOne,3); - consumerService.addFlow(thirdMessage); - + final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath); + consumerService.routedSimpleRoute(simpleRouteThirdFoo); + /** - * We verify that first provider was invoked 0 times, - * second provider 1 time. + * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1 + * time. + * TODO: fix unregister path */ - verify(first,times(0)).addFlow(thirdMessage); - verify(second).addFlow(thirdMessage); - - } + //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo); + verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo); - private static NodeRef createNodeRef(String string) { - NodeKey key = new NodeKey(new NodeId(string)); - InstanceIdentifier path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key) - .toInstance(); + } - return new NodeRef(path); + /** + * Returns node reference from string which represents path + * + * @param string + * string with key(path) + * @return instance identifier to {@link UnorderedList} + */ + private static InstanceIdentifier createNodeRef(final String string) { + final UnorderedListKey key = new UnorderedListKey(string); + final InstanceIdentifier path = InstanceIdentifier.builder(Lists.class) + .child(UnorderedContainer.class) + .child(UnorderedList.class, key) + .build(); + + return path; } - static AddFlowInput createSampleAddFlow(NodeRef node,int cookie) { - AddFlowInputBuilder ret = new AddFlowInputBuilder(); - ret.setNode(node); - ret.setCookie(BigInteger.valueOf(cookie)); + /** + * Creates flow AddFlowInput for which only node and cookie are set + * + * @param node + * NodeRef value + * @return simpleRouteInput instance + */ + static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier node) { + final RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder(); + ret.setRoute(node); return ret.build(); } }