75c106b0d4ef5e594b8728a1ba3962be9ddfe320
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / test / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / ArpThaConvertorTest.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 package org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match;
9
10 import static org.mockito.Mockito.when;
11
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.ArgumentMatchers;
17 import org.mockito.Mock;
18 import org.mockito.junit.MockitoJUnitRunner;
19 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
20 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.ArpThaCaseValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.ArpThaCaseValueBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.tha.grouping.ArpThaValuesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketIn;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcAddFlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcGetFlowStats;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxArpThaKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.arp.tha.grouping.NxmNxArpThaBuilder;
34 import org.opendaylight.yangtools.yang.binding.Augmentation;
35
36 /**
37  * Test for {@link ArpThaConvertor}.
38  */
39 @RunWith(MockitoJUnitRunner.class)
40 public class ArpThaConvertorTest {
41
42     @Mock
43     private Extension extension;
44     @Mock
45     private MatchEntry matchEntry;
46
47     private static final MacAddress MAC_ADDRESS =  MacAddress.getDefaultInstance("01:23:45:67:89:AB");
48
49     private ArpThaConvertor arpThaConvertor;
50
51     @Before
52     public void setUp() {
53
54         final NxmNxArpThaBuilder nxArpThaBuilder = new NxmNxArpThaBuilder()
55                 .setMacAddress(MAC_ADDRESS);
56         final NxAugMatchRpcAddFlowBuilder nxAugMatchRpcAddFlowBuilder = new NxAugMatchRpcAddFlowBuilder()
57                 .setNxmNxArpTha(nxArpThaBuilder.build());
58         final Augmentation<Extension> extensionAugmentation = nxAugMatchRpcAddFlowBuilder.build();
59
60         when(extension.augmentation(ArgumentMatchers.any()))
61             .thenReturn(extensionAugmentation);
62
63         arpThaConvertor = new ArpThaConvertor();
64     }
65
66     @Test
67     public void testConvertToOFJava() {
68         final MatchEntry converted = arpThaConvertor.convert(extension);
69         Assert.assertEquals(MAC_ADDRESS.getValue(),
70                 ((ArpThaCaseValue) converted.getMatchEntryValue()).getArpThaValues().getMacAddress().getValue());
71     }
72
73     @Test
74     public void testConvertFromOFJava() {
75         final ArpThaValuesBuilder arpThaValuesBuilder = new ArpThaValuesBuilder()
76                 .setMacAddress(MAC_ADDRESS);
77         final ArpThaCaseValueBuilder arpThaCaseValueBuilder = new ArpThaCaseValueBuilder()
78                 .setArpThaValues(arpThaValuesBuilder.build());
79         final ArpThaCaseValue arpThaCaseValue = arpThaCaseValueBuilder.build();
80
81         when(matchEntry.getMatchEntryValue()).thenReturn(arpThaCaseValue);
82
83         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment = arpThaConvertor.convert(matchEntry,
84                 MatchPath.PACKET_RECEIVED_MATCH);
85         Assert.assertEquals(arpThaCaseValue.getArpThaValues().getMacAddress(),
86                 ((NxAugMatchNotifPacketIn) extensionAugment.getAugmentationObject()).getNxmNxArpTha().getMacAddress());
87         Assert.assertEquals(NxmNxArpThaKey.VALUE, extensionAugment.getKey());
88
89         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment1 = arpThaConvertor
90                 .convert(matchEntry, MatchPath.SWITCH_FLOW_REMOVED_MATCH);
91         Assert.assertEquals(arpThaCaseValue.getArpThaValues().getMacAddress(),
92                 ((NxAugMatchNotifSwitchFlowRemoved) extensionAugment1.getAugmentationObject()).getNxmNxArpTha()
93                         .getMacAddress());
94         Assert.assertEquals(NxmNxArpThaKey.VALUE, extensionAugment.getKey());
95
96         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment2 = arpThaConvertor
97                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_UPDATE_MATCH);
98         Assert.assertEquals(arpThaCaseValue.getArpThaValues().getMacAddress(),
99                 ((NxAugMatchNodesNodeTableFlow) extensionAugment2.getAugmentationObject()).getNxmNxArpTha()
100                         .getMacAddress());
101         Assert.assertEquals(NxmNxArpThaKey.VALUE, extensionAugment.getKey());
102
103         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment3 = arpThaConvertor
104                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_RPC_MATCH);
105         Assert.assertEquals(arpThaCaseValue.getArpThaValues().getMacAddress(),
106                 ((NxAugMatchRpcGetFlowStats) extensionAugment3.getAugmentationObject()).getNxmNxArpTha()
107                         .getMacAddress());
108         Assert.assertEquals(NxmNxArpThaKey.VALUE, extensionAugment.getKey());
109     }
110 }