13520a1923cc0da66f63cae22c7b8599af826b29
[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
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.yang.types.rev130715.MacAddress;
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.nx.match.arp.tha.grouping.ArpThaValuesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpThaCaseValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpThaCaseValueBuilder;
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.NxAugMatchNotifPacketIn;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcAddFlowBuilder;
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.NxmNxArpThaKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.arp.tha.grouping.NxmNxArpThaBuilder;
35 import org.opendaylight.yangtools.yang.binding.Augmentation;
36
37 /**
38  * Test for {@link ArpThaConvertor}.
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class ArpThaConvertorTest {
42
43     @Mock
44     private Extension extension;
45     @Mock
46     private MatchEntry matchEntry;
47
48     private static final MacAddress MAC_ADDRESS =  MacAddress.getDefaultInstance("01:23:45:67:89:AB");
49
50     private ArpThaConvertor arpThaConvertor;
51
52     @Before
53     public void setUp() throws Exception {
54
55         final NxmNxArpThaBuilder nxArpThaBuilder = new NxmNxArpThaBuilder()
56                 .setMacAddress(MAC_ADDRESS);
57         final NxAugMatchRpcAddFlowBuilder nxAugMatchRpcAddFlowBuilder = new NxAugMatchRpcAddFlowBuilder()
58                 .setNxmNxArpTha(nxArpThaBuilder.build());
59         final Augmentation<Extension> extensionAugmentation = nxAugMatchRpcAddFlowBuilder.build();
60
61         when(extension.getAugmentation(Matchers.<Class<Augmentation<Extension>>>any()))
62             .thenReturn(extensionAugmentation);
63
64         arpThaConvertor = new ArpThaConvertor();
65     }
66
67     @Test
68     public void testConvertToOFJava() throws Exception {
69         final MatchEntry converted = arpThaConvertor.convert(extension);
70         Assert.assertEquals(MAC_ADDRESS.getValue(),
71                 ((ArpThaCaseValue) converted.getMatchEntryValue()).getArpThaValues().getMacAddress().getValue());
72     }
73
74     @Test
75     public void testConvertFromOFJava() throws Exception {
76         final ArpThaValuesBuilder arpThaValuesBuilder = new ArpThaValuesBuilder()
77                 .setMacAddress(MAC_ADDRESS);
78         final ArpThaCaseValueBuilder arpThaCaseValueBuilder = new ArpThaCaseValueBuilder()
79                 .setArpThaValues(arpThaValuesBuilder.build());
80         final ArpThaCaseValue arpThaCaseValue = arpThaCaseValueBuilder.build();
81
82         when(matchEntry.getMatchEntryValue()).thenReturn(arpThaCaseValue);
83
84         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment = arpThaConvertor.convert(matchEntry,
85                 MatchPath.PACKET_RECEIVED_MATCH);
86         Assert.assertEquals(arpThaCaseValue.getArpThaValues().getMacAddress(),
87                 ((NxAugMatchNotifPacketIn) extensionAugment.getAugmentationObject()).getNxmNxArpTha().getMacAddress());
88         Assert.assertEquals(extensionAugment.getKey(), NxmNxArpThaKey.class);
89
90         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment1 = arpThaConvertor
91                 .convert(matchEntry, MatchPath.SWITCH_FLOW_REMOVED_MATCH);
92         Assert.assertEquals(arpThaCaseValue.getArpThaValues().getMacAddress(),
93                 ((NxAugMatchNotifSwitchFlowRemoved) extensionAugment1.getAugmentationObject()).getNxmNxArpTha()
94                         .getMacAddress());
95         Assert.assertEquals(extensionAugment.getKey(), NxmNxArpThaKey.class);
96
97         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment2 = arpThaConvertor
98                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_UPDATE_MATCH);
99         Assert.assertEquals(arpThaCaseValue.getArpThaValues().getMacAddress(),
100                 ((NxAugMatchNodesNodeTableFlow) extensionAugment2.getAugmentationObject()).getNxmNxArpTha()
101                         .getMacAddress());
102         Assert.assertEquals(extensionAugment.getKey(), NxmNxArpThaKey.class);
103
104         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment3 = arpThaConvertor
105                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_RPC_MATCH);
106         Assert.assertEquals(arpThaCaseValue.getArpThaValues().getMacAddress(),
107                 ((NxAugMatchRpcGetFlowStats) extensionAugment3.getAugmentationObject()).getNxmNxArpTha()
108                         .getMacAddress());
109         Assert.assertEquals(extensionAugment.getKey(), NxmNxArpThaKey.class);
110     }
111 }