Update MRI upstreams for Phosphorus
[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 org.junit.Test;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
25 import org.opendaylight.yangtools.yang.common.Uint32;
26 import org.opendaylight.yangtools.yang.common.Uint64;
27 import org.opendaylight.yangtools.yang.common.Uint8;
28
29 /**
30  * Created by Martin Bobak mbobak@cisco.com on 7/29/14.
31  */
32 public class PortTranslatorUtilTest {
33     private static final String MAC_ADDRESS = "00:01:02:03:04:05";
34     private static final Uint32 MAX_SPEED = Uint32.valueOf(4294967295L);
35     private static final String NAME = "PortTranslatorTest";
36
37     private final Boolean[] pfBls = {
38         false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
39     };
40
41     private final boolean[] pfV10Bls = {
42         false, false, false, false, false, false, false, false, false, false, false, false
43     };
44
45     private final boolean[] portCfgBools = { false, false, false, false };
46     private final boolean[] portCfgV10bools = { false, false, false, false, false, false, false };
47     private final boolean[] portStateBools = { false, false, false, false };
48     private final Uint32 currentSpeed = Uint32.MAX_VALUE;
49
50     /**
51      * Test  method for
52      * {@link PortTranslatorUtil#translatePortFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight
53      * .openflow.common.types.rev130731.PortFeatures)}.
54      */
55     @Test
56     public void testTranslatePortFeatures() {
57         for (int i = 0; i < pfBls.length; i++) {
58             pfBls[i] = true;
59             final PortFeatures apf = getPortFeatures();
60             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf
61                     = PortTranslatorUtil.translatePortFeatures(apf);
62             assertEqualsPortFeatures(apf, npf);
63             pfBls[i] = false;
64         }
65     }
66
67     private PortFeatures getPortFeatures() {
68         return new PortFeatures(pfBls[0], pfBls[1], pfBls[2], pfBls[3], pfBls[4], pfBls[5], pfBls[6], pfBls[7],
69                                 pfBls[8], pfBls[9], pfBls[10], pfBls[11], pfBls[12], pfBls[13], pfBls[14], pfBls[15]);
70     }
71
72     /**
73      * Test  method for
74      * {@link PortTranslatorUtil#translatePortFeatures(org.opendaylight.yang.gen.v1.urn
75      * .opendaylight.openflow.common.types.rev130731.PortFeaturesV10)}.
76      */
77     @Test
78     public void testTranslatePortFeaturesV10() {
79         for (int i = 0; i < pfV10Bls.length; i++) {
80             pfV10Bls[i] = true;
81             final PortFeaturesV10 apfV10 = getPortFeaturesV10();
82             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf
83                     = PortTranslatorUtil.translatePortFeatures(apfV10);
84             assertEqualsPortFeaturesV10(apfV10, npf);
85             pfV10Bls[i] = false;
86         }
87     }
88
89     private PortFeaturesV10 getPortFeaturesV10() {
90         return new PortFeaturesV10(pfV10Bls[0], pfV10Bls[1], pfV10Bls[2], pfV10Bls[3], pfV10Bls[4], pfV10Bls[5],
91                                    pfV10Bls[6], pfV10Bls[7], pfV10Bls[8], pfV10Bls[9], pfV10Bls[10], pfV10Bls[11]);
92     }
93
94     /**
95      * Test  method for
96      * {@link PortTranslatorUtil#translatePort(Short, java.math.BigInteger,
97      * Long, org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping)} ()}.
98      */
99     @Test
100     public void testTranslatePort() {
101
102         Uint8 version = OpenflowVersion.OF10.getVersion();
103
104         Uint64 dataPathId = Uint64.ONE;
105         Uint32 portNumber = Uint32.MAX_VALUE;
106         PortGrouping portGrouping = mockPortGrouping();
107
108         NodeConnectorUpdated nodeConnectorUpdated = PortTranslatorUtil
109                 .translatePort(version, dataPathId, portNumber, portGrouping);
110         assertNotNull(nodeConnectorUpdated);
111         version = OpenflowVersion.OF13.getVersion();
112         nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
113         assertNotNull(nodeConnectorUpdated);
114
115         version = OpenflowVersion.UNSUPPORTED.getVersion();
116         nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
117         assertNotNull(nodeConnectorUpdated);
118
119
120     }
121
122     private PortGrouping mockPortGrouping() {
123         PortGrouping portGrouping = mock(PortGrouping.class);
124         when(portGrouping.getAdvertisedFeatures()).thenReturn(getPortFeatures());
125         when(portGrouping.getAdvertisedFeaturesV10()).thenReturn(getPortFeaturesV10());
126         when(portGrouping.getConfig()).thenReturn(getPortConfig());
127         when(portGrouping.getConfigV10()).thenReturn(getPortConfigV10());
128         when(portGrouping.getCurrentFeatures()).thenReturn(getPortFeatures());
129         when(portGrouping.getCurrentFeaturesV10()).thenReturn(getPortFeaturesV10());
130         when(portGrouping.getCurrSpeed()).thenReturn(currentSpeed);
131         when(portGrouping.getHwAddr()).thenReturn(getMacAddress());
132         when(portGrouping.getName()).thenReturn(NAME);
133         when(portGrouping.getMaxSpeed()).thenReturn(MAX_SPEED);
134         when(portGrouping.getPeerFeatures()).thenReturn(getPortFeatures());
135         when(portGrouping.getPeerFeaturesV10()).thenReturn(getPortFeaturesV10());
136         when(portGrouping.getPortNo()).thenReturn(Uint32.MAX_VALUE);
137         when(portGrouping.getState()).thenReturn(getPortState());
138         when(portGrouping.getSupportedFeatures()).thenReturn(getPortFeatures());
139         when(portGrouping.getSupportedFeaturesV10()).thenReturn(getPortFeaturesV10());
140         return portGrouping;
141     }
142
143     private PortState getPortState() {
144         PortState portState = new PortState(portStateBools[0], portStateBools[1], portStateBools[2]);
145         return portState;
146     }
147
148     private static MacAddress getMacAddress() {
149         return new MacAddress(MAC_ADDRESS);
150     }
151
152     private PortConfigV10 getPortConfigV10() {
153         return new PortConfigV10(portCfgV10bools[0], portCfgV10bools[1], portCfgV10bools[2], portCfgV10bools[3],
154                                  portCfgV10bools[4], portCfgV10bools[5], portCfgV10bools[6]);
155     }
156
157     private PortConfig getPortConfig() {
158         return new PortConfig(portCfgBools[0], portCfgBools[1], portCfgBools[2], portCfgBools[3]);
159     }
160
161     private static void assertEqualsPortFeaturesV10(final PortFeaturesV10 apfV10,
162             final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
163         assertEquals(apfV10.get_100mbFd(), npf.getHundredMbFd());
164         assertEquals(apfV10.get_100mbHd(), npf.getHundredMbHd());
165
166         assertEquals(apfV10.get_10gbFd(), npf.getTenGbFd());
167         assertEquals(apfV10.get_10mbFd(), npf.getTenMbFd());
168         assertEquals(apfV10.get_10mbHd(), npf.getTenMbHd());
169
170         assertEquals(apfV10.get_1gbFd(), npf.getOneGbFd());
171         assertEquals(apfV10.get_1gbHd(), npf.getOneGbHd());
172
173         assertEquals(apfV10.getAutoneg(), npf.getAutoeng());
174         assertEquals(apfV10.getCopper(), npf.getCopper());
175         assertEquals(apfV10.getFiber(), npf.getFiber());
176         assertEquals(apfV10.getPause(), npf.getPause());
177         assertEquals(apfV10.getPauseAsym(), npf.getPauseAsym());
178     }
179
180     private static void assertEqualsPortFeatures(final PortFeatures apf,
181             final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
182         assertEquals(apf.get_100gbFd(), npf.getHundredGbFd());
183         assertEquals(apf.get_100mbFd(), npf.getHundredMbFd());
184         assertEquals(apf.get_100mbHd(), npf.getHundredMbHd());
185
186         assertEquals(apf.get_10gbFd(), npf.getTenGbFd());
187         assertEquals(apf.get_10mbFd(), npf.getTenMbFd());
188         assertEquals(apf.get_10mbHd(), npf.getTenMbHd());
189
190         assertEquals(apf.get_1gbFd(), npf.getOneGbFd());
191         assertEquals(apf.get_1gbHd(), npf.getOneGbHd());
192         assertEquals(apf.get_1tbFd(), npf.getOneTbFd());
193
194         assertEquals(apf.get_40gbFd(), npf.getFortyGbFd());
195
196         assertEquals(apf.getAutoneg(), npf.getAutoeng());
197         assertEquals(apf.getCopper(), npf.getCopper());
198         assertEquals(apf.getFiber(), npf.getFiber());
199         assertEquals(apf.getOther(), npf.getOther());
200         assertEquals(apf.getPause(), npf.getPause());
201         assertEquals(apf.getPauseAsym(), npf.getPauseAsym());
202     }
203 }