Add missing license headers
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / 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.openflow.md.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 import java.math.BigInteger;
15 import org.junit.Before;
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     @Before
43     public void setupEnvironment(){
44         OpenflowPortsUtil.init();
45     }
46
47     /**
48      * Test  method for {@link PortTranslatorUtil#translatePortFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures)}
49      */
50     @Test
51     public void testTranslatePortFeatures() {
52
53
54         for (int i = 0; i < pfBls.length; i++) {
55             pfBls[i] = true;
56             final PortFeatures apf = getPortFeatures();
57             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf = PortTranslatorUtil.translatePortFeatures(apf);
58             assertEqualsPortFeatures(apf, npf);
59             pfBls[i] = false;
60         }
61
62     }
63
64     private PortFeatures getPortFeatures() {
65         return new PortFeatures(pfBls[0], pfBls[1], pfBls[2], pfBls[3], pfBls[4], pfBls[5], pfBls[6], pfBls[7], pfBls[8],
66                 pfBls[9], pfBls[10], pfBls[11], pfBls[12], pfBls[13], pfBls[14], pfBls[15]);
67     }
68
69     /**
70      * Test  method for {@link PortTranslatorUtil#translatePortFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10)}
71      */
72     @Test
73     public void testTranslatePortFeaturesV10() {
74
75
76         for (int i = 0; i < pfV10Bls.length; i++) {
77
78             pfV10Bls[i] = true;
79             final PortFeaturesV10 apfV10 = getPortFeaturesV10();
80             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf = PortTranslatorUtil.translatePortFeatures(apfV10);
81             assertEqualsPortFeaturesV10(apfV10, npf);
82             pfV10Bls[i] = false;
83
84         }
85
86     }
87
88     private PortFeaturesV10 getPortFeaturesV10() {
89         return new PortFeaturesV10(pfV10Bls[0], pfV10Bls[1], pfV10Bls[2], pfV10Bls[3], pfV10Bls[4], pfV10Bls[5], pfV10Bls[6],
90                 pfV10Bls[7], pfV10Bls[8], pfV10Bls[9], pfV10Bls[10], pfV10Bls[11]);
91     }
92
93     /**
94      * Test  method for {@link PortTranslatorUtil#translatePort(Short, java.math.BigInteger, Long, org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping)} ()}
95      */
96     @Test
97     public void testTranslatePort() {
98
99         Short version = OpenflowVersion.OF10.getVersion();
100
101         BigInteger dataPathId = BigInteger.ONE;
102         Long portNumber = Long.MAX_VALUE;
103         PortGrouping portGrouping = mockPortGrouping();
104
105         NodeConnectorUpdated nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
106         assertNotNull(nodeConnectorUpdated);
107         version = OpenflowVersion.OF13.getVersion();
108         nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
109         assertNotNull(nodeConnectorUpdated);
110
111         version = OpenflowVersion.UNSUPPORTED.getVersion();
112         nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
113         assertNotNull(nodeConnectorUpdated);
114
115
116     }
117
118     private PortGrouping mockPortGrouping() {
119         PortGrouping portGrouping = mock(PortGrouping.class);
120         when(portGrouping.getAdvertisedFeatures()).thenReturn(getPortFeatures());
121         when(portGrouping.getAdvertisedFeaturesV10()).thenReturn(getPortFeaturesV10());
122         when(portGrouping.getConfig()).thenReturn(getPortConfig());
123         when(portGrouping.getConfigV10()).thenReturn(getPortConfigV10());
124         when(portGrouping.getCurrentFeatures()).thenReturn(getPortFeatures());
125         when(portGrouping.getCurrentFeaturesV10()).thenReturn(getPortFeaturesV10());
126         when(portGrouping.getCurrSpeed()).thenReturn(currentSpeed);
127         when(portGrouping.getHwAddr()).thenReturn(getMacAddress());
128         when(portGrouping.getName()).thenReturn(NAME);
129         when(portGrouping.getMaxSpeed()).thenReturn(maxSpeed);
130         when(portGrouping.getPeerFeatures()).thenReturn(getPortFeatures());
131         when(portGrouping.getPeerFeaturesV10()).thenReturn(getPortFeaturesV10());
132         when(portGrouping.getPortNo()).thenReturn(Long.MAX_VALUE);
133         when(portGrouping.getState()).thenReturn(getPortState());
134         when(portGrouping.getSupportedFeatures()).thenReturn(getPortFeatures());
135         when(portGrouping.getSupportedFeaturesV10()).thenReturn(getPortFeaturesV10());
136         return portGrouping;
137     }
138
139     private PortState getPortState() {
140         PortState portState = new PortState(portStateBools[0], portStateBools[1], portStateBools[2]);
141         return portState;
142     }
143
144     private static MacAddress getMacAddress() {
145         return new MacAddress(MAC_ADDRESS);
146     }
147
148     private PortConfigV10 getPortConfigV10() {
149         return new PortConfigV10(portCfgV10bools[0], portCfgV10bools[1], portCfgV10bools[2], portCfgV10bools[3], portCfgV10bools[4], portCfgV10bools[5], portCfgV10bools[6]);
150     }
151
152     private PortConfig getPortConfig() {
153         return new PortConfig(portCfgBools[0], portCfgBools[1], portCfgBools[2], portCfgBools[3]);
154     }
155
156     private static void assertEqualsPortFeaturesV10(final PortFeaturesV10 apfV10, final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
157         assertEquals(apfV10.is_100mbFd(), npf.isHundredMbFd());
158         assertEquals(apfV10.is_100mbHd(), npf.isHundredMbHd());
159
160         assertEquals(apfV10.is_10gbFd(), npf.isTenGbFd());
161         assertEquals(apfV10.is_10mbFd(), npf.isTenMbFd());
162         assertEquals(apfV10.is_10mbHd(), npf.isTenMbHd());
163
164         assertEquals(apfV10.is_1gbFd(), npf.isOneGbFd());
165         assertEquals(apfV10.is_1gbHd(), npf.isOneGbHd());
166
167         assertEquals(apfV10.isAutoneg(), npf.isAutoeng());
168         assertEquals(apfV10.isCopper(), npf.isCopper());
169         assertEquals(apfV10.isFiber(), npf.isFiber());
170         assertEquals(apfV10.isPause(), npf.isPause());
171         assertEquals(apfV10.isPauseAsym(), npf.isPauseAsym());
172     }
173
174     private static void assertEqualsPortFeatures(final PortFeatures apf, final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
175         assertEquals(apf.is_100gbFd(), npf.isHundredGbFd());
176         assertEquals(apf.is_100mbFd(), npf.isHundredMbFd());
177         assertEquals(apf.is_100mbHd(), npf.isHundredMbHd());
178
179         assertEquals(apf.is_10gbFd(), npf.isTenGbFd());
180         assertEquals(apf.is_10mbFd(), npf.isTenMbFd());
181         assertEquals(apf.is_10mbHd(), npf.isTenMbHd());
182
183         assertEquals(apf.is_1gbFd(), npf.isOneGbFd());
184         assertEquals(apf.is_1gbHd(), npf.isOneGbHd());
185         assertEquals(apf.is_1tbFd(), npf.isOneTbFd());
186
187         assertEquals(apf.is_40gbFd(), npf.isFortyGbFd());
188
189         assertEquals(apf.isAutoneg(), npf.isAutoeng());
190         assertEquals(apf.isCopper(), npf.isCopper());
191         assertEquals(apf.isFiber(), npf.isFiber());
192         assertEquals(apf.isOther(), npf.isOther());
193         assertEquals(apf.isPause(), npf.isPause());
194         assertEquals(apf.isPauseAsym(), npf.isPauseAsym());
195     }
196
197 }