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 / TcpDstConvertorTest.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.dst.grouping.TcpDstValuesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpDstCaseValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpDstCaseValueBuilder;
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.NxmOfTcpDstKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.dst.grouping.NxmOfTcpDstBuilder;
35 import org.opendaylight.yangtools.yang.binding.Augmentation;
36
37 /**
38  * Test for {@link TcpDstConvertor}.
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class TcpDstConvertorTest {
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 TcpDstConvertor tcpDstConvertor;
50
51     @Before
52     public void setUp() throws Exception {
53         final NxmOfTcpDstBuilder nxmOfTcpDstBuilder = new NxmOfTcpDstBuilder()
54                 .setMask(1)
55                 .setPort(DEFAULT_PORT);
56         final NxAugMatchNodesNodeTableFlowBuilder nxAugMatchNotifUpdateFlowStatsBuilder =
57                 new NxAugMatchNodesNodeTableFlowBuilder();
58         nxAugMatchNotifUpdateFlowStatsBuilder.setNxmOfTcpDst(nxmOfTcpDstBuilder.build());
59
60         final Augmentation<Extension> extensionAugmentation = nxAugMatchNotifUpdateFlowStatsBuilder.build();
61         when(extension.augmentation(ArgumentMatchers.<Class<Augmentation<Extension>>>any()))
62             .thenReturn(extensionAugmentation);
63
64         tcpDstConvertor = new TcpDstConvertor();
65     }
66
67     @Test
68     public void testConvert() throws Exception {
69         final MatchEntry converted = tcpDstConvertor.convert(extension);
70         Assert.assertEquals(1,
71                 ((TcpDstCaseValue) converted.getMatchEntryValue()).getTcpDstValues().getMask().intValue());
72         Assert.assertEquals(DEFAULT_PORT,
73                 ((TcpDstCaseValue) converted.getMatchEntryValue()).getTcpDstValues().getPort());
74     }
75
76     @Test
77     public void testConvert1() throws Exception {
78         final TcpDstValuesBuilder tcpDstValuesBuilder = new TcpDstValuesBuilder()
79                 .setMask(2)
80                 .setPort(DEFAULT_PORT);
81         final TcpDstCaseValueBuilder tcpDstCaseValueBuilder = new TcpDstCaseValueBuilder()
82                 .setTcpDstValues(tcpDstValuesBuilder.build());
83
84         final TcpDstCaseValue tcpDstCaseValue = tcpDstCaseValueBuilder.build();
85
86         when(matchEntry.getMatchEntryValue()).thenReturn(tcpDstCaseValue);
87
88         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment = tcpDstConvertor.convert(matchEntry,
89                 MatchPath.PACKET_RECEIVED_MATCH);
90         Assert.assertEquals(2, ((NxAugMatchNotifPacketIn) extensionAugment.getAugmentationObject()).getNxmOfTcpDst()
91                 .getMask().intValue());
92         Assert.assertEquals(DEFAULT_PORT,
93                 ((NxAugMatchNotifPacketIn) extensionAugment.getAugmentationObject()).getNxmOfTcpDst().getPort());
94         Assert.assertEquals(extensionAugment.getKey(), NxmOfTcpDstKey.class);
95
96         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment1 = tcpDstConvertor
97                 .convert(matchEntry, MatchPath.SWITCH_FLOW_REMOVED_MATCH);
98         Assert.assertEquals(2, ((NxAugMatchNotifSwitchFlowRemoved) extensionAugment1.getAugmentationObject())
99                 .getNxmOfTcpDst().getMask().intValue());
100         Assert.assertEquals(DEFAULT_PORT, ((NxAugMatchNotifSwitchFlowRemoved) extensionAugment1.getAugmentationObject())
101                 .getNxmOfTcpDst().getPort());
102         Assert.assertEquals(extensionAugment.getKey(), NxmOfTcpDstKey.class);
103
104         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment2 = tcpDstConvertor
105                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_UPDATE_MATCH);
106         Assert.assertEquals(2, ((NxAugMatchNodesNodeTableFlow) extensionAugment2.getAugmentationObject())
107                 .getNxmOfTcpDst().getMask().intValue());
108         Assert.assertEquals(DEFAULT_PORT,
109                 ((NxAugMatchNodesNodeTableFlow) extensionAugment2.getAugmentationObject()).getNxmOfTcpDst().getPort());
110         Assert.assertEquals(extensionAugment.getKey(), NxmOfTcpDstKey.class);
111
112         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment3 = tcpDstConvertor
113                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_RPC_MATCH);
114         Assert.assertEquals(2, ((NxAugMatchRpcGetFlowStats) extensionAugment3.getAugmentationObject()).getNxmOfTcpDst()
115                 .getMask().intValue());
116         Assert.assertEquals(DEFAULT_PORT,
117                 ((NxAugMatchRpcGetFlowStats) extensionAugment3.getAugmentationObject()).getNxmOfTcpDst().getPort());
118         Assert.assertEquals(extensionAugment.getKey(), NxmOfTcpDstKey.class);
119     }
120 }