2 * Copyright (c) 2016 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.openflowplugin.extension.vendor.nicira.convertor.match;
11 import static org.mockito.Mockito.when;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Matchers;
18 import org.mockito.Mock;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
21 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.tcp.src.grouping.TcpSrcValuesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpSrcCaseValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpSrcCaseValueBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketIn;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemoved;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcGetFlowStats;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpSrcKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.src.grouping.NxmOfTcpSrcBuilder;
35 import org.opendaylight.yangtools.yang.binding.Augmentation;
38 * Test for {@link TcpSrcConvertor}.
40 @RunWith(MockitoJUnitRunner.class)
41 public class TcpSrcConvertorTest {
43 private Extension extension;
45 private MatchEntry matchEntry;
47 private static final PortNumber DEFAULT_PORT = new PortNumber(9999);
49 private TcpSrcConvertor tcpSrcConvertor;
52 public void setUp() throws Exception {
53 final NxmOfTcpSrcBuilder nxmOfTcpSrcBuilder = new NxmOfTcpSrcBuilder()
55 .setPort(DEFAULT_PORT);
56 final NxAugMatchNodesNodeTableFlowBuilder nxAugMatchNotifUpdateFlowStatsBuilder =
57 new NxAugMatchNodesNodeTableFlowBuilder();
58 nxAugMatchNotifUpdateFlowStatsBuilder.setNxmOfTcpSrc(nxmOfTcpSrcBuilder.build());
60 final Augmentation<Extension> extensionAugmentation = nxAugMatchNotifUpdateFlowStatsBuilder.build();
61 when(extension.getAugmentation(Matchers.<Class<Augmentation<Extension>>>any()))
62 .thenReturn(extensionAugmentation);
64 tcpSrcConvertor = new TcpSrcConvertor();
68 public void testConvert() throws Exception {
69 final MatchEntry converted = tcpSrcConvertor.convert(extension);
70 Assert.assertEquals(1,
71 ((TcpSrcCaseValue) converted.getMatchEntryValue()).getTcpSrcValues().getMask().intValue());
72 Assert.assertEquals(DEFAULT_PORT,
73 ((TcpSrcCaseValue) converted.getMatchEntryValue()).getTcpSrcValues().getPort());
77 public void testConvert1() throws Exception {
78 final TcpSrcValuesBuilder tcpSrcValuesBuilder = new TcpSrcValuesBuilder()
80 .setPort(DEFAULT_PORT);
81 final TcpSrcCaseValueBuilder tcpSrcCaseValueBuilder = new TcpSrcCaseValueBuilder()
82 .setTcpSrcValues(tcpSrcValuesBuilder.build());
84 final TcpSrcCaseValue tcpSrcCaseValue = tcpSrcCaseValueBuilder.build();
86 when(matchEntry.getMatchEntryValue()).thenReturn(tcpSrcCaseValue);
88 final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment = tcpSrcConvertor.convert(matchEntry,
89 MatchPath.PACKET_RECEIVED_MATCH);
90 Assert.assertEquals(2, ((NxAugMatchNotifPacketIn) extensionAugment.getAugmentationObject()).getNxmOfTcpSrc()
91 .getMask().intValue());
92 Assert.assertEquals(DEFAULT_PORT,
93 ((NxAugMatchNotifPacketIn) extensionAugment.getAugmentationObject()).getNxmOfTcpSrc().getPort());
94 Assert.assertEquals(extensionAugment.getKey(), NxmOfTcpSrcKey.class);
96 final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment1 = tcpSrcConvertor
97 .convert(matchEntry, MatchPath.SWITCH_FLOW_REMOVED_MATCH);
98 Assert.assertEquals(2, ((NxAugMatchNotifSwitchFlowRemoved) extensionAugment1.getAugmentationObject())
99 .getNxmOfTcpSrc().getMask().intValue());
100 Assert.assertEquals(DEFAULT_PORT, ((NxAugMatchNotifSwitchFlowRemoved) extensionAugment1.getAugmentationObject())
101 .getNxmOfTcpSrc().getPort());
102 Assert.assertEquals(extensionAugment.getKey(), NxmOfTcpSrcKey.class);
104 final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment2 = tcpSrcConvertor
105 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_UPDATE_MATCH);
106 Assert.assertEquals(2, ((NxAugMatchNodesNodeTableFlow) extensionAugment2.getAugmentationObject())
107 .getNxmOfTcpSrc().getMask().intValue());
108 Assert.assertEquals(DEFAULT_PORT,
109 ((NxAugMatchNodesNodeTableFlow) extensionAugment2.getAugmentationObject()).getNxmOfTcpSrc().getPort());
110 Assert.assertEquals(extensionAugment.getKey(), NxmOfTcpSrcKey.class);
112 final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment3 = tcpSrcConvertor
113 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_RPC_MATCH);
114 Assert.assertEquals(2, ((NxAugMatchRpcGetFlowStats) extensionAugment3.getAugmentationObject()).getNxmOfTcpSrc()
115 .getMask().intValue());
116 Assert.assertEquals(DEFAULT_PORT,
117 ((NxAugMatchRpcGetFlowStats) extensionAugment3.getAugmentationObject()).getNxmOfTcpSrc().getPort());
118 Assert.assertEquals(extensionAugment.getKey(), NxmOfTcpSrcKey.class);