Merge "EPG controller and service"
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / FlowUtilsTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import java.math.BigInteger;
17 import java.util.Arrays;
18
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.RegMatch;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg7;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.DstChoice;
49 import org.opendaylight.yangtools.yang.binding.Augmentation;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51
52 public class FlowUtilsTest {
53
54     private MatchBuilder match;
55
56     @Before
57     public void initialisation() {
58         match = mock(MatchBuilder.class);
59
60         GeneralAugMatchNodesNodeTableFlow augMatch = mock(GeneralAugMatchNodesNodeTableFlow.class);
61         when(match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class)).thenReturn(augMatch);
62         ExtensionList extensionList = mock(ExtensionList.class);
63         when(augMatch.getExtensionList()).thenReturn(Arrays.asList(extensionList));
64     }
65
66     @Test
67     public void createNodePathTest() {
68         NodeId nodeId = mock(NodeId.class);
69         InstanceIdentifier<Node> iINode = FlowUtils.createNodePath(nodeId);
70         Assert.assertEquals(nodeId, InstanceIdentifier.keyOf(iINode).getId());
71     }
72
73     @Test
74     public void createTablePathTest() {
75         NodeId nodeId = mock(NodeId.class);
76         short tableId = 5;
77         InstanceIdentifier<Table> iITable = FlowUtils.createTablePath(nodeId, tableId);
78         Assert.assertEquals(Short.valueOf(tableId), InstanceIdentifier.keyOf(iITable).getId());
79         InstanceIdentifier<Node> iINode = iITable.firstIdentifierOf(Node.class);
80         Assert.assertNotNull(iINode);
81         Assert.assertEquals(nodeId, InstanceIdentifier.keyOf(iINode).getId());
82     }
83
84     @Test
85     public void createGroupPathTest() {
86         NodeId nodeId = mock(NodeId.class);
87         GroupId groupId = mock(GroupId.class);
88         InstanceIdentifier<Group> iIGroup = FlowUtils.createGroupPath(nodeId, groupId);
89         Assert.assertEquals(groupId, InstanceIdentifier.keyOf(iIGroup).getGroupId());
90         InstanceIdentifier<Node> iINode = iIGroup.firstIdentifierOf(Node.class);
91         Assert.assertNotNull(iINode);
92         Assert.assertEquals(nodeId, InstanceIdentifier.keyOf(iINode).getId());
93     }
94
95     @Test
96     public void createBucketPathTest() {
97         NodeId nodeId = mock(NodeId.class);
98         GroupId groupId = mock(GroupId.class);
99         BucketId bucketId = mock(BucketId.class);
100         InstanceIdentifier<Bucket> iIBucket = FlowUtils.createBucketPath(nodeId, groupId, bucketId);
101         Assert.assertEquals(bucketId, InstanceIdentifier.keyOf(iIBucket).getBucketId());
102         InstanceIdentifier<Group> iIGroup = iIBucket.firstIdentifierOf(Group.class);
103         Assert.assertEquals(groupId, InstanceIdentifier.keyOf(iIGroup).getGroupId());
104         InstanceIdentifier<Node> iINode = iIGroup.firstIdentifierOf(Node.class);
105         Assert.assertEquals(nodeId, InstanceIdentifier.keyOf(iINode).getId());
106     }
107
108     @Test
109     public void gotoTableInstructionsTest() {
110         Assert.assertNotNull(FlowUtils.gotoTableInstructions((short) 5));
111     }
112
113     @Test
114     public void gotoTableInsTest() {
115         Assert.assertNotNull(FlowUtils.gotoTableIns((short) 5));
116     }
117
118     @Test
119     public void applyActionInsTest() {
120         ActionBuilder actionBuilder = mock(ActionBuilder.class);
121         when(actionBuilder.setOrder(any(Integer.class))).thenReturn(actionBuilder);
122         Assert.assertNotNull(FlowUtils.applyActionIns(Arrays.asList(actionBuilder)));
123     }
124
125     @Test
126     public void writeActionsInsTestActionBuilder() {
127         ActionBuilder actionBuilder = mock(ActionBuilder.class);
128         when(actionBuilder.setOrder(any(Integer.class))).thenReturn(actionBuilder);
129         Assert.assertNotNull(FlowUtils.writeActionIns(Arrays.asList(actionBuilder)));
130     }
131
132     @Test
133     public void writeActionInsTestAction() {
134         Action action = mock(Action.class);
135         Assert.assertNotNull(FlowUtils.writeActionIns(action));
136     }
137
138     @Test
139     public void dropInstructionsTest() {
140         Assert.assertNotNull(FlowUtils.dropInstructions());
141     }
142
143     @Test
144     public void dropActionTest() {
145         Assert.assertNotNull(FlowUtils.dropAction());
146     }
147
148     @Test
149     public void outputActionTest() {
150         NodeConnectorId nodeConnectorId = mock(NodeConnectorId.class);
151         when(nodeConnectorId.getValue()).thenReturn("value");
152         Assert.assertNotNull(FlowUtils.outputAction(nodeConnectorId));
153     }
154
155     @Test
156     public void groupActionTest() {
157         Assert.assertNotNull(FlowUtils.groupAction(5L));
158     }
159
160     @Test
161     public void setDlSrcActionTest() {
162         Assert.assertNotNull(FlowUtils.setDlSrcAction(mock(MacAddress.class)));
163     }
164
165     @Test
166     public void setDlDstActionTest() {
167         Assert.assertNotNull(FlowUtils.setDlDstAction(mock(MacAddress.class)));
168     }
169
170     @Test
171     public void setIpv4DstActionTest() {
172         Ipv4Address ipAddress = mock(Ipv4Address.class);
173         when(ipAddress.getValue()).thenReturn("127.0.0.1");
174         Assert.assertNotNull(FlowUtils.setIpv4DstAction(ipAddress));
175     }
176
177     @Test
178     public void setIpv6DstActionTest() {
179         Ipv6Address ipAddress = mock(Ipv6Address.class);
180         when(ipAddress.getValue()).thenReturn("0:0:0:0:0:0:0:1");
181         Assert.assertNotNull(FlowUtils.setIpv6DstAction(ipAddress));
182     }
183
184     @Test
185     public void setIpv4SrcActionTest() {
186         Ipv4Address ipAddress = mock(Ipv4Address.class);
187         when(ipAddress.getValue()).thenReturn("127.0.0.1");
188         Assert.assertNotNull(FlowUtils.setIpv4SrcAction(ipAddress));
189     }
190
191     @Test
192     public void setIpv6SrcActionTest() {
193         Ipv6Address ipAddress = mock(Ipv6Address.class);
194         when(ipAddress.getValue()).thenReturn("0:0:0:0:0:0:0:1");
195         Assert.assertNotNull(FlowUtils.setIpv6SrcAction(ipAddress));
196     }
197
198     @Test
199     public void decNwTtlActionTest() {
200         Assert.assertNotNull(FlowUtils.decNwTtlAction());
201     }
202
203     @Test
204     public void nxSetNsiActionTest() {
205         Assert.assertNotNull(FlowUtils.nxSetNsiAction((short) 5));
206     }
207
208     @Test
209     public void nxSetNspActionTest() {
210         Assert.assertNotNull(FlowUtils.nxSetNspAction(5L));
211     }
212
213     @Test
214     public void nxLoadRegActionTest() {
215         Assert.assertNotNull(FlowUtils.nxLoadRegAction(mock(DstChoice.class), BigInteger.ONE));
216         Assert.assertNotNull(FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.ONE));
217     }
218
219     @Test
220     public void nxLoadNshc1RegAction() {
221         Assert.assertNotNull(FlowUtils.nxLoadNshc1RegAction(5L));
222     }
223
224     @Test
225     public void nxLoadNshc2RegAction() {
226         Assert.assertNotNull(FlowUtils.nxLoadNshc2RegAction(5L));
227     }
228
229     @Test
230     public void nxLoadNshc3RegAction() {
231         Assert.assertNotNull(FlowUtils.nxLoadNshc3RegAction(5L));
232     }
233
234     @Test
235     public void nxLoadNshc4RegAction() {
236         Assert.assertNotNull(FlowUtils.nxLoadNshc4RegAction(5L));
237     }
238
239     @Test
240     public void nxLoadTunIPv4ActionTest() {
241         Assert.assertNotNull(FlowUtils.nxLoadTunIPv4Action("127.0.0.1", true));
242     }
243
244     @Test
245     public void nxLoadArpOpActionTest() {
246         Assert.assertNotNull(FlowUtils.nxLoadArpOpAction(BigInteger.ONE));
247     }
248
249     @Test
250     public void nxLoadArpShaActionTest() {
251         Assert.assertNotNull(FlowUtils.nxLoadArpShaAction(BigInteger.ONE));
252     }
253
254     @Test
255     public void nxLoadArpSpaActionTest() {
256         Assert.assertNotNull(FlowUtils.nxLoadArpSpaAction(BigInteger.ONE));
257         Assert.assertNotNull(FlowUtils.nxLoadArpSpaAction("127.0.0.1"));
258     }
259
260     @Test
261     public void nxMoveRegTunIdtoNshc1Test() {
262         Assert.assertNotNull(FlowUtils.nxMoveRegTunDstToNshc1());
263     }
264
265     @Test
266     public void nxMoveTunIdtoNshc2Test() {
267         Assert.assertNotNull(FlowUtils.nxMoveTunIdtoNshc2());
268     }
269
270     @Test
271     public void nxMoveRegTunIdActionTest() {
272         Assert.assertNotNull(FlowUtils.nxMoveRegTunIdAction(NxmNxReg0.class, true));
273     }
274
275     @Test
276     public void nxLoadTunIdActionTest() {
277         Assert.assertNotNull(FlowUtils.nxLoadTunIdAction(BigInteger.ONE, true));
278     }
279
280     @Test
281     public void nxMoveArpShaToArpThaAction() {
282         Assert.assertNotNull(FlowUtils.nxMoveArpShaToArpThaAction());
283     }
284
285     @Test
286     public void nxMoveEthSrcToEthDstAction() {
287         Assert.assertNotNull(FlowUtils.nxMoveEthSrcToEthDstAction());
288     }
289
290     @Test
291     public void nxMoveArpSpaToArpTpaActionTest() {
292         Assert.assertNotNull(FlowUtils.nxMoveArpSpaToArpTpaAction());
293     }
294
295     @Test
296     public void nxOutputRegActionTest() {
297         Assert.assertNotNull(FlowUtils.nxOutputRegAction(NxmNxReg0.class));
298     }
299
300     @SuppressWarnings("unchecked")
301     @Test
302     public void addNxRegMatchTest() {
303         MatchBuilder match = mock(MatchBuilder.class);
304         RegMatch regMatch0 = new RegMatch(NxmNxReg0.class, 5L);
305         RegMatch regMatch1 = new RegMatch(NxmNxReg1.class, 5L);
306         RegMatch regMatch2 = new RegMatch(NxmNxReg2.class, 5L);
307         RegMatch regMatch3 = new RegMatch(NxmNxReg3.class, 5L);
308         RegMatch regMatch4 = new RegMatch(NxmNxReg4.class, 5L);
309         RegMatch regMatch5 = new RegMatch(NxmNxReg5.class, 5L);
310         RegMatch regMatch6 = new RegMatch(NxmNxReg6.class, 5L);
311         RegMatch regMatch7 = new RegMatch(NxmNxReg7.class, 5L);
312         FlowUtils.addNxRegMatch(match, regMatch0, regMatch1, regMatch2, regMatch3, regMatch4, regMatch5, regMatch6,
313                 regMatch7);
314         verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
315     }
316
317     @SuppressWarnings("unchecked")
318     @Test
319     public void addNxNshc1RegMatchTest() {
320         Long value = Long.valueOf(5L);
321         FlowUtils.addNxNshc1RegMatch(match, value);
322         verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
323     }
324
325     @SuppressWarnings("unchecked")
326     @Test
327     public void addNxNshc2RegMatchTest() {
328         Long value = Long.valueOf(5L);
329         FlowUtils.addNxNshc2RegMatch(match, value);
330         verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
331     }
332
333     @SuppressWarnings("unchecked")
334     @Test
335     public void addNxNshc3RegMatchTest() {
336         Long value = Long.valueOf(5L);
337         FlowUtils.addNxNshc3RegMatch(match, value);
338         verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
339     }
340
341     @SuppressWarnings("unchecked")
342     @Test
343     public void addNxNshc4RegMatchTest() {
344         Long value = Long.valueOf(5L);
345         FlowUtils.addNxNshc4RegMatch(match, value);
346         verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
347     }
348
349     @SuppressWarnings("unchecked")
350     @Test
351     public void addNxTunIdMatchTest() {
352         int tunId = 5;
353         FlowUtils.addNxTunIdMatch(match, tunId);
354         verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
355     }
356
357     @SuppressWarnings("unchecked")
358     @Test
359     public void addNxTunIpv4DstMatchTest() {
360         Ipv4Address ipv4Address = mock(Ipv4Address.class);
361         FlowUtils.addNxTunIpv4DstMatch(match, ipv4Address);
362         verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
363     }
364
365     @SuppressWarnings("unchecked")
366     @Test
367     public void addNxNsiMatchTest() {
368         Short nsi = Short.valueOf((short) 5);
369         FlowUtils.addNxNsiMatch(match, nsi);
370         verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
371     }
372
373     @SuppressWarnings("unchecked")
374     @Test
375     public void addNxNspMatchTest() {
376         Long nsp = Long.valueOf(5L);
377         FlowUtils.addNxNspMatch(match, nsp);
378         verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
379     }
380
381     @Test
382     public void ethernetMatchTest() {
383         MacAddress srcMac = mock(MacAddress.class);
384         MacAddress dstMac = mock(MacAddress.class);
385         Long etherType = Long.valueOf(5L);
386
387         EthernetMatch ethernetMatch;
388         ethernetMatch = FlowUtils.ethernetMatch(srcMac, dstMac, etherType);
389         Assert.assertEquals(srcMac, ethernetMatch.getEthernetSource().getAddress());
390         Assert.assertEquals(dstMac, ethernetMatch.getEthernetDestination().getAddress());
391         Assert.assertEquals(etherType, ethernetMatch.getEthernetType().getType().getValue());
392
393         ethernetMatch = FlowUtils.ethernetMatch(null, null, null);
394         Assert.assertNull(ethernetMatch.getEthernetSource());
395         Assert.assertNull(ethernetMatch.getEthernetDestination());
396         Assert.assertNull(ethernetMatch.getEthernetType());
397     }
398
399     @Test
400     public void getOfPortNumTest() {
401         NodeConnectorId nodeConnectorId = mock(NodeConnectorId.class);
402         when(nodeConnectorId.getValue()).thenReturn(":5");
403         Assert.assertEquals(5, FlowUtils.getOfPortNum(nodeConnectorId));
404     }
405
406     @Test(expected = NumberFormatException.class)
407     public void getOfPortNumTestException1() {
408         NodeConnectorId nodeConnectorId = mock(NodeConnectorId.class);
409         when(nodeConnectorId.getValue()).thenReturn("5:");
410         Assert.assertEquals(5, FlowUtils.getOfPortNum(nodeConnectorId));
411     }
412
413     @Test(expected = NumberFormatException.class)
414     public void getOfPortNumTestException2() {
415         NodeConnectorId nodeConnectorId = mock(NodeConnectorId.class);
416         when(nodeConnectorId.getValue()).thenReturn("5");
417         Assert.assertEquals(5, FlowUtils.getOfPortNum(nodeConnectorId));
418     }
419 }