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