Move used code from openflowplugin to impl
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / util / PortTranslatorUtilTest.java
1 /*
2  * Copyright (c) 2014 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.impl.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.math.BigInteger;
16 import org.junit.Test;
17 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
26
27 /**
28  * Created by Martin Bobak mbobak@cisco.com on 7/29/14.
29  */
30 public class PortTranslatorUtilTest {
31
32     private static final String MAC_ADDRESS = "00:01:02:03:04:05";
33     private static final String NAME = "PortTranslatorTest";
34     private final Boolean[] pfBls = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
35     private final boolean[] pfV10Bls = {false, false, false, false, false, false, false, false, false, false, false, false};
36     private final boolean[] portCfgBools = {false, false, false, false};
37     private final boolean[] portCfgV10bools = {false, false, false, false, false, false, false};
38     private final boolean[] portStateBools = {false, false, false, false};
39     private final Long currentSpeed = Long.decode("4294967295");
40     private static final Long maxSpeed = Long.decode("4294967295");
41
42     /**
43      * Test  method for {@link PortTranslatorUtil#translatePortFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures)}
44      */
45     @Test
46     public void testTranslatePortFeatures() {
47
48
49         for (int i = 0; i < pfBls.length; i++) {
50             pfBls[i] = true;
51             final PortFeatures apf = getPortFeatures();
52             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf = PortTranslatorUtil.translatePortFeatures(apf);
53             assertEqualsPortFeatures(apf, npf);
54             pfBls[i] = false;
55         }
56
57     }
58
59     private PortFeatures getPortFeatures() {
60         return new PortFeatures(pfBls[0], pfBls[1], pfBls[2], pfBls[3], pfBls[4], pfBls[5], pfBls[6], pfBls[7], pfBls[8],
61                 pfBls[9], pfBls[10], pfBls[11], pfBls[12], pfBls[13], pfBls[14], pfBls[15]);
62     }
63
64     /**
65      * Test  method for {@link PortTranslatorUtil#translatePortFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10)}
66      */
67     @Test
68     public void testTranslatePortFeaturesV10() {
69
70
71         for (int i = 0; i < pfV10Bls.length; i++) {
72
73             pfV10Bls[i] = true;
74             final PortFeaturesV10 apfV10 = getPortFeaturesV10();
75             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf = PortTranslatorUtil.translatePortFeatures(apfV10);
76             assertEqualsPortFeaturesV10(apfV10, npf);
77             pfV10Bls[i] = false;
78
79         }
80
81     }
82
83     private PortFeaturesV10 getPortFeaturesV10() {
84         return new PortFeaturesV10(pfV10Bls[0], pfV10Bls[1], pfV10Bls[2], pfV10Bls[3], pfV10Bls[4], pfV10Bls[5], pfV10Bls[6],
85                 pfV10Bls[7], pfV10Bls[8], pfV10Bls[9], pfV10Bls[10], pfV10Bls[11]);
86     }
87
88     /**
89      * Test  method for {@link PortTranslatorUtil#translatePort(Short, java.math.BigInteger, Long, org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping)} ()}
90      */
91     @Test
92     public void testTranslatePort() {
93
94         Short version = OpenflowVersion.OF10.getVersion();
95
96         BigInteger dataPathId = BigInteger.ONE;
97         Long portNumber = Long.MAX_VALUE;
98         PortGrouping portGrouping = mockPortGrouping();
99
100         NodeConnectorUpdated nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
101         assertNotNull(nodeConnectorUpdated);
102         version = OpenflowVersion.OF13.getVersion();
103         nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
104         assertNotNull(nodeConnectorUpdated);
105
106         version = OpenflowVersion.UNSUPPORTED.getVersion();
107         nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
108         assertNotNull(nodeConnectorUpdated);
109
110
111     }
112
113     private PortGrouping mockPortGrouping() {
114         PortGrouping portGrouping = mock(PortGrouping.class);
115         when(portGrouping.getAdvertisedFeatures()).thenReturn(getPortFeatures());
116         when(portGrouping.getAdvertisedFeaturesV10()).thenReturn(getPortFeaturesV10());
117         when(portGrouping.getConfig()).thenReturn(getPortConfig());
118         when(portGrouping.getConfigV10()).thenReturn(getPortConfigV10());
119         when(portGrouping.getCurrentFeatures()).thenReturn(getPortFeatures());
120         when(portGrouping.getCurrentFeaturesV10()).thenReturn(getPortFeaturesV10());
121         when(portGrouping.getCurrSpeed()).thenReturn(currentSpeed);
122         when(portGrouping.getHwAddr()).thenReturn(getMacAddress());
123         when(portGrouping.getName()).thenReturn(NAME);
124         when(portGrouping.getMaxSpeed()).thenReturn(maxSpeed);
125         when(portGrouping.getPeerFeatures()).thenReturn(getPortFeatures());
126         when(portGrouping.getPeerFeaturesV10()).thenReturn(getPortFeaturesV10());
127         when(portGrouping.getPortNo()).thenReturn(Long.MAX_VALUE);
128         when(portGrouping.getState()).thenReturn(getPortState());
129         when(portGrouping.getSupportedFeatures()).thenReturn(getPortFeatures());
130         when(portGrouping.getSupportedFeaturesV10()).thenReturn(getPortFeaturesV10());
131         return portGrouping;
132     }
133
134     private PortState getPortState() {
135         PortState portState = new PortState(portStateBools[0], portStateBools[1], portStateBools[2]);
136         return portState;
137     }
138
139     private static MacAddress getMacAddress() {
140         return new MacAddress(MAC_ADDRESS);
141     }
142
143     private PortConfigV10 getPortConfigV10() {
144         return new PortConfigV10(portCfgV10bools[0], portCfgV10bools[1], portCfgV10bools[2], portCfgV10bools[3], portCfgV10bools[4], portCfgV10bools[5], portCfgV10bools[6]);
145     }
146
147     private PortConfig getPortConfig() {
148         return new PortConfig(portCfgBools[0], portCfgBools[1], portCfgBools[2], portCfgBools[3]);
149     }
150
151     private static void assertEqualsPortFeaturesV10(final PortFeaturesV10 apfV10, final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
152         assertEquals(apfV10.is_100mbFd(), npf.isHundredMbFd());
153         assertEquals(apfV10.is_100mbHd(), npf.isHundredMbHd());
154
155         assertEquals(apfV10.is_10gbFd(), npf.isTenGbFd());
156         assertEquals(apfV10.is_10mbFd(), npf.isTenMbFd());
157         assertEquals(apfV10.is_10mbHd(), npf.isTenMbHd());
158
159         assertEquals(apfV10.is_1gbFd(), npf.isOneGbFd());
160         assertEquals(apfV10.is_1gbHd(), npf.isOneGbHd());
161
162         assertEquals(apfV10.isAutoneg(), npf.isAutoeng());
163         assertEquals(apfV10.isCopper(), npf.isCopper());
164         assertEquals(apfV10.isFiber(), npf.isFiber());
165         assertEquals(apfV10.isPause(), npf.isPause());
166         assertEquals(apfV10.isPauseAsym(), npf.isPauseAsym());
167     }
168
169     private static void assertEqualsPortFeatures(final PortFeatures apf, final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
170         assertEquals(apf.is_100gbFd(), npf.isHundredGbFd());
171         assertEquals(apf.is_100mbFd(), npf.isHundredMbFd());
172         assertEquals(apf.is_100mbHd(), npf.isHundredMbHd());
173
174         assertEquals(apf.is_10gbFd(), npf.isTenGbFd());
175         assertEquals(apf.is_10mbFd(), npf.isTenMbFd());
176         assertEquals(apf.is_10mbHd(), npf.isTenMbHd());
177
178         assertEquals(apf.is_1gbFd(), npf.isOneGbFd());
179         assertEquals(apf.is_1gbHd(), npf.isOneGbHd());
180         assertEquals(apf.is_1tbFd(), npf.isOneTbFd());
181
182         assertEquals(apf.is_40gbFd(), npf.isFortyGbFd());
183
184         assertEquals(apf.isAutoneg(), npf.isAutoeng());
185         assertEquals(apf.isCopper(), npf.isCopper());
186         assertEquals(apf.isFiber(), npf.isFiber());
187         assertEquals(apf.isOther(), npf.isOther());
188         assertEquals(apf.isPause(), npf.isPause());
189         assertEquals(apf.isPauseAsym(), npf.isPauseAsym());
190     }
191
192 }