Merge changes from topic 'blueprint'
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / common / NodeConnectorTranslatorUtilTest.java
1 /**
2  * Copyright (c) 2015 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.impl.common;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.math.BigInteger;
16 import java.util.Arrays;
17 import java.util.List;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.openflowplugin.api.OFConstants;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * openflowplugin-impl
44  * org.opendaylight.openflowplugin.impl.common
45  *
46  * Test class for testing {@link org.opendaylight.openflowplugin.impl.common.NodeConnectorTranslatorUtil}
47  *
48  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
49  *
50  * Created: Mar 31, 2015
51  */
52 public class NodeConnectorTranslatorUtilTest {
53
54     private static final Logger LOG = LoggerFactory.getLogger(NodeConnectorTranslatorUtilTest.class);
55
56     private static final String MAC_ADDRESS = "00:01:02:03:04:05";
57     private static final String NAME = "PortTranslatorTest";
58     private final Boolean[] pfBls = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
59     private final boolean[] pfV10Bls = {false, false, false, false, false, false, false, false, false, false, false, false};
60     private final boolean[] portCfgBools = {false, false, false, false};
61     private final boolean[] portCfgV10bools = {false, false, false, false, false, false, false};
62     private final boolean[] portStateBools = {false, false, false, false};
63     private final Long currentSpeed = Long.decode("4294967295");
64     private static final Long maxSpeed = Long.decode("4294967295");
65
66     /**
67      * Test method for {@link NodeConnectorTranslatorUtil#translateNodeConnectorFromFeaturesReply(FeaturesReply)}.
68      */
69     @Test
70     public void testTranslateNodeConnectorFromFeaturesReply(){
71         final FeaturesReply reply = mock(FeaturesReply.class);
72         final BigInteger dataPathId = BigInteger.valueOf(25L);
73         final List<PhyPort> listPorts = Arrays.asList(mockPhyPortPort());
74         when(reply.getPhyPort()).thenReturn(listPorts);
75         when(reply.getDatapathId()).thenReturn(dataPathId);
76         when(reply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
77         final List<NodeConnector> nodeConnector = NodeConnectorTranslatorUtil.translateNodeConnectorFromFeaturesReply(reply);
78         Assert.assertNotNull(nodeConnector);
79         Assert.assertEquals(1, nodeConnector.size());
80     }
81
82     /**
83      * Test method for {@link NodeConnectorTranslatorUtil#translateNodeConnectorFromFeaturesReply(FeaturesReply)}.
84      * {@link IllegalArgumentException}
85      */
86     @Test(expected=IllegalArgumentException.class)
87     public void testTranslateNodeConnectorFromFeaturesReplyNullPorts(){
88         final FeaturesReply reply = mock(FeaturesReply.class);
89         when(reply.getPhyPort()).thenReturn(null);
90         NodeConnectorTranslatorUtil.translateNodeConnectorFromFeaturesReply(reply);
91     }
92
93     /**
94      * Test method for {@link NodeConnectorTranslatorUtil#translateNodeConnectorFromFeaturesReply(FeaturesReply)}.
95      * {@link IllegalArgumentException}
96      */
97     @Test(expected=IllegalArgumentException.class)
98     public void testTranslateNodeConnectorFromFeaturesReplyNullReplay(){
99         NodeConnectorTranslatorUtil.translateNodeConnectorFromFeaturesReply(null);
100     }
101
102     /**
103      * Test method for {@link NodeConnectorTranslatorUtil#translateFlowCapableNodeFromPhyPort(PhyPort, short)}.
104      */
105     @Test
106     public void testTranslateFlowCapableNodeFromPhyPortOF10(){
107         final PhyPort port = mockPhyPortPort();
108         final FlowCapableNodeConnector flowCapableNodeConnector = NodeConnectorTranslatorUtil
109                 .translateFlowCapableNodeFromPhyPort(port, OFConstants.OFP_VERSION_1_0);
110         Assert.assertNotNull(flowCapableNodeConnector);
111         Assert.assertEquals(port.getName(), flowCapableNodeConnector.getName());
112         Assert.assertEquals(port.getPortNo(), flowCapableNodeConnector.getPortNumber().getUint32());
113         Assert.assertEquals(port.getHwAddr().getValue(), flowCapableNodeConnector.getHardwareAddress().getValue());
114         Assert.assertEquals(port.getCurrSpeed(), flowCapableNodeConnector.getCurrentSpeed());
115         Assert.assertEquals(port.getMaxSpeed(), flowCapableNodeConnector.getMaximumSpeed());
116         assertEqualsStateV10(port.getStateV10(), flowCapableNodeConnector.getState());
117         assertEqualsPortFeaturesV10(port.getAdvertisedFeaturesV10(), flowCapableNodeConnector.getAdvertisedFeatures());
118         assertEqualsPortFeaturesV10(port.getCurrentFeaturesV10(), flowCapableNodeConnector.getCurrentFeature());
119         assertEqualsPortFeaturesV10(port.getPeerFeaturesV10(), flowCapableNodeConnector.getPeerFeatures());
120         assertEqualsPortFeaturesV10(port.getSupportedFeaturesV10(), flowCapableNodeConnector.getSupported());
121
122     }
123
124     /**
125      * Test method for {@link NodeConnectorTranslatorUtil#translateFlowCapableNodeFromPhyPort(PhyPort, short)}.
126      */
127     @Test
128     public void testTranslateFlowCapableNodeFromPhyPortOF13(){
129         final PhyPort port = mockPhyPortPort();
130         final FlowCapableNodeConnector flowCapableNodeConnector = NodeConnectorTranslatorUtil
131                 .translateFlowCapableNodeFromPhyPort(port, OFConstants.OFP_VERSION_1_3);
132         Assert.assertNotNull(flowCapableNodeConnector);
133         Assert.assertEquals(port.getName(), flowCapableNodeConnector.getName());
134         Assert.assertEquals(port.getPortNo(), flowCapableNodeConnector.getPortNumber().getUint32());
135         Assert.assertEquals(port.getHwAddr().getValue(), flowCapableNodeConnector.getHardwareAddress().getValue());
136         Assert.assertEquals(port.getCurrSpeed(), flowCapableNodeConnector.getCurrentSpeed());
137         Assert.assertEquals(port.getMaxSpeed(), flowCapableNodeConnector.getMaximumSpeed());
138         assertEqualsState(port.getState(), flowCapableNodeConnector.getState());
139         assertEqualsPortFeatures(port.getAdvertisedFeatures(), flowCapableNodeConnector.getAdvertisedFeatures());
140         assertEqualsPortFeatures(port.getCurrentFeatures(), flowCapableNodeConnector.getCurrentFeature());
141         assertEqualsPortFeatures(port.getPeerFeatures(), flowCapableNodeConnector.getPeerFeatures());
142         assertEqualsPortFeatures(port.getSupportedFeatures(), flowCapableNodeConnector.getSupported());
143     }
144
145     /**
146      * Here unsupported version is used
147      * Test method for {@link NodeConnectorTranslatorUtil#translateFlowCapableNodeFromPhyPort(PhyPort, short)}.
148      */
149     @Test
150     public void testTranslateFlowCapableNodeFromPhyPortOF12() {
151         final PhyPort port = mockPhyPortPort();
152         try {
153             final FlowCapableNodeConnector flowCapableNodeConnector = NodeConnectorTranslatorUtil
154                     .translateFlowCapableNodeFromPhyPort(port, (short) 0x03);
155             Assert.fail("port of version 0x03 (OF-1.2) should not be translated");
156         } catch (Exception e) {
157             LOG.debug("expected exception: {}", e.getMessage());
158             Assert.assertTrue(e instanceof IllegalArgumentException);
159         }
160
161     }
162
163     /**
164      * Test method for {@link NodeConnectorTranslatorUtil#makeNodeConnectorId(BigInteger, String, long)}.
165      */
166     @Test
167     public void testMakeNodeConnectorId(){
168         final BigInteger dataPathId = BigInteger.valueOf(25L);
169         final String logicalName = "testPort";
170         final long portNo = 45L;
171         final NodeConnectorId nodeConnectorId = NodeConnectorTranslatorUtil.makeNodeConnectorId(dataPathId, logicalName, portNo);
172         Assert.assertNotNull(nodeConnectorId);
173         Assert.assertNotNull(nodeConnectorId.getValue());
174         Assert.assertTrue(nodeConnectorId.getValue().contains(logicalName));
175         Assert.assertTrue(nodeConnectorId.getValue().contains(dataPathId.toString()));
176         Assert.assertFalse(nodeConnectorId.getValue().contains(":" + portNo));
177     }
178
179     /**
180      * Test method for {@link NodeConnectorTranslatorUtil#makeNodeConnectorId(BigInteger, String, long)}.
181      */
182     @Test
183     public void testMakeNodeConnectorIdNullLogicalName(){
184         final BigInteger dataPathId = BigInteger.valueOf(25L);
185         final long portNo = 45L;
186         final NodeConnectorId nodeConnectorId = NodeConnectorTranslatorUtil.makeNodeConnectorId(dataPathId, null, portNo);
187         Assert.assertNotNull(nodeConnectorId);
188         Assert.assertNotNull(nodeConnectorId.getValue());
189         Assert.assertTrue(nodeConnectorId.getValue().contains(dataPathId.toString()));
190         Assert.assertTrue(nodeConnectorId.getValue().contains(":" + portNo));
191     }
192
193     /**
194      * Test method for {@link NodeConnectorTranslatorUtil#makeNodeConnectorId(BigInteger, String, long)}.
195      * expect {@link IllegalArgumentException}
196      */
197     @Test(expected=IllegalArgumentException.class)
198     public void testMakeNodeConnectorIdNullDataPath(){
199         final long portNo = 45L;
200         NodeConnectorTranslatorUtil.makeNodeConnectorId(null, null, portNo);
201     }
202
203     private PhyPort mockPhyPortPort() {
204         final PhyPort phyport = mock(PhyPort.class);
205         when(phyport.getAdvertisedFeatures()).thenReturn(getPortFeatures());
206         when(phyport.getAdvertisedFeaturesV10()).thenReturn(getPortFeaturesV10());
207         when(phyport.getConfig()).thenReturn(getPortConfig());
208         when(phyport.getConfigV10()).thenReturn(getPortConfigV10());
209         when(phyport.getCurrentFeatures()).thenReturn(getPortFeatures());
210         when(phyport.getCurrentFeaturesV10()).thenReturn(getPortFeaturesV10());
211         when(phyport.getCurrSpeed()).thenReturn(currentSpeed);
212         when(phyport.getHwAddr()).thenReturn(getMacAddress());
213         when(phyport.getName()).thenReturn(NAME);
214         when(phyport.getMaxSpeed()).thenReturn(maxSpeed);
215         when(phyport.getPeerFeatures()).thenReturn(getPortFeatures());
216         when(phyport.getPeerFeaturesV10()).thenReturn(getPortFeaturesV10());
217         when(phyport.getPortNo()).thenReturn(Long.MAX_VALUE);
218         when(phyport.getState()).thenReturn(getPortState());
219         when(phyport.getStateV10()).thenReturn(getPortStateV10());
220         when(phyport.getSupportedFeatures()).thenReturn(getPortFeatures());
221         when(phyport.getSupportedFeaturesV10()).thenReturn(getPortFeaturesV10());
222         return phyport;
223     }
224
225     private static PortStateV10 getPortStateV10() {
226         final PortStateV10 portState = new PortStateV10(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE);
227         return portState;
228     }
229
230     private PortState getPortState() {
231         final PortState portState = new PortState(portStateBools[0], portStateBools[1], portStateBools[2]);
232         return portState;
233     }
234
235     private PortFeatures getPortFeatures() {
236         return new PortFeatures(pfBls[0], pfBls[1], pfBls[2], pfBls[3], pfBls[4], pfBls[5], pfBls[6], pfBls[7], pfBls[8],
237                 pfBls[9], pfBls[10], pfBls[11], pfBls[12], pfBls[13], pfBls[14], pfBls[15]);
238     }
239
240     private PortFeaturesV10 getPortFeaturesV10() {
241         return new PortFeaturesV10(pfV10Bls[0], pfV10Bls[1], pfV10Bls[2], pfV10Bls[3], pfV10Bls[4], pfV10Bls[5], pfV10Bls[6],
242                 pfV10Bls[7], pfV10Bls[8], pfV10Bls[9], pfV10Bls[10], pfV10Bls[11]);
243     }
244
245     private static MacAddress getMacAddress() {
246         return new MacAddress(MAC_ADDRESS);
247     }
248
249     private PortConfigV10 getPortConfigV10() {
250         return new PortConfigV10(portCfgV10bools[0], portCfgV10bools[1], portCfgV10bools[2], portCfgV10bools[3], portCfgV10bools[4], portCfgV10bools[5], portCfgV10bools[6]);
251     }
252
253     private PortConfig getPortConfig() {
254         return new PortConfig(portCfgBools[0], portCfgBools[1], portCfgBools[2], portCfgBools[3]);
255     }
256
257     private static void assertEqualsStateV10(final PortStateV10 psV10, final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState state) {
258         assertEquals(psV10.isBlocked(), state.isBlocked());
259         assertEquals(psV10.isLinkDown(), state.isLinkDown());
260         assertEquals(psV10.isLive(), state.isLive());
261     }
262
263     private static void assertEqualsState(final PortState ps, final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState state) {
264         assertEquals(ps.isBlocked(), state.isBlocked());
265         assertEquals(ps.isLinkDown(), state.isLinkDown());
266         assertEquals(ps.isLive(), state.isLive());
267     }
268
269     private static void assertEqualsPortFeaturesV10(final PortFeaturesV10 apfV10, final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
270         assertEquals(apfV10.is_100mbFd(), npf.isHundredMbFd());
271         assertEquals(apfV10.is_100mbHd(), npf.isHundredMbHd());
272
273         assertEquals(apfV10.is_10gbFd(), npf.isTenGbFd());
274         assertEquals(apfV10.is_10mbFd(), npf.isTenMbFd());
275         assertEquals(apfV10.is_10mbHd(), npf.isTenMbHd());
276
277         assertEquals(apfV10.is_1gbFd(), npf.isOneGbFd());
278         assertEquals(apfV10.is_1gbHd(), npf.isOneGbHd());
279
280         assertEquals(apfV10.isAutoneg(), npf.isAutoeng());
281         assertEquals(apfV10.isCopper(), npf.isCopper());
282         assertEquals(apfV10.isFiber(), npf.isFiber());
283         assertEquals(apfV10.isPause(), npf.isPause());
284         assertEquals(apfV10.isPauseAsym(), npf.isPauseAsym());
285     }
286
287     private static void assertEqualsPortFeatures(final PortFeatures apf, final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
288         assertEquals(apf.is_100gbFd(), npf.isHundredGbFd());
289         assertEquals(apf.is_100mbFd(), npf.isHundredMbFd());
290         assertEquals(apf.is_100mbHd(), npf.isHundredMbHd());
291
292         assertEquals(apf.is_10gbFd(), npf.isTenGbFd());
293         assertEquals(apf.is_10mbFd(), npf.isTenMbFd());
294         assertEquals(apf.is_10mbHd(), npf.isTenMbHd());
295
296         assertEquals(apf.is_1gbFd(), npf.isOneGbFd());
297         assertEquals(apf.is_1gbHd(), npf.isOneGbHd());
298         assertEquals(apf.is_1tbFd(), npf.isOneTbFd());
299
300         assertEquals(apf.is_40gbFd(), npf.isFortyGbFd());
301
302         assertEquals(apf.isAutoneg(), npf.isAutoeng());
303         assertEquals(apf.isCopper(), npf.isCopper());
304         assertEquals(apf.isFiber(), npf.isFiber());
305         assertEquals(apf.isOther(), npf.isOther());
306         assertEquals(apf.isPause(), npf.isPause());
307         assertEquals(apf.isPauseAsym(), npf.isPauseAsym());
308     }
309
310     static InstanceIdentifier<NodeConnector> createNodeConnectorId(String nodeKey, String nodeConnectorKey) {
311         return InstanceIdentifier.builder(Nodes.class)
312                 .child(Node.class, new NodeKey(new NodeId(nodeKey)))
313                 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(nodeConnectorKey)))
314                 .build();
315     }
316
317     @Test
318     public void testDummy() {
319         InstanceIdentifier<NodeConnector> id = createNodeConnectorId("openflow:1", "openflow:1:1");
320         InstanceIdentifier<Node> nodeId = id.firstIdentifierOf(Node.class);
321         System.out.println(nodeId);
322     }
323 }