Support for Table Features
[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(2, addFlowCalls.get(1).getTableId().intValue());
86         assertEquals(flowII, addFlowCalls.get(1).getFlowRef().getValue());
87
88         forwardingRulesManager.close();
89     }
90
91     @Test
92     public void updateFlowTest() throws Exception {
93         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock);
94         forwardingRulesManager.start();
95
96         addFlowCapableNode(s1Key);
97
98         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
99         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
100                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
101         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
102                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
103         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
104         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
105
106         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
107         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
108         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
109         assertCommit(writeTx.submit());
110         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
111         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
112         assertEquals(1, addFlowCalls.size());
113         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
114
115         flowKey = new FlowKey(new FlowId("test_Flow"));
116         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
117                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
118         flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).setOutGroup((long) 5).build();
119         writeTx = getDataBroker().newWriteOnlyTransaction();
120         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
121         assertCommit(writeTx.submit());
122         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
123         List<UpdateFlowInput> updateFlowCalls = salFlowService.getUpdateFlowCalls();
124         assertEquals(1, updateFlowCalls.size());
125         assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
126         assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getOriginalFlow().isStrict());
127         assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getUpdatedFlow().isStrict());
128
129         forwardingRulesManager.close();
130     }
131
132     @Test
133     public void updateFlowScopeTest() throws Exception {
134         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock);
135         forwardingRulesManager.start();
136
137         addFlowCapableNode(s1Key);
138
139         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
140         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
141                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
142         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
143                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
144         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
145         IpMatch ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)4)).build();
146         Match match = new MatchBuilder().setIpMatch(ipMatch).build();
147         Flow flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
148
149         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
150         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
151         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
152         assertCommit(writeTx.submit());
153         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
154         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
155         assertEquals(1, addFlowCalls.size());
156         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
157
158         flowKey = new FlowKey(new FlowId("test_Flow"));
159         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
160                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
161         ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)5)).build();
162         match = new MatchBuilder().setIpMatch(ipMatch).build();
163         flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
164         writeTx = getDataBroker().newWriteOnlyTransaction();
165         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
166         assertCommit(writeTx.submit());
167         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
168         List<UpdateFlowInput> updateFlowCalls = salFlowService.getUpdateFlowCalls();
169         assertEquals(1, updateFlowCalls.size());
170         assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
171         assertEquals(ipMatch, updateFlowCalls.get(0).getUpdatedFlow().getMatch().getIpMatch());
172         forwardingRulesManager.close();
173     }
174
175     @Test
176     public void deleteFlowTest() throws Exception {
177         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock);
178         forwardingRulesManager.start();
179
180         addFlowCapableNode(s1Key);
181
182         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
183         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
184                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
185         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
186                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
187         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
188         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
189
190         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
191         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
192         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
193         assertCommit(writeTx.submit());
194         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
195         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
196         assertEquals(1, addFlowCalls.size());
197         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
198
199         writeTx = getDataBroker().newWriteOnlyTransaction();
200         writeTx.delete(LogicalDatastoreType.CONFIGURATION, flowII);
201         assertCommit(writeTx.submit());
202         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
203         List<RemoveFlowInput> removeFlowCalls = salFlowService.getRemoveFlowCalls();
204         assertEquals(1, removeFlowCalls.size());
205         assertEquals(flowII, removeFlowCalls.get(0).getFlowRef().getValue());
206         assertEquals(Boolean.TRUE, removeFlowCalls.get(0).isStrict());
207
208         forwardingRulesManager.close();
209     }
210 }