Bug 1254 - added unit tests for MultiPartReplyPortToNodeConnectorUpdatedTranslator
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / translator / MultiPartReplyPortToNodeConnectorUpdatedTranslatorTest.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowplugin.openflow.md.core.translator;\r
10 \r
11 import static org.mockito.Mockito.when;\r
12 \r
13 import java.math.BigInteger;\r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 import org.junit.Assert;\r
18 import org.junit.Before;\r
19 import org.junit.Test;\r
20 import org.mockito.Mock;\r
21 import org.mockito.MockitoAnnotations;\r
22 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
23 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;\r
24 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;\r
25 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;\r
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCaseBuilder;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDescBuilder;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.Ports;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.PortsBuilder;\r
44 import org.opendaylight.yangtools.yang.binding.DataObject;\r
45 \r
46 /**\r
47  * @author michal.polkorab\r
48  *\r
49  */\r
50 public class MultiPartReplyPortToNodeConnectorUpdatedTranslatorTest {\r
51 \r
52     @Mock SwitchConnectionDistinguisher cookie;\r
53     @Mock SessionContext sc;\r
54     @Mock GetFeaturesOutput features;\r
55 \r
56     MultiPartReplyPortToNodeConnectorUpdatedTranslator translator = new MultiPartReplyPortToNodeConnectorUpdatedTranslator();\r
57 \r
58     /**\r
59      * Initializes mocks\r
60      */\r
61     @Before\r
62     public void startUp() {\r
63         MockitoAnnotations.initMocks(this);\r
64         when(sc.getFeatures()).thenReturn(features);\r
65         when(features.getDatapathId()).thenReturn(new BigInteger("42"));\r
66         OpenflowPortsUtil.init();\r
67     }\r
68 \r
69     /**\r
70      * Test {@link MultiPartReplyPortToNodeConnectorUpdatedTranslator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
71      * with wrong inputs\r
72      */\r
73     @Test\r
74     public void testWrongInputs() {\r
75         HelloMessageBuilder helloBuilder = new HelloMessageBuilder();\r
76         HelloMessage helloMessage = helloBuilder.build();\r
77         List<DataObject> list = translator.translate(cookie, sc, helloMessage);\r
78         Assert.assertEquals("Wrong output", 0, list.size());\r
79         \r
80         MultipartReplyMessageBuilder builder = new MultipartReplyMessageBuilder();\r
81         builder.setType(MultipartType.OFPMPFLOW);\r
82         MultipartReplyMessage message = builder.build();\r
83         list = translator.translate(cookie, sc, message);\r
84         Assert.assertEquals("Wrong output", 0, list.size());\r
85     }\r
86 \r
87     /**\r
88      * Test {@link MultiPartReplyPortToNodeConnectorUpdatedTranslator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
89      * with correct input (no ports)\r
90      */\r
91     @Test\r
92     public void testEmptyPortDescWithCorrectInput() {\r
93         MultipartReplyMessageBuilder builder = new MultipartReplyMessageBuilder();\r
94         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);\r
95         builder.setXid(12345L);\r
96         builder.setType(MultipartType.OFPMPPORTDESC);\r
97         builder.setFlags(new MultipartRequestFlags(false));\r
98         \r
99         MultipartReplyPortDescCaseBuilder caseBuilder = new MultipartReplyPortDescCaseBuilder();\r
100         MultipartReplyPortDescBuilder descBuilder = new MultipartReplyPortDescBuilder();\r
101         List<Ports> ports = new ArrayList<>();\r
102         descBuilder.setPorts(ports);\r
103         caseBuilder.setMultipartReplyPortDesc(descBuilder.build());\r
104         builder.setMultipartReplyBody(caseBuilder.build());\r
105         MultipartReplyMessage message = builder.build();\r
106         \r
107         List<DataObject> list = translator.translate(cookie, sc, message);\r
108         Assert.assertEquals("Wrong output", 0, list.size());\r
109     }\r
110 \r
111     /**\r
112      * Test {@link MultiPartReplyPortToNodeConnectorUpdatedTranslator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
113      * with correct input\r
114      */\r
115     @Test\r
116     public void testPortDescWithCorrectInput() {\r
117         MultipartReplyMessageBuilder builder = new MultipartReplyMessageBuilder();\r
118         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);\r
119         builder.setXid(12345L);\r
120         builder.setType(MultipartType.OFPMPPORTDESC);\r
121         builder.setFlags(new MultipartRequestFlags(false));\r
122         \r
123         MultipartReplyPortDescCaseBuilder caseBuilder = new MultipartReplyPortDescCaseBuilder();\r
124         MultipartReplyPortDescBuilder descBuilder = new MultipartReplyPortDescBuilder();\r
125         List<Ports> ports = new ArrayList<>();\r
126         PortsBuilder portBuilder = new PortsBuilder();\r
127         portBuilder.setPortNo(5L);\r
128         portBuilder.setHwAddr(new MacAddress("06:07:08:09:0A:0B"));\r
129         portBuilder.setName("MultipartPortDesc port");\r
130         portBuilder.setConfig(new PortConfig(true, false, true, false));\r
131         portBuilder.setState(new PortState(true, false, true));\r
132         portBuilder.setCurrentFeatures(new PortFeatures(false, true, false, true,\r
133                 false, true, false, true, false, true, false, true, false, true, false, true));\r
134         portBuilder.setAdvertisedFeatures(new PortFeatures(true, false, true, false, true,\r
135                 false, true, false, true, false, true, false, true, false, true, false));\r
136         portBuilder.setSupportedFeatures(new PortFeatures(false, false, false, false, false,\r
137                 false, false, false, false, false, false, false, false, false, false, false));\r
138         portBuilder.setPeerFeatures(new PortFeatures(true, true, true, true, true,\r
139                 true, true, true, true, true, true, true, true, true, true, true));\r
140         portBuilder.setCurrSpeed(12L);\r
141         portBuilder.setMaxSpeed(13L);\r
142         ports.add(portBuilder.build());\r
143         portBuilder = new PortsBuilder();\r
144         portBuilder.setPortNo(50L);\r
145         portBuilder.setHwAddr(new MacAddress("05:06:07:08:09:0A"));\r
146         portBuilder.setName("MultipartPortDesc port");\r
147         portBuilder.setConfig(new PortConfig(false, true, false, true));\r
148         portBuilder.setState(new PortState(false, true, false));\r
149         portBuilder.setCurrentFeatures(null);\r
150         portBuilder.setAdvertisedFeatures(null);\r
151         portBuilder.setSupportedFeatures(null);\r
152         portBuilder.setPeerFeatures(null);\r
153         portBuilder.setCurrSpeed(120L);\r
154         portBuilder.setMaxSpeed(130L);\r
155         ports.add(portBuilder.build());\r
156         descBuilder.setPorts(ports);\r
157         caseBuilder.setMultipartReplyPortDesc(descBuilder.build());\r
158         builder.setMultipartReplyBody(caseBuilder.build());\r
159         MultipartReplyMessage message = builder.build();\r
160         \r
161         List<DataObject> list = translator.translate(cookie, sc, message);\r
162         Assert.assertEquals("Wrong output", 2, list.size());\r
163         NodeConnectorUpdated nodeUpdated = (NodeConnectorUpdated) list.get(0);\r
164         Assert.assertEquals("Wrong node-id", "openflow:42:5", nodeUpdated.getId().getValue());\r
165         FlowCapableNodeConnectorUpdated flowCapable = nodeUpdated.getAugmentation(FlowCapableNodeConnectorUpdated.class);\r
166         Assert.assertEquals("Wrong port number", 5, flowCapable.getPortNumber().getUint32().intValue());\r
167         Assert.assertEquals("Wrong hardware address", new MacAddress("06:07:08:09:0A:0B"), flowCapable.getHardwareAddress());\r
168         Assert.assertEquals("Wrong name", "MultipartPortDesc port", flowCapable.getName());\r
169         Assert.assertEquals("Wrong config", true, flowCapable.getConfiguration().isNOFWD());\r
170         Assert.assertEquals("Wrong config", false, flowCapable.getConfiguration().isNOPACKETIN());\r
171         Assert.assertEquals("Wrong config", true, flowCapable.getConfiguration().isNORECV());\r
172         Assert.assertEquals("Wrong config", false, flowCapable.getConfiguration().isPORTDOWN());\r
173         Assert.assertEquals("Wrong state", true, flowCapable.getState().isBlocked());\r
174         Assert.assertEquals("Wrong state", false, flowCapable.getState().isLinkDown());\r
175         Assert.assertEquals("Wrong state", true, flowCapable.getState().isLive());\r
176         Assert.assertEquals("Wrong current features", new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port\r
177                 .rev130925.PortFeatures(false, true, false, true, false, true, false, false, true, false, true, false,\r
178                         true, true, false, true), flowCapable.getCurrentFeature());\r
179         Assert.assertEquals("Wrong advertised features", new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port\r
180                 .rev130925.PortFeatures(true, false, true, false, true, false, true, true, false, true, false, true,\r
181                         false, false, true, false), flowCapable.getAdvertisedFeatures());\r
182         Assert.assertEquals("Wrong supported features", new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port\r
183                 .rev130925.PortFeatures(false, false, false, false, false, false, false, false, false, false, false, false,\r
184                         false, false, false, false), flowCapable.getSupported());\r
185         Assert.assertEquals("Wrong peer features", new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port\r
186                 .rev130925.PortFeatures(true, true, true, true, true, true, true, true, true, true, true, true,\r
187                         true, true, true, true), flowCapable.getPeerFeatures());\r
188         Assert.assertEquals("Wrong current speed", 12, flowCapable.getCurrentSpeed().intValue());\r
189         Assert.assertEquals("Wrong max speed", 13, flowCapable.getMaximumSpeed().intValue());\r
190         nodeUpdated = (NodeConnectorUpdated) list.get(1);\r
191         Assert.assertEquals("Wrong node-id", "openflow:42:50", nodeUpdated.getId().getValue());\r
192         flowCapable = nodeUpdated.getAugmentation(FlowCapableNodeConnectorUpdated.class);\r
193         Assert.assertEquals("Wrong port number", 50, flowCapable.getPortNumber().getUint32().intValue());\r
194         Assert.assertEquals("Wrong hardware address", new MacAddress("05:06:07:08:09:0A"), flowCapable.getHardwareAddress());\r
195         Assert.assertEquals("Wrong name", "MultipartPortDesc port", flowCapable.getName());\r
196         Assert.assertEquals("Wrong config", false, flowCapable.getConfiguration().isNOFWD());\r
197         Assert.assertEquals("Wrong config", true, flowCapable.getConfiguration().isNOPACKETIN());\r
198         Assert.assertEquals("Wrong config", false, flowCapable.getConfiguration().isNORECV());\r
199         Assert.assertEquals("Wrong config", true, flowCapable.getConfiguration().isPORTDOWN());\r
200         Assert.assertEquals("Wrong state", false, flowCapable.getState().isBlocked());\r
201         Assert.assertEquals("Wrong state", true, flowCapable.getState().isLinkDown());\r
202         Assert.assertEquals("Wrong state", false, flowCapable.getState().isLive());\r
203         Assert.assertEquals("Wrong current features", null, flowCapable.getCurrentFeature());\r
204         Assert.assertEquals("Wrong advertised features", null, flowCapable.getAdvertisedFeatures());\r
205         Assert.assertEquals("Wrong supported features", null, flowCapable.getSupported());\r
206         Assert.assertEquals("Wrong peer features", null, flowCapable.getPeerFeatures());\r
207         Assert.assertEquals("Wrong current speed", 120, flowCapable.getCurrentSpeed().intValue());\r
208         Assert.assertEquals("Wrong max speed", 130, flowCapable.getMaximumSpeed().intValue());\r
209     }\r
210 }