Bug 8902 - Changes in FRM for Reconciliation Framework
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / test / mock / FlowListenerTest.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package test.mock;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.Collections;
13 import java.util.List;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
24 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
26 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
27 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
28 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import test.mock.util.FRMTest;
54 import test.mock.util.RpcProviderRegistryMock;
55 import test.mock.util.SalFlowServiceMock;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class FlowListenerTest extends FRMTest {
59     private ForwardingRulesManagerImpl forwardingRulesManager;
60     private final static NodeId NODE_ID = new NodeId("testnode:1");
61     private final static NodeKey s1Key = new NodeKey(NODE_ID);
62     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
63     TableKey tableKey = new TableKey((short) 2);
64     @Mock
65     ClusterSingletonServiceProvider clusterSingletonService;
66     @Mock
67     DeviceMastershipManager deviceMastershipManager;
68     @Mock
69     private NotificationProviderService notificationService;
70     @Mock
71     private ReconciliationManager reconciliationManager;
72
73     @Before
74     public void setUp() {
75         forwardingRulesManager = new ForwardingRulesManagerImpl(
76                 getDataBroker(),
77                 rpcProviderRegistryMock,
78                 getConfig(),
79                 clusterSingletonService,
80                 notificationService,
81                 getConfigurationService(),
82                 reconciliationManager);
83
84         forwardingRulesManager.start();
85         // TODO consider tests rewrite (added because of complicated access)
86         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
87         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
88     }
89
90     @Test
91     public void addTwoFlowsTest() throws Exception {
92         addFlowCapableNode(s1Key);
93
94         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
95         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
96                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
97         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
98                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
99         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
100         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
101
102         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
103         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
104         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
105         assertCommit(writeTx.submit());
106         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
107         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
108         assertEquals(1, addFlowCalls.size());
109         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
110
111         flowKey = new FlowKey(new FlowId("test_Flow2"));
112         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
113                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
114         flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
115         writeTx = getDataBroker().newWriteOnlyTransaction();
116         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
117         assertCommit(writeTx.submit());
118         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
119         addFlowCalls = salFlowService.getAddFlowCalls();
120         assertEquals(2, addFlowCalls.size());
121         assertEquals("DOM-1", addFlowCalls.get(1).getTransactionUri().getValue());
122         assertEquals(2, addFlowCalls.get(1).getTableId().intValue());
123         assertEquals(flowII, addFlowCalls.get(1).getFlowRef().getValue());
124 }
125
126     @Test
127     public void updateFlowTest() throws Exception {
128         addFlowCapableNode(s1Key);
129
130         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
131         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
132                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
133         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
134                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
135         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
136         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
137
138         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
139         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
140         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
141         assertCommit(writeTx.submit());
142         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
143         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
144         assertEquals(1, addFlowCalls.size());
145         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
146
147         flowKey = new FlowKey(new FlowId("test_Flow"));
148         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
149                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
150         flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).setOutGroup((long) 5).build();
151         writeTx = getDataBroker().newWriteOnlyTransaction();
152         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
153         assertCommit(writeTx.submit());
154         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
155         List<UpdateFlowInput> updateFlowCalls = salFlowService.getUpdateFlowCalls();
156         assertEquals(1, updateFlowCalls.size());
157         assertEquals("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue());
158         assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
159         assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getOriginalFlow().isStrict());
160         assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getUpdatedFlow().isStrict());
161     }
162
163     @Test
164     public void updateFlowScopeTest() throws Exception {
165         addFlowCapableNode(s1Key);
166
167         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
168         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
169                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
170         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
171                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
172         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
173         IpMatch ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)4)).build();
174         Match match = new MatchBuilder().setIpMatch(ipMatch).build();
175         Flow flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
176
177         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
178         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
179         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
180         assertCommit(writeTx.submit());
181         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
182         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
183         assertEquals(1, addFlowCalls.size());
184         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
185
186         flowKey = new FlowKey(new FlowId("test_Flow"));
187         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
188                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
189         ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)5)).build();
190         match = new MatchBuilder().setIpMatch(ipMatch).build();
191         flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
192         writeTx = getDataBroker().newWriteOnlyTransaction();
193         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
194         assertCommit(writeTx.submit());
195         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
196         List<UpdateFlowInput> updateFlowCalls = salFlowService.getUpdateFlowCalls();
197         assertEquals(1, updateFlowCalls.size());
198         assertEquals("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue());
199         assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
200         assertEquals(ipMatch, updateFlowCalls.get(0).getUpdatedFlow().getMatch().getIpMatch());
201     }
202
203     @Test
204     public void deleteFlowTest() throws Exception {
205         addFlowCapableNode(s1Key);
206
207         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
208         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
209                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
210         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
211                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
212         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
213         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
214
215         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
216         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
217         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
218         assertCommit(writeTx.submit());
219         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
220         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
221         assertEquals(1, addFlowCalls.size());
222         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
223
224         writeTx = getDataBroker().newWriteOnlyTransaction();
225         writeTx.delete(LogicalDatastoreType.CONFIGURATION, flowII);
226         assertCommit(writeTx.submit());
227         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
228         List<RemoveFlowInput> removeFlowCalls = salFlowService.getRemoveFlowCalls();
229         assertEquals(1, removeFlowCalls.size());
230         assertEquals("DOM-1", removeFlowCalls.get(0).getTransactionUri().getValue());
231         assertEquals(flowII, removeFlowCalls.get(0).getFlowRef().getValue());
232         assertEquals(Boolean.TRUE, removeFlowCalls.get(0).isStrict());
233     }
234
235
236     @Test
237     public void staleMarkedFlowCreationTest() throws Exception{
238
239         addFlowCapableNode(s1Key);
240
241         StaleFlowKey flowKey = new StaleFlowKey(new FlowId("stale_Flow"));
242         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
243                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
244         InstanceIdentifier<StaleFlow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
245                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(StaleFlow.class, flowKey);
246         Table table = new TableBuilder().setKey(tableKey).setStaleFlow(Collections.<StaleFlow>emptyList()).build();
247         StaleFlow flow = new StaleFlowBuilder().setKey(flowKey).setTableId((short) 2).build();
248
249         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
250         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
251         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
252         assertCommit(writeTx.submit());
253     }
254
255     @After
256     public void tearDown() throws Exception {
257         forwardingRulesManager.close();
258     }
259
260 }