Fixed discard-changes for mdsal netconf, mapping code cleanup.
[controller.git] / opendaylight / md-sal / 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.frm.impl.ForwardingRulesManagerImpl;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
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 import test.mock.util.FRMTest;
37 import test.mock.util.RpcProviderRegistryMock;
38 import test.mock.util.SalFlowServiceMock;
39
40 import java.util.Collections;
41 import java.util.List;
42
43 import static org.junit.Assert.assertEquals;
44
45 public class FlowListenerTest extends FRMTest {
46     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
47     NodeKey s1Key = new NodeKey(new NodeId("S1"));
48     TableKey tableKey = new TableKey((short) 2);
49
50     @Test
51     public void addTwoFlowsTest() throws Exception {
52         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock);
53         forwardingRulesManager.start();
54
55         addFlowCapableNode(s1Key);
56
57         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
58         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
59                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
60         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
61                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
62         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
63         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
64
65         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
66         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
67         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
68         assertCommit(writeTx.submit());
69         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
70         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
71         assertEquals(1, addFlowCalls.size());
72         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
73
74         flowKey = new FlowKey(new FlowId("test_Flow2"));
75         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
76                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
77         flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
78         writeTx = getDataBroker().newWriteOnlyTransaction();
79         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
80         assertCommit(writeTx.submit());
81         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
82         addFlowCalls = salFlowService.getAddFlowCalls();
83         assertEquals(2, addFlowCalls.size());
84         assertEquals("DOM-1", addFlowCalls.get(1).getTransactionUri().getValue());
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("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue());
126         assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
127         assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getOriginalFlow().isStrict());
128         assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getUpdatedFlow().isStrict());
129
130         forwardingRulesManager.close();
131     }
132
133     @Test
134     public void updateFlowScopeTest() throws Exception {
135         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock);
136         forwardingRulesManager.start();
137
138         addFlowCapableNode(s1Key);
139
140         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
141         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
142                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
143         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
144                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
145         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
146         IpMatch ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)4)).build();
147         Match match = new MatchBuilder().setIpMatch(ipMatch).build();
148         Flow flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
149
150         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
151         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
152         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
153         assertCommit(writeTx.submit());
154         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
155         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
156         assertEquals(1, addFlowCalls.size());
157         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
158
159         flowKey = new FlowKey(new FlowId("test_Flow"));
160         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
161                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
162         ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)5)).build();
163         match = new MatchBuilder().setIpMatch(ipMatch).build();
164         flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
165         writeTx = getDataBroker().newWriteOnlyTransaction();
166         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
167         assertCommit(writeTx.submit());
168         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
169         List<UpdateFlowInput> updateFlowCalls = salFlowService.getUpdateFlowCalls();
170         assertEquals(1, updateFlowCalls.size());
171         assertEquals("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue());
172         assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
173         assertEquals(ipMatch, updateFlowCalls.get(0).getUpdatedFlow().getMatch().getIpMatch());
174         forwardingRulesManager.close();
175     }
176
177     @Test
178     public void deleteFlowTest() throws Exception {
179         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock);
180         forwardingRulesManager.start();
181
182         addFlowCapableNode(s1Key);
183
184         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
185         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
186                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
187         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
188                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
189         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
190         Flow flow = new FlowBuilder().setKey(flowKey).setTableId((short) 2).build();
191
192         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
193         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
194         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
195         assertCommit(writeTx.submit());
196         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
197         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
198         assertEquals(1, addFlowCalls.size());
199         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
200
201         writeTx = getDataBroker().newWriteOnlyTransaction();
202         writeTx.delete(LogicalDatastoreType.CONFIGURATION, flowII);
203         assertCommit(writeTx.submit());
204         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
205         List<RemoveFlowInput> removeFlowCalls = salFlowService.getRemoveFlowCalls();
206         assertEquals(1, removeFlowCalls.size());
207         assertEquals("DOM-1", removeFlowCalls.get(0).getTransactionUri().getValue());
208         assertEquals(flowII, removeFlowCalls.get(0).getFlowRef().getValue());
209         assertEquals(Boolean.TRUE, removeFlowCalls.get(0).isStrict());
210
211         forwardingRulesManager.close();
212     }
213 }