Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / test / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / ArpShaConvertorTest.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.ArpShaCaseValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.ArpShaCaseValueBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.sha.grouping.ArpShaValuesBuilder;
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.NxmNxArpShaKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.arp.sha.grouping.NxmNxArpShaBuilder;
34 import org.opendaylight.yangtools.yang.binding.Augmentation;
35
36 /**
37  * Test for {@link ArpShaConvertor}.
38  */
39 @RunWith(MockitoJUnitRunner.class)
40 public class ArpShaConvertorTest {
41
42     @Mock
43     private Extension extension;
44     @Mock
45     private MatchEntry matchEntry;
46
47     private ArpShaConvertor arpShaConvertor;
48
49     private static final MacAddress MAC_ADDRESS = new MacAddress("01:23:45:67:89:AB");
50
51     @Before
52     public void setUp() {
53         final NxmNxArpShaBuilder nxmNxArpShaBuilder = new NxmNxArpShaBuilder()
54                 .setMacAddress(MAC_ADDRESS);
55         final NxAugMatchRpcAddFlowBuilder nxAugMatchRpcAddFlowBuilder = new NxAugMatchRpcAddFlowBuilder()
56                 .setNxmNxArpSha(nxmNxArpShaBuilder.build());
57         final Augmentation<Extension> extensionAugmentation = nxAugMatchRpcAddFlowBuilder.build();
58
59         when(extension.augmentation(ArgumentMatchers.any()))
60             .thenReturn(extensionAugmentation);
61
62         arpShaConvertor = new ArpShaConvertor();
63     }
64
65     @Test
66     public void testConvertToOFJava() {
67         final MatchEntry converted = arpShaConvertor.convert(extension);
68         Assert.assertEquals(MAC_ADDRESS.getValue(),
69                 ((ArpShaCaseValue) converted.getMatchEntryValue()).getArpShaValues().getMacAddress().getValue());
70     }
71
72     @Test
73     public void testConvertFromOFJava() {
74         final ArpShaValuesBuilder arpShaValuesBuilder = new ArpShaValuesBuilder()
75                 .setMacAddress(MAC_ADDRESS);
76         final ArpShaCaseValueBuilder arpShaCaseValueBuilder = new ArpShaCaseValueBuilder()
77                 .setArpShaValues(arpShaValuesBuilder.build());
78         final ArpShaCaseValue arpShaCaseValue = arpShaCaseValueBuilder.build();
79
80         when(matchEntry.getMatchEntryValue()).thenReturn(arpShaCaseValue);
81
82         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment = arpShaConvertor.convert(matchEntry,
83                 MatchPath.PACKET_RECEIVED_MATCH);
84         Assert.assertEquals(arpShaCaseValue.getArpShaValues().getMacAddress(),
85                 ((NxAugMatchNotifPacketIn) extensionAugment.getAugmentationObject()).getNxmNxArpSha().getMacAddress());
86         Assert.assertEquals(NxmNxArpShaKey.VALUE, extensionAugment.getKey());
87
88         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment1 = arpShaConvertor
89                 .convert(matchEntry, MatchPath.SWITCH_FLOW_REMOVED_MATCH);
90         Assert.assertEquals(arpShaCaseValue.getArpShaValues().getMacAddress(),
91                 ((NxAugMatchNotifSwitchFlowRemoved) extensionAugment1.getAugmentationObject()).getNxmNxArpSha()
92                         .getMacAddress());
93         Assert.assertEquals(NxmNxArpShaKey.VALUE, extensionAugment.getKey());
94
95         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment2 = arpShaConvertor
96                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_UPDATE_MATCH);
97         Assert.assertEquals(arpShaCaseValue.getArpShaValues().getMacAddress(),
98                 ((NxAugMatchNodesNodeTableFlow) extensionAugment2.getAugmentationObject()).getNxmNxArpSha()
99                         .getMacAddress());
100         Assert.assertEquals(NxmNxArpShaKey.VALUE, extensionAugment.getKey());
101
102         final ExtensionAugment<? extends Augmentation<Extension>> extensionAugment3 = arpShaConvertor
103                 .convert(matchEntry, MatchPath.FLOWS_STATISTICS_RPC_MATCH);
104         Assert.assertEquals(arpShaCaseValue.getArpShaValues().getMacAddress(),
105                 ((NxAugMatchRpcGetFlowStats) extensionAugment3.getAugmentationObject()).getNxmNxArpSha()
106                         .getMacAddress());
107         Assert.assertEquals(NxmNxArpShaKey.VALUE, extensionAugment.getKey());
108     }
109 }