Updating features archetype to talk about user-facing features.
[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
128         forwardingRulesManager.close();
129     }
130
131     @Test
132     public void updateFlowScopeTest() throws Exception {
133         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock);
134         forwardingRulesManager.start();
135
136         addFlowCapableNode(s1Key);
137
138         FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
139         InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
140                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
141         InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
142                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
143         Table table = new TableBuilder().setKey(tableKey).setFlow(Collections.<Flow>emptyList()).build();
144         IpMatch ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)4)).build();
145         Match match = new MatchBuilder().setIpMatch(ipMatch).build();
146         Flow flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
147
148         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
149         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
150         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
151         assertCommit(writeTx.submit());
152         SalFlowServiceMock salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
153         List<AddFlowInput> addFlowCalls = salFlowService.getAddFlowCalls();
154         assertEquals(1, addFlowCalls.size());
155         assertEquals("DOM-0", addFlowCalls.get(0).getTransactionUri().getValue());
156
157         flowKey = new FlowKey(new FlowId("test_Flow"));
158         flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
159                 .augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey);
160         ipMatch = new IpMatchBuilder().setIpDscp(new Dscp((short)5)).build();
161         match = new MatchBuilder().setIpMatch(ipMatch).build();
162         flow = new FlowBuilder().setMatch(match).setKey(flowKey).setTableId((short) 2).build();
163         writeTx = getDataBroker().newWriteOnlyTransaction();
164         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
165         assertCommit(writeTx.submit());
166         salFlowService = (SalFlowServiceMock) forwardingRulesManager.getSalFlowService();
167         List<UpdateFlowInput> updateFlowCalls = salFlowService.getUpdateFlowCalls();
168         assertEquals(1, updateFlowCalls.size());
169         assertEquals("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue());
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("DOM-1", removeFlowCalls.get(0).getTransactionUri().getValue());
206         assertEquals(flowII, removeFlowCalls.get(0).getFlowRef().getValue());
207
208         forwardingRulesManager.close();
209     }
210 }