2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
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;
16 import java.math.BigInteger;
17 import java.util.Arrays;
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;
52 public class FlowUtilsTest {
54 private MatchBuilder match;
57 public void initialisation() {
58 match = mock(MatchBuilder.class);
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));
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());
74 public void createTablePathTest() {
75 NodeId nodeId = mock(NodeId.class);
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());
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());
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());
109 public void gotoTableInstructionsTest() {
110 Assert.assertNotNull(FlowUtils.gotoTableInstructions((short) 5));
114 public void gotoTableInsTest() {
115 Assert.assertNotNull(FlowUtils.gotoTableIns((short) 5));
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)));
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)));
133 public void writeActionInsTestAction() {
134 Action action = mock(Action.class);
135 Assert.assertNotNull(FlowUtils.writeActionIns(action));
139 public void dropInstructionsTest() {
140 Assert.assertNotNull(FlowUtils.dropInstructions());
144 public void dropActionTest() {
145 Assert.assertNotNull(FlowUtils.dropAction());
149 public void outputActionTest() {
150 NodeConnectorId nodeConnectorId = mock(NodeConnectorId.class);
151 when(nodeConnectorId.getValue()).thenReturn("value");
152 Assert.assertNotNull(FlowUtils.outputAction(nodeConnectorId));
156 public void groupActionTest() {
157 Assert.assertNotNull(FlowUtils.groupAction(5L));
161 public void setDlSrcActionTest() {
162 Assert.assertNotNull(FlowUtils.setDlSrcAction(mock(MacAddress.class)));
166 public void setDlDstActionTest() {
167 Assert.assertNotNull(FlowUtils.setDlDstAction(mock(MacAddress.class)));
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));
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));
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));
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));
199 public void decNwTtlActionTest() {
200 Assert.assertNotNull(FlowUtils.decNwTtlAction());
204 public void nxSetNsiActionTest() {
205 Assert.assertNotNull(FlowUtils.nxSetNsiAction((short) 5));
209 public void nxSetNspActionTest() {
210 Assert.assertNotNull(FlowUtils.nxSetNspAction(5L));
214 public void nxLoadRegActionTest() {
215 Assert.assertNotNull(FlowUtils.nxLoadRegAction(mock(DstChoice.class), BigInteger.ONE));
216 Assert.assertNotNull(FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.ONE));
220 public void nxLoadNshc1RegAction() {
221 Assert.assertNotNull(FlowUtils.nxLoadNshc1RegAction(5L));
225 public void nxLoadNshc2RegAction() {
226 Assert.assertNotNull(FlowUtils.nxLoadNshc2RegAction(5L));
230 public void nxLoadNshc3RegAction() {
231 Assert.assertNotNull(FlowUtils.nxLoadNshc3RegAction(5L));
235 public void nxLoadNshc4RegAction() {
236 Assert.assertNotNull(FlowUtils.nxLoadNshc4RegAction(5L));
240 public void nxLoadTunIPv4ActionTest() {
241 Assert.assertNotNull(FlowUtils.nxLoadTunIPv4Action("127.0.0.1", true));
245 public void nxLoadArpOpActionTest() {
246 Assert.assertNotNull(FlowUtils.nxLoadArpOpAction(BigInteger.ONE));
250 public void nxLoadArpShaActionTest() {
251 Assert.assertNotNull(FlowUtils.nxLoadArpShaAction(BigInteger.ONE));
255 public void nxLoadArpSpaActionTest() {
256 Assert.assertNotNull(FlowUtils.nxLoadArpSpaAction(BigInteger.ONE));
257 Assert.assertNotNull(FlowUtils.nxLoadArpSpaAction("127.0.0.1"));
261 public void nxMoveRegTunIdtoNshc1Test() {
262 Assert.assertNotNull(FlowUtils.nxMoveRegTunDstToNshc1());
266 public void nxMoveTunIdtoNshc2Test() {
267 Assert.assertNotNull(FlowUtils.nxMoveTunIdtoNshc2());
271 public void nxMoveRegTunIdActionTest() {
272 Assert.assertNotNull(FlowUtils.nxMoveRegTunIdAction(NxmNxReg0.class, true));
276 public void nxLoadTunIdActionTest() {
277 Assert.assertNotNull(FlowUtils.nxLoadTunIdAction(BigInteger.ONE, true));
281 public void nxMoveArpShaToArpThaAction() {
282 Assert.assertNotNull(FlowUtils.nxMoveArpShaToArpThaAction());
286 public void nxMoveEthSrcToEthDstAction() {
287 Assert.assertNotNull(FlowUtils.nxMoveEthSrcToEthDstAction());
291 public void nxMoveArpSpaToArpTpaActionTest() {
292 Assert.assertNotNull(FlowUtils.nxMoveArpSpaToArpTpaAction());
296 public void nxOutputRegActionTest() {
297 Assert.assertNotNull(FlowUtils.nxOutputRegAction(NxmNxReg0.class));
300 @SuppressWarnings("unchecked")
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,
314 verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
317 @SuppressWarnings("unchecked")
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));
325 @SuppressWarnings("unchecked")
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));
333 @SuppressWarnings("unchecked")
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));
341 @SuppressWarnings("unchecked")
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));
349 @SuppressWarnings("unchecked")
351 public void addNxTunIdMatchTest() {
353 FlowUtils.addNxTunIdMatch(match, tunId);
354 verify(match).addAugmentation(any(Class.class), any(Augmentation.class));
357 @SuppressWarnings("unchecked")
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));
365 @SuppressWarnings("unchecked")
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));
373 @SuppressWarnings("unchecked")
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));
382 public void ethernetMatchTest() {
383 MacAddress srcMac = mock(MacAddress.class);
384 MacAddress dstMac = mock(MacAddress.class);
385 Long etherType = Long.valueOf(5L);
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());
393 ethernetMatch = FlowUtils.ethernetMatch(null, null, null);
394 Assert.assertNull(ethernetMatch.getEthernetSource());
395 Assert.assertNull(ethernetMatch.getEthernetDestination());
396 Assert.assertNull(ethernetMatch.getEthernetType());
400 public void getOfPortNumTest() {
401 NodeConnectorId nodeConnectorId = mock(NodeConnectorId.class);
402 when(nodeConnectorId.getValue()).thenReturn(":5");
403 Assert.assertEquals(5, FlowUtils.getOfPortNum(nodeConnectorId));
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));
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));