package org.opendaylight.controller.test.sal.binding.it;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import java.math.BigInteger;
import java.util.ArrayList;
import org.junit.Test;
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.BindingAwareConsumer;
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
-import org.opendaylight.controller.sal.binding.api.NotificationService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
+import org.opendaylight.controller.sal.binding.api.*;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.*;
import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.binding.NotificationListener;
private Registration<NotificationListener> listener1Reg;
private Registration<NotificationListener> listener2Reg;
- private NotificationProviderService notifyService;
+ private NotificationProviderService notifyProviderService;
@Before
public void setUp() throws Exception {
public void notificationTest() throws Exception {
/**
*
- * We register Provider 1 which retrieves Notification Service from MD-SAL
+ * The registration of the Provider 1.
*
*/
- AbstractTestProvider provider = new AbstractTestProvider() {
+ AbstractTestProvider provider1 = new AbstractTestProvider() {
@Override
public void onSessionInitiated(ProviderContext session) {
- notifyService = session.getSALService(NotificationProviderService.class);
+ notifyProviderService = session.getSALService(NotificationProviderService.class);
}
};
- broker.registerProvider(provider, getBundleContext());
+
+ // registerProvider method calls onSessionInitiated method above
+ broker.registerProvider(provider1, getBundleContext());
+ assertNotNull(notifyProviderService);
/**
*
- * We register Consumer 1 which retrieves Notification Service from MD-SAL
- * and registers SalFlowListener as notification listener
+ * The registration of the Consumer 1. It retrieves Notification Service
+ * from MD-SAL and registers SalFlowListener as notification listener
*
*/
BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
listener1Reg = notificationService.registerNotificationListener(listener1);
}
};
-
+ // registerConsumer method calls onSessionInitialized method above
broker.registerConsumer(consumer1, getBundleContext());
assertNotNull(listener1Reg);
/**
- * We wait 100ms for to make sure broker threads delivered notifications
+ * The notification of type FlowAdded with cookie ID 0 is created. The
+ * delay 100ms to make sure that the notification was delivered to
+ * listener.
*/
- notifyService.publish(flowAdded(0));
+ notifyProviderService.publish(flowAdded(0));
Thread.sleep(100);
-
- /**
- * We verify one notification was delivered
+
+ /**
+ * Check that one notification was delivered and has correct cookie.
*
*/
assertEquals(1, listener1.addedFlows.size());
assertEquals(0, listener1.addedFlows.get(0).getCookie().intValue());
-
/**
- * We also register second consumerm and it's SalFlowListener
+ * The registration of the Consumer 2. SalFlowListener is registered
+ * registered as notification listener.
*/
BindingAwareConsumer consumer2 = new BindingAwareConsumer() {
@Override
}
};
+ // registerConsumer method calls onSessionInitialized method above
broker.registerConsumer(consumer2, getBundleContext());
/**
- * We publish 3 notifications
+ * 3 notifications are published
*/
- notifyService.publish(flowAdded(5));
- notifyService.publish(flowAdded(10));
- notifyService.publish(flowAdded(2));
+ notifyProviderService.publish(flowAdded(5));
+ notifyProviderService.publish(flowAdded(10));
+ notifyProviderService.publish(flowAdded(2));
/**
- * We wait 100ms for to make sure broker threads delivered notifications
+ * The delay 100ms to make sure that the notifications were delivered to
+ * listeners.
*/
Thread.sleep(100);
-
- /**
- * We verify 3 notification was delivered to both listeners
- * (first one received 4 total, second 3 in total).
+
+ /**
+ * Check that 3 notification was delivered to both listeners (first one
+ * received 4 in total, second 3 in total).
*
*/
-
assertEquals(4, listener1.addedFlows.size());
assertEquals(3, listener2.addedFlows.size());
/**
- * We close / unregister second listener
+ * The second listener is closed (unregistered)
*
*/
listener2Reg.close();
-
+
/**
*
- * We punblish 5th notification
+ * The notification 5 is published
*/
- notifyService.publish(flowAdded(10));
-
+ notifyProviderService.publish(flowAdded(10));
+
/**
- * We wait 100ms for to make sure broker threads delivered notifications
+ * The delay 100ms to make sure that the notification was delivered to
+ * listener.
*/
Thread.sleep(100);
-
+
/**
- * We verify that first consumer received 5 notifications in total,
- * second consumer only three. Last notification was never received,
- * because it already unregistered listener.
+ * Check that first consumer received 5 notifications in total, second
+ * consumer received only three. Last notification was never received by
+ * second consumer because its listener was unregistered.
*
*/
assertEquals(5, listener1.addedFlows.size());
}
+ /**
+ * Creates instance of the type FlowAdded. Only cookie value is set. It is
+ * used only for testing purpose.
+ *
+ * @param i
+ * cookie value
+ * @return instance of the type FlowAdded
+ */
public static FlowAdded flowAdded(int i) {
FlowAddedBuilder ret = new FlowAddedBuilder();
ret.setCookie(BigInteger.valueOf(i));
return ret.build();
}
+ /**
+ *
+ * Implements
+ * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
+ * SalFlowListener} and contains attributes which keep lists of objects of
+ * the type
+ * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819. NodeFlow
+ * NodeFlow}. The lists are defined for flows which were added, removed or
+ * updated.
+ */
private static class FlowListener implements SalFlowListener {
List<FlowAdded> addedFlows = new ArrayList<>();
package org.opendaylight.controller.test.sal.binding.it;
-import static org.junit.Assert.*;
+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 java.math.BigInteger;
-import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
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.controller.sal.binding.api.*;
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.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.RpcService;
-
-import com.google.inject.Inject;
-
-import static org.mockito.Mockito.*;
public class RoutedServiceTest extends AbstractTest {
- private SalFlowService first;
- private SalFlowService second;
+ private SalFlowService salFlowService1;
+ private SalFlowService salFlowService2;
private SalFlowService consumerService;
@Before
public void setUp() throws Exception {
- first = mock(SalFlowService.class,"First Flow Service");
- second = mock(SalFlowService.class,"Second Flow Service");
+ salFlowService1 = mock(SalFlowService.class, "First Flow Service");
+ salFlowService2 = mock(SalFlowService.class, "Second Flow Service");
}
@Test
@Override
public void onSessionInitiated(ProviderContext session) {
assertNotNull(session);
- firstReg = session.addRoutedRpcImplementation(SalFlowService.class, first);
+ firstReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService1);
}
};
/**
- * Register first provider, which register first implementation of
- * SalFlowService
+ * Register provider 1 with first implementation of SalFlowService -
+ * service1
*
*/
- getBroker().registerProvider(provider1, getBundleContext());
+ broker.registerProvider(provider1, getBundleContext());
assertNotNull("Registration should not be null", firstReg);
- assertSame(first, firstReg.getInstance());
-
+ assertSame(salFlowService1, firstReg.getInstance());
+
BindingAwareProvider provider2 = new AbstractTestProvider() {
@Override
public void onSessionInitiated(ProviderContext session) {
assertNotNull(session);
- secondReg = session.addRoutedRpcImplementation(SalFlowService.class, second);
+ secondReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService2);
}
};
- getBroker().registerProvider(provider2, getBundleContext());
+
+ /**
+ * Register provider 2 with first implementation of SalFlowService -
+ * service2
+ *
+ */
+ broker.registerProvider(provider2, getBundleContext());
assertNotNull("Registration should not be null", firstReg);
+ assertSame(salFlowService2, secondReg.getInstance());
assertNotSame(secondReg, firstReg);
-
BindingAwareConsumer consumer = new BindingAwareConsumer() {
@Override
public void onSessionInitialized(ConsumerContext session) {
broker.registerConsumer(consumer, getBundleContext());
assertNotNull("MD-SAL instance of Flow Service should be returned", consumerService);
- assertNotSame("Provider instance and consumer instance should not be same.", first, consumerService);
+ assertNotSame("Provider instance and consumer instance should not be same.", salFlowService1, consumerService);
NodeRef nodeOne = createNodeRef("foo:node:1");
-
/**
- * Provider 1 - register itself as provider for SalFlowService
- * for nodeOne
+ * Provider 1 registers path of node 1
*/
firstReg.registerPath(NodeContext.class, nodeOne.getValue());
/**
- * Consumer creates addFlow Message for node one and sends
- * it to the MD-SAL
+ * Consumer creates addFlow message for node one and sends it to the
+ * MD-SAL
*
*/
- AddFlowInput firstMessage = createSampleAddFlow(nodeOne,1);
- consumerService.addFlow(firstMessage);
-
+ AddFlowInput addFlowFirstMessage = createSampleAddFlow(nodeOne, 1);
+ consumerService.addFlow(addFlowFirstMessage);
+
/**
- * 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);
-
+ verify(salFlowService1).addFlow(addFlowFirstMessage);
+
/**
- * Verifies that second instance was not invoked with first
- * message
+ * Verifies that second instance was not invoked with first message
*
*/
- verify(second, times(0)).addFlow(firstMessage);
-
+ verify(salFlowService2, times(0)).addFlow(addFlowFirstMessage);
+
/**
- * Second provider registers as provider for nodeTwo
+ * Provider 2 registers path of node 2
*
*/
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.
+ * 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);
-
+ AddFlowInput AddFlowSecondMessage = createSampleAddFlow(nodeTwo, 2);
+ consumerService.addFlow(AddFlowSecondMessage);
+ consumerService.addFlow(AddFlowSecondMessage);
+ consumerService.addFlow(AddFlowSecondMessage);
+
/**
- * We verify that second was invoked 3 times, with message
- * two as parameter, first was invoked 0 times.
+ * Verifies that second instance was invoked 3 times with second message
+ * and first instance wasn't invoked.
*
*/
- verify(second, times(3)).addFlow(secondMessage);
- verify(first, times(0)).addFlow(secondMessage);
-
-
+ verify(salFlowService2, times(3)).addFlow(AddFlowSecondMessage);
+ verify(salFlowService1, times(0)).addFlow(AddFlowSecondMessage);
+
/**
- * First provider unregisters as implementation of FlowService
- * for node one
+ * Unregisteration of the path for the node one in the first provider
*
*/
firstReg.unregisterPath(NodeContext.class, nodeOne.getValue());
-
-
+
/**
- * Second provider registers as implementation for FlowService
- * for node one
+ * Provider 2 registers path of node 1
*
*/
secondReg.registerPath(NodeContext.class, nodeOne.getValue());
-
+
/**
- * 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);
-
+ AddFlowInput AddFlowThirdMessage = createSampleAddFlow(nodeOne, 3);
+ consumerService.addFlow(AddFlowThirdMessage);
+
/**
- * 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.
*/
- verify(first,times(0)).addFlow(thirdMessage);
- verify(second).addFlow(thirdMessage);
-
+ verify(salFlowService1, times(0)).addFlow(AddFlowThirdMessage);
+ verify(salFlowService2).addFlow(AddFlowThirdMessage);
+
}
+ /**
+ * Returns node reference from string which represents path
+ *
+ * @param string
+ * string with key(path)
+ * @return instance of the type NodeRef
+ */
private static NodeRef createNodeRef(String string) {
NodeKey key = new NodeKey(new NodeId(string));
InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
return new NodeRef(path);
}
- static AddFlowInput createSampleAddFlow(NodeRef node,int cookie) {
+ /**
+ * Creates flow AddFlowInput for which only node and cookie are set
+ *
+ * @param node
+ * NodeRef value
+ * @param cookie
+ * integer with cookie value
+ * @return AddFlowInput instance
+ */
+ static AddFlowInput createSampleAddFlow(NodeRef node, int cookie) {
AddFlowInputBuilder ret = new AddFlowInputBuilder();
ret.setNode(node);
ret.setCookie(BigInteger.valueOf(cookie));