9eedf33a736099f1e57e064cd3016b45c52ba314
[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 import java.util.Collections;
12 import java.util.List;
13 import org.junit.Test;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
18 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import test.mock.util.EntityOwnershipServiceMock;
44 import test.mock.util.FRMTest;
45 import test.mock.util.RpcProviderRegistryMock;
46 import test.mock.util.SalFlowServiceMock;
47
48 public class FlowListenerTest extends FRMTest {
49     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
50     EntityOwnershipService eos = new EntityOwnershipServiceMock();
51
52     NodeKey s1Key = new NodeKey(new NodeId("S1"));
53     TableKey tableKey = new TableKey((short) 2);
54
55     @Test
56     public void addTwoFlowsTest() throws Exception {
57         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
58                 getDataBroker(),
59                 rpcProviderRegistryMock,
60                 getConfig(),
61                 eos);
62         forwardingRulesManager.start();
63
64         addFlowCapableNode(s1Key);
65
66         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
67         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
68                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
69         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
70                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
71         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
72         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
73
74         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
75         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
76         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
77         assertCommit(writeTx.submit());
78         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
79         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
80         assertEquals(1, addFlowCalls.size());
81         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
82
83         flowKey = new FlowKey(new FlowId("test_Flow2"));
84         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
85                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
86         flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
87         writeTx = getDataBroker().newWriteOnlyTransaction();
88         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
89         assertCommit(writeTx.submit());
90         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
91         addFlowCalls = salFlowService.getAddFlowCalls();
92         assertEquals(2, addFlowCalls.size());
93         assertEquals("DOM-1", addFlowCalls.get(1).getTransactionUri().getValue());
94         assertEquals(2, addFlowCalls.get(1).getTableId().intValue());
95         assertEquals(flowII, addFlowCalls.get(1).getFlowRef().getValue());
96
97         forwardingRulesManager.close();
98     }
99
100     @Test
101     public void updateFlowTest() throws Exception {
102         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
103                 getDataBroker(),
104                 rpcProviderRegistryMock,
105                 getConfig(),
106                 eos);
107         forwardingRulesManager.start();
108
109         addFlowCapableNode(s1Key);
110
111         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
112         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
113                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
114         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
115                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
116         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
117         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
118
119         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
120         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
121         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
122         assertCommit(writeTx.submit());
123         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
124         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
125         assertEquals(1, addFlowCalls.size());
126         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
127
128         flowKey = new FlowKey(new FlowId("test_Flow"));
129         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
130                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
131         flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).setOutGroup((long) 5).build();
132         writeTx = getDataBroker().newWriteOnlyTransaction();
133         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
134         assertCommit(writeTx.submit());
135         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
136         List<UpdateFlowInput> updateFlowCalls = salFlowService.getUpdateFlowCalls();
137         assertEquals(1, updateFlowCalls.size());
138         assertEquals("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue());
139         assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
140         assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getOriginalFlow().isStrict());
141         assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getUpdatedFlow().isStrict());
142
143         forwardingRulesManager.close();
144     }
145
146     @Test
147     public void updateFlowScopeTest() throws Exception {
148         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
149                 getDataBroker(),
150                 rpcProviderRegistryMock,
151                 getConfig(),
152                 eos);
153         forwardingRulesManager.start();
154
155         addFlowCapableNode(s1Key);
156
157         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
158         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
159                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
160         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
161                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
162         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
163         IpMatch ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)4)).build();
164         Match match = new MatchBuilder().setIpMatch(ipMatch).build();
165         Flow flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
166
167         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
168         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
169         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
170         assertCommit(writeTx.submit());
171         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
172         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
173         assertEquals(1, addFlowCalls.size());
174         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
175
176         flowKey = new FlowKey(new FlowId("test_Flow"));
177         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
178                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
179         ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)5)).build();
180         match = new MatchBuilder().setIpMatch(ipMatch).build();
181         flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
182         writeTx = getDataBroker().newWriteOnlyTransaction();
183         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
184         assertCommit(writeTx.submit());
185         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
186         List<UpdateFlowInput> updateFlowCalls = salFlowService.getUpdateFlowCalls();
187         assertEquals(1, updateFlowCalls.size());
188         assertEquals("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue());
189         assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
190         assertEquals(ipMatch, updateFlowCalls.get(0).getUpdatedFlow().getMatch().getIpMatch());
191         forwardingRulesManager.close();
192     }
193
194     @Test
195     public void deleteFlowTest() throws Exception {
196         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
197                 getDataBroker(),
198                 rpcProviderRegistryMock,
199                 getConfig(),
200                 eos);
201         forwardingRulesManager.start();
202
203         addFlowCapableNode(s1Key);
204
205         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
206         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
207                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
208         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
209                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
210         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
211         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
212
213         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
214         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
215         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
216         assertCommit(writeTx.submit());
217         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
218         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
219         assertEquals(1, addFlowCalls.size());
220         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
221
222         writeTx = getDataBroker().newWriteOnlyTransaction();
223         writeTx.delete(LogicalDatastoreType.CONFIGURATION, flowII);
224         assertCommit(writeTx.submit());
225         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
226         List<RemoveFlowInput> removeFlowCalls = salFlowService.getRemoveFlowCalls();
227         assertEquals(1, removeFlowCalls.size());
228         assertEquals("DOM-1", removeFlowCalls.get(0).getTransactionUri().getValue());
229         assertEquals(flowII, removeFlowCalls.get(0).getFlowRef().getValue());
230         assertEquals(Boolean.TRUE, removeFlowCalls.get(0).isStrict());
231
232         forwardingRulesManager.close();
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
256     }
257 }