OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / test / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / TcpSrcConvertorTest.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.ArgumentMatchers;
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;
36
37 /**
38  * Test for {@link TcpSrcConvertor}.
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class TcpSrcConvertorTest {
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 TcpSrcConvertor tcpSrcConvertor;
50
51     @Before
52     public void setUp() throws Exception {
53         final NxmOfTcpSrcBuilder nxmOfTcpSrcBuilder = new NxmOfTcpSrcBuilder()
54                 .setMask(1)
55                 .setPort(DEFAULT_PORT);
56         final NxAugMatchNodesNodeTableFlowBuilder nxAugMatchNotifUpdateFlowStatsBuilder =
57                 new NxAugMatchNodesNodeTableFlowBuilder();
58         nxAugMatchNotifUpdateFlowStatsBuilder.setNxmOfTcpSrc(nxmOfTcpSrcBuilder.build());
59
60         final Augmentation<Extension> extensionAugmentation = nxAugMatchNotifUpdateFlowStatsBuilder.build();
61         when(extension.augmentation(ArgumentMatchers.<Class<Augmentation<Extension>>>any()))
62             .thenReturn(extensionAugmentation);
63
64         tcpSrcConvertor = new TcpSrcConvertor();
65     }
66
67     @Test
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());
74     }
75
76     @Test
77     public void testConvert1() throws Exception {
78         final TcpSrcValuesBuilder tcpSrcValuesBuilder = new TcpSrcValuesBuilder()
79                 .setMask(2)
80                 .setPort(DEFAULT_PORT);
81         final TcpSrcCaseValueBuilder tcpSrcCaseValueBuilder = new TcpSrcCaseValueBuilder()
82                 .setTcpSrcValues(tcpSrcValuesBuilder.build());
83
84         final TcpSrcCaseValue tcpSrcCaseValue = tcpSrcCaseValueBuilder.build();
85
86         when(matchEntry.getMatchEntryValue()).thenReturn(tcpSrcCaseValue);
87
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);
95
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);
103
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);
111
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);
119     }
120 }