X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=applications%2Fforwardingrules-manager%2Fsrc%2Ftest%2Fjava%2Ftest%2Fmock%2FFlowListenerTest.java;h=1e49669077f1a025a3186b22c23b0f42478f5f48;hb=674bb50091db4628a50c08606f8f8b9f3c8a5e55;hp=31ab45a413e55de27119fdd0e0e8c282d2d9c5ef;hpb=864f9b901d0afa8267f8d91de83ebca978fb3553;p=openflowplugin.git diff --git a/applications/forwardingrules-manager/src/test/java/test/mock/FlowListenerTest.java b/applications/forwardingrules-manager/src/test/java/test/mock/FlowListenerTest.java index 31ab45a413..1e49669077 100644 --- a/applications/forwardingrules-manager/src/test/java/test/mock/FlowListenerTest.java +++ b/applications/forwardingrules-manager/src/test/java/test/mock/FlowListenerTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the @@ -7,15 +7,29 @@ */ package test.mock; +import static org.awaitility.Awaitility.await; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertEquals; + import java.util.Collections; import java.util.List; +import org.junit.After; +import org.junit.Before; import org.junit.Test; -import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; -import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.opendaylight.mdsal.binding.api.WriteTransaction; +import org.opendaylight.mdsal.common.api.LogicalDatastoreType; +import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; +import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager; +import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager; +import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager; import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl; +import org.opendaylight.openflowplugin.applications.frm.recovery.OpenflowServiceRecoveryHandler; +import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager; +import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId; @@ -40,218 +54,219 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch; import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import test.mock.util.EntityOwnershipServiceMock; import test.mock.util.FRMTest; import test.mock.util.RpcProviderRegistryMock; import test.mock.util.SalFlowServiceMock; +@RunWith(MockitoJUnitRunner.class) public class FlowListenerTest extends FRMTest { - RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock(); - EntityOwnershipService eos = new EntityOwnershipServiceMock(); - - NodeKey s1Key = new NodeKey(new NodeId("S1")); + private ForwardingRulesManagerImpl forwardingRulesManager; + private static final NodeId NODE_ID = new NodeId("testnode:1"); + private static final NodeKey NODE_KEY = new NodeKey(NODE_ID); + RpcProviderRegistryMock rpcProviderRegistryMock = new RpcProviderRegistryMock(); TableKey tableKey = new TableKey((short) 2); - - @Test - public void addTwoFlowsTest() throws Exception { - ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl( - getDataBroker(), - rpcProviderRegistryMock, - getConfig(), - eos); + @Mock + ClusterSingletonServiceProvider clusterSingletonService; + @Mock + DeviceMastershipManager deviceMastershipManager; + @Mock + private ReconciliationManager reconciliationManager; + @Mock + private OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler; + @Mock + private ServiceRecoveryRegistry serviceRecoveryRegistry; + @Mock + private MastershipChangeServiceManager mastershipChangeServiceManager; + @Mock + private FlowGroupCacheManager flowGroupCacheManager; + + @Before + public void setUp() { + forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock, + rpcProviderRegistryMock, getConfig(), mastershipChangeServiceManager, clusterSingletonService, + getConfigurationService(), reconciliationManager, openflowServiceRecoveryHandler, + serviceRecoveryRegistry, flowGroupCacheManager); forwardingRulesManager.start(); + // TODO consider tests rewrite (added because of complicated access) + forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager); + Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true); + } - addFlowCapableNode(s1Key); + @Test + public void addTwoFlowsTest() { + addFlowCapableNode(NODE_KEY); FlowKey flowKey = new FlowKey(new FlowId("test_Flow")); - InstanceIdentifier tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier
tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey); - InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey); - Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.emptyList()).build(); - Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build(); + Table table = new TableBuilder().withKey(tableKey).setFlow(Collections.emptyList()).build(); + Flow flow = new FlowBuilder().withKey(flowKey).setTableId((short) 2).build(); WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table); writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow); - assertCommit(writeTx.submit()); + assertCommit(writeTx.commit()); SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService(); + await().until(listSize(salFlowService.getAddFlowCalls()), equalTo(1)); List addFlowCalls = salFlowService.getAddFlowCalls(); assertEquals(1, addFlowCalls.size()); assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue()); flowKey = new FlowKey(new FlowId("test_Flow2")); - flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) - .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey); - flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build(); + flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY).augmentation(FlowCapableNode.class) + .child(Table.class, tableKey).child(Flow.class, flowKey); + flow = new FlowBuilder().withKey(flowKey).setTableId((short) 2).build(); writeTx = getDataBroker().newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow); - assertCommit(writeTx.submit()); + assertCommit(writeTx.commit()); salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService(); + await().until(listSize(salFlowService.getAddFlowCalls()), equalTo(2)); addFlowCalls = salFlowService.getAddFlowCalls(); assertEquals(2, addFlowCalls.size()); assertEquals("DOM-1", addFlowCalls.get(1).getTransactionUri().getValue()); assertEquals(2, addFlowCalls.get(1).getTableId().intValue()); assertEquals(flowII, addFlowCalls.get(1).getFlowRef().getValue()); - - forwardingRulesManager.close(); } @Test - public void updateFlowTest() throws Exception { - ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl( - getDataBroker(), - rpcProviderRegistryMock, - getConfig(), - eos); - forwardingRulesManager.start(); - - addFlowCapableNode(s1Key); + public void updateFlowTest() { + addFlowCapableNode(NODE_KEY); FlowKey flowKey = new FlowKey(new FlowId("test_Flow")); - InstanceIdentifier
tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier
tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey); - InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey); - Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.emptyList()).build(); - Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build(); + Table table = new TableBuilder().withKey(tableKey).setFlow(Collections.emptyList()).build(); + Flow flow = new FlowBuilder().withKey(flowKey).setTableId((short) 2).build(); WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table); writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow); - assertCommit(writeTx.submit()); + assertCommit(writeTx.commit()); SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService(); + await().until(listSize(salFlowService.getAddFlowCalls()), equalTo(1)); + List addFlowCalls = salFlowService.getAddFlowCalls(); assertEquals(1, addFlowCalls.size()); assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue()); flowKey = new FlowKey(new FlowId("test_Flow")); - flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) - .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey); - flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).setOutGroup((long) 5).build(); + flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY).augmentation(FlowCapableNode.class) + .child(Table.class, tableKey).child(Flow.class, flowKey); + flow = new FlowBuilder().withKey(flowKey).setTableId((short) 2).setOutGroup((long) 5).build(); writeTx = getDataBroker().newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow); - assertCommit(writeTx.submit()); + assertCommit(writeTx.commit()); salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService(); + await().until(listSize(salFlowService.getUpdateFlowCalls()), equalTo(1)); List updateFlowCalls = salFlowService.getUpdateFlowCalls(); assertEquals(1, updateFlowCalls.size()); assertEquals("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue()); assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue()); assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getOriginalFlow().isStrict()); assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getUpdatedFlow().isStrict()); - - forwardingRulesManager.close(); } @Test - public void updateFlowScopeTest() throws Exception { - ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl( - getDataBroker(), - rpcProviderRegistryMock, - getConfig(), - eos); - forwardingRulesManager.start(); - - addFlowCapableNode(s1Key); + public void updateFlowScopeTest() { + addFlowCapableNode(NODE_KEY); FlowKey flowKey = new FlowKey(new FlowId("test_Flow")); - InstanceIdentifier
tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier
tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey); - InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey); - Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.emptyList()).build(); - IpMatch ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)4)).build(); + Table table = new TableBuilder().withKey(tableKey).setFlow(Collections.emptyList()).build(); + IpMatch ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short) 4)).build(); Match match = new MatchBuilder().setIpMatch(ipMatch).build(); - Flow flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build(); + Flow flow = new FlowBuilder().setMatch(match).withKey(flowKey).setTableId((short) 2).build(); WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table); writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow); - assertCommit(writeTx.submit()); + assertCommit(writeTx.commit()); SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService(); + await().until(listSize(salFlowService.getAddFlowCalls()), equalTo(1)); List addFlowCalls = salFlowService.getAddFlowCalls(); assertEquals(1, addFlowCalls.size()); assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue()); flowKey = new FlowKey(new FlowId("test_Flow")); - flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) - .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey); - ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)5)).build(); + flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY).augmentation(FlowCapableNode.class) + .child(Table.class, tableKey).child(Flow.class, flowKey); + ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short) 5)).build(); match = new MatchBuilder().setIpMatch(ipMatch).build(); - flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build(); + flow = new FlowBuilder().setMatch(match).withKey(flowKey).setTableId((short) 2).build(); writeTx = getDataBroker().newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow); - assertCommit(writeTx.submit()); + assertCommit(writeTx.commit()); salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService(); + await().until(listSize(salFlowService.getUpdateFlowCalls()), equalTo(1)); List updateFlowCalls = salFlowService.getUpdateFlowCalls(); assertEquals(1, updateFlowCalls.size()); assertEquals("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue()); assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue()); assertEquals(ipMatch, updateFlowCalls.get(0).getUpdatedFlow().getMatch().getIpMatch()); - forwardingRulesManager.close(); } @Test - public void deleteFlowTest() throws Exception { - ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl( - getDataBroker(), - rpcProviderRegistryMock, - getConfig(), - eos); - forwardingRulesManager.start(); - - addFlowCapableNode(s1Key); + public void deleteFlowTest() { + addFlowCapableNode(NODE_KEY); FlowKey flowKey = new FlowKey(new FlowId("test_Flow")); - InstanceIdentifier
tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier
tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey); - InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey); - Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.emptyList()).build(); - Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build(); + Table table = new TableBuilder().withKey(tableKey).setFlow(Collections.emptyList()).build(); + Flow flow = new FlowBuilder().withKey(flowKey).setTableId((short) 2).build(); WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table); writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow); - assertCommit(writeTx.submit()); + assertCommit(writeTx.commit()); SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService(); + await().until(listSize(salFlowService.getAddFlowCalls()), equalTo(1)); List addFlowCalls = salFlowService.getAddFlowCalls(); assertEquals(1, addFlowCalls.size()); assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue()); writeTx = getDataBroker().newWriteOnlyTransaction(); writeTx.delete(LogicalDatastoreType.CONFIGURATION, flowII); - assertCommit(writeTx.submit()); + assertCommit(writeTx.commit()); salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService(); + await().until(listSize(salFlowService.getRemoveFlowCalls()), equalTo(1)); List removeFlowCalls = salFlowService.getRemoveFlowCalls(); assertEquals(1, removeFlowCalls.size()); assertEquals("DOM-1", removeFlowCalls.get(0).getTransactionUri().getValue()); assertEquals(flowII, removeFlowCalls.get(0).getFlowRef().getValue()); assertEquals(Boolean.TRUE, removeFlowCalls.get(0).isStrict()); - - forwardingRulesManager.close(); } - @Test - public void staleMarkedFlowCreationTest() throws Exception{ + public void staleMarkedFlowCreationTest() { - addFlowCapableNode(s1Key); + addFlowCapableNode(NODE_KEY); StaleFlowKey flowKey = new StaleFlowKey(new FlowId("stale_Flow")); - InstanceIdentifier
tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier
tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey); - InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key) + InstanceIdentifier flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY) .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(StaleFlow.class, flowKey); - Table table = new TableBuilder().setKey(tableKey).setStaleFlow(Collections.emptyList()).build(); - StaleFlow flow = new StaleFlowBuilder().setKey(flowKey).setTableId((short) 2).build(); + Table table = new TableBuilder().withKey(tableKey).setStaleFlow(Collections.emptyList()).build(); + StaleFlow flow = new StaleFlowBuilder().withKey(flowKey).setTableId((short) 2).build(); WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table); writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow); - assertCommit(writeTx.submit()); - - + assertCommit(writeTx.commit()); + } + @After + public void tearDown() throws Exception { + forwardingRulesManager.close(); } }