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