a4bd524d4f9604a706c3c11df1b57ebd6ea884f8
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / test / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / UdpSrcConvertorTest.java
1 /**
2  * Copyright (c) 2016 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.openflowplugin.extension.vendor.nicira.convertor.match;
10
11 import static org.mockito.Mockito.when;
12
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.udp.src.grouping.UdpSrcValuesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.UdpSrcCaseValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.UdpSrcCaseValueBuilder;
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.NxmOfUdpSrcKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.udp.src.grouping.NxmOfUdpSrcBuilder;
35 import org.opendaylight.yangtools.yang.binding.Augmentation;
36
37 /**
38  * Test for {@link UdpSrcConvertor}.
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class UdpSrcConvertorTest {
42     @Mock
43     private Extension extension;
44     @Mock
45     private MatchEntry matchEntry;
46
47     private static final PortNumber DEFAULT_PORT = new PortNumber(9999);
48
49     private UdpSrcConvertor udpSrcConvertor;
50
51     @Before
52     public void setUp() throws Exception {
53         final NxmOfUdpSrcBuilder nxmOfUdpSrcBuilder = new NxmOfUdpSrcBuilder()
54                 .setMask(1)
55                 .setPort(DEFAULT_PORT);
56         final NxAugMatchNodesNodeTableFlowBuilder nxAugMatchNotifUpdateFlowStatsBuilder =
57                 new NxAugMatchNodesNodeTableFlowBuilder();
58         nxAugMatchNotifUpdateFlowStatsBuilder.setNxmOfUdpSrc(nxmOfUdpSrcBuilder.build());
59
60         final Augmentation<Extension> extensionAugmentation = nxAugMatchNotifUpdateFlowStatsBuilder.build();
61         when(extension.getAugmentation(Matchers.<Class<Augmentation<Extension>>>any()))
62             .thenReturn(extensionAugmentation);
63
64         udpSrcConvertor = new UdpSrcConvertor();
65     }
66
67     @Test
68     public void testConvert() throws Exception {
69         final MatchEntry converted = udpSrcConvertor.convert(extension);
70         Assert.assertEquals(Integer.valueOf(1),
71                 ((UdpSrcCaseValue) converted.getMatchEntryValue()).getUdpSrcValues().getMask());
72         Assert.assertEquals(DEFAULT_PORT,
73                 ((UdpSrcCaseValue) converted.getMatchEntryValue()).getUdpSrcValues().getPort());
74     }
75
76     @Test
77     public void testConvert1() throws Exception {
78         final UdpSrcValuesBuilder udpSrcValuesBuilder = new UdpSrcValuesBuilder()
79                 .setMask(2)
80                 .setPort(DEFAULT_PORT);
81         final UdpSrcCaseValueBuilder udpSrcCaseValueBuilder = new UdpSrcCaseValueBuilder()
82                 .setUdpSrcValues(udpSrcValuesBuilder.build());
83
84         final UdpSrcCaseValue udpSrcCaseValue = udpSrcCaseValueBuilder.build();
85
86         when(matchEntry.getMatchEntryValue()).thenReturn(udpSrcCaseValue);
87
88         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment = udpSrcConvertor.convert(matchEntry,
89                 MatchPath.PACKET_RECEIVED_MATCH);
90         Assert.assertEquals(Integer.valueOf(2),
91                 ((NxAugMatchNotifPacketIn) extensionAugment.getAugmentationObject()).getNxmOfUdpSrc().getMask());
92         Assert.assertEquals(DEFAULT_PORT,
93                 ((NxAugMatchNotifPacketIn) extensionAugment.getAugmentationObject()).getNxmOfUdpSrc().getPort());
94         Assert.assertEquals(extensionAugment.getKey(), NxmOfUdpSrcKey.class);
95
96         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment1 = udpSrcConvertor
97                 .convert(matchEntry, MatchPath.SWITCH_FLOW_REMOVED_MATCH);
98         Assert.assertEquals(Integer.valueOf(2),
99                 ((NxAugMatchNotifSwitchFlowRemoved) extensionAugment1.getAugmentationObject()).getNxmOfUdpSrc()
100                         .getMask());
101         Assert.assertEquals(DEFAULT_PORT, ((NxAugMatchNotifSwitchFlowRemoved) extensionAugment1.getAugmentationObject())
102                 .getNxmOfUdpSrc().getPort());
103         Assert.assertEquals(extensionAugment.getKey(), NxmOfUdpSrcKey.class);
104
105         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment2 = udpSrcConvertor
106                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_UPDATE_MATCH);
107         Assert.assertEquals(Integer.valueOf(2),
108                 ((NxAugMatchNodesNodeTableFlow) extensionAugment2.getAugmentationObject()).getNxmOfUdpSrc().getMask());
109         Assert.assertEquals(DEFAULT_PORT,
110                 ((NxAugMatchNodesNodeTableFlow) extensionAugment2.getAugmentationObject()).getNxmOfUdpSrc().getPort());
111         Assert.assertEquals(extensionAugment.getKey(), NxmOfUdpSrcKey.class);
112
113         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment3 = udpSrcConvertor
114                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_RPC_MATCH);
115         Assert.assertEquals(Integer.valueOf(2),
116                 ((NxAugMatchRpcGetFlowStats) extensionAugment3.getAugmentationObject()).getNxmOfUdpSrc().getMask());
117         Assert.assertEquals(DEFAULT_PORT,
118                 ((NxAugMatchRpcGetFlowStats) extensionAugment3.getAugmentationObject()).getNxmOfUdpSrc().getPort());
119         Assert.assertEquals(extensionAugment.getKey(), NxmOfUdpSrcKey.class);
120     }
121 }