Bug 1254 - Added unit tests
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / translator / MultipartReplyTranslatorTest.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.ConnectionConductor;\r
25 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.MatchBuilder;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCaseBuilder;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlowBuilder;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;\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 MultipartReplyTranslatorTest {\r
51 \r
52     @Mock SwitchConnectionDistinguisher cookie;\r
53     @Mock SessionContext sc;\r
54     @Mock ConnectionConductor conductor;\r
55     @Mock GetFeaturesOutput features;\r
56 \r
57     MultipartReplyTranslator translator = new MultipartReplyTranslator();\r
58 \r
59     /**\r
60      * Initializes mocks\r
61      */\r
62     @Before\r
63     public void startUp() {\r
64         MockitoAnnotations.initMocks(this);\r
65         when(sc.getPrimaryConductor()).thenReturn(conductor);\r
66         when(conductor.getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);\r
67         when(sc.getFeatures()).thenReturn(features);\r
68         when(features.getDatapathId()).thenReturn(new BigInteger("42"));\r
69     }\r
70 \r
71     /**\r
72      * Test with null and incorrect message\r
73      */\r
74     @Test\r
75     public void test() {\r
76         List<DataObject> list = translator.translate(cookie, sc, null);\r
77 \r
78         Assert.assertEquals("Wrong list size", 0, list.size());\r
79         HelloMessageBuilder helloBuilder = new HelloMessageBuilder();\r
80         list = translator.translate(cookie, sc, helloBuilder.build());\r
81         Assert.assertEquals("Wrong list size", 0, list.size());\r
82     }\r
83 \r
84     /**\r
85      * Test with experimenter MultipartReply message\r
86      */\r
87     @Test\r
88     public void testExperimenterCase() {\r
89         MultipartReplyMessageBuilder mpBuilder = new MultipartReplyMessageBuilder();\r
90         mpBuilder.setVersion((short) EncodeConstants.OF13_VERSION_ID);\r
91         mpBuilder.setXid(123L);\r
92         mpBuilder.setFlags(new MultipartRequestFlags(false));\r
93         mpBuilder.setType(MultipartType.OFPMPEXPERIMENTER);\r
94         MultipartReplyMessage message = mpBuilder.build();\r
95 \r
96         List<DataObject> list = translator.translate(cookie, sc, message);\r
97 \r
98         Assert.assertEquals("Wrong list size", 0, list.size());\r
99     }\r
100 \r
101     /**\r
102      * Test  MultipartReply message with empty flow stats\r
103      */\r
104     @Test\r
105     public void testEmptyFlowCase() {\r
106         MultipartReplyMessageBuilder mpBuilder = new MultipartReplyMessageBuilder();\r
107         mpBuilder.setVersion((short) EncodeConstants.OF13_VERSION_ID);\r
108         mpBuilder.setXid(123L);\r
109         mpBuilder.setFlags(new MultipartRequestFlags(false));\r
110         mpBuilder.setType(MultipartType.OFPMPFLOW);\r
111         \r
112         MultipartReplyFlowCaseBuilder caseBuilder = new MultipartReplyFlowCaseBuilder();\r
113         MultipartReplyFlowBuilder flowBuilder = new MultipartReplyFlowBuilder();\r
114         List<FlowStats> flowStats = new ArrayList<>();\r
115         flowBuilder.setFlowStats(flowStats);\r
116         caseBuilder.setMultipartReplyFlow(flowBuilder.build());\r
117         mpBuilder.setMultipartReplyBody(caseBuilder.build());\r
118         MultipartReplyMessage message = mpBuilder.build();\r
119 \r
120         List<DataObject> list = translator.translate(cookie, sc, message);\r
121 \r
122         Assert.assertEquals("Wrong list size", 1, list.size());\r
123         FlowsStatisticsUpdate flowUpdate = (FlowsStatisticsUpdate) list.get(0);\r
124         Assert.assertEquals("Wrong node-id", "openflow:42", flowUpdate.getId().getValue());\r
125         Assert.assertEquals("Wrong more-replies", false, flowUpdate.isMoreReplies());\r
126         Assert.assertEquals("Wrong transaction-id", 123, flowUpdate.getTransactionId().getValue().intValue());\r
127         List<FlowAndStatisticsMapList> mapList = flowUpdate.getFlowAndStatisticsMapList();\r
128         Assert.assertEquals("Wrong flow stats size", 0, mapList.size());\r
129     }\r
130 \r
131     /**\r
132      * Test with experimenter MultipartReply message\r
133      */\r
134     @Test\r
135     public void testFlowCase() {\r
136         MultipartReplyMessageBuilder mpBuilder = new MultipartReplyMessageBuilder();\r
137         mpBuilder.setVersion((short) EncodeConstants.OF13_VERSION_ID);\r
138         mpBuilder.setXid(123L);\r
139         mpBuilder.setFlags(new MultipartRequestFlags(false));\r
140         mpBuilder.setType(MultipartType.OFPMPFLOW);\r
141         \r
142         MultipartReplyFlowCaseBuilder caseBuilder = new MultipartReplyFlowCaseBuilder();\r
143         MultipartReplyFlowBuilder flowBuilder = new MultipartReplyFlowBuilder();\r
144         List<FlowStats> flowStats = new ArrayList<>();\r
145         FlowStatsBuilder statsBuilder = new FlowStatsBuilder();\r
146         statsBuilder.setTableId((short) 1);\r
147         statsBuilder.setDurationSec(2L);\r
148         statsBuilder.setDurationNsec(3L);\r
149         statsBuilder.setPriority(4);\r
150         statsBuilder.setIdleTimeout(5);\r
151         statsBuilder.setHardTimeout(6);\r
152         FlowModFlags flags = new FlowModFlags(true, false, true, false, true);\r
153         statsBuilder.setFlags(flags);\r
154         statsBuilder.setCookie(new BigInteger("7"));\r
155         statsBuilder.setPacketCount(new BigInteger("8"));\r
156         statsBuilder.setByteCount(new BigInteger("9"));\r
157         MatchBuilder matchBuilder = new MatchBuilder();\r
158         matchBuilder.setType(OxmMatchType.class);\r
159         matchBuilder.setMatchEntries(new ArrayList<MatchEntries>());\r
160         statsBuilder.setMatch(matchBuilder.build());\r
161         statsBuilder.setInstruction(new ArrayList<Instruction>());\r
162         flowStats.add(statsBuilder.build());\r
163         statsBuilder = new FlowStatsBuilder();\r
164         statsBuilder.setTableId((short) 10);\r
165         statsBuilder.setDurationSec(20L);\r
166         statsBuilder.setDurationNsec(30L);\r
167         statsBuilder.setPriority(40);\r
168         statsBuilder.setIdleTimeout(50);\r
169         statsBuilder.setHardTimeout(60);\r
170         flags = new FlowModFlags(false, true, false, true, false);\r
171         statsBuilder.setFlags(flags);\r
172         statsBuilder.setCookie(new BigInteger("70"));\r
173         statsBuilder.setPacketCount(new BigInteger("80"));\r
174         statsBuilder.setByteCount(new BigInteger("90"));\r
175         matchBuilder = new MatchBuilder();\r
176         matchBuilder.setType(OxmMatchType.class);\r
177         matchBuilder.setMatchEntries(new ArrayList<MatchEntries>());\r
178         statsBuilder.setMatch(matchBuilder.build());\r
179         statsBuilder.setInstruction(new ArrayList<Instruction>());\r
180         flowStats.add(statsBuilder.build());\r
181         flowBuilder.setFlowStats(flowStats);\r
182         caseBuilder.setMultipartReplyFlow(flowBuilder.build());\r
183         mpBuilder.setMultipartReplyBody(caseBuilder.build());\r
184         MultipartReplyMessage message = mpBuilder.build();\r
185 \r
186         List<DataObject> list = translator.translate(cookie, sc, message);\r
187 \r
188         Assert.assertEquals("Wrong list size", 1, list.size());\r
189         FlowsStatisticsUpdate flowUpdate = (FlowsStatisticsUpdate) list.get(0);\r
190         Assert.assertEquals("Wrong node-id", "openflow:42", flowUpdate.getId().getValue());\r
191         Assert.assertEquals("Wrong more-replies", false, flowUpdate.isMoreReplies());\r
192         Assert.assertEquals("Wrong transaction-id", 123, flowUpdate.getTransactionId().getValue().intValue());\r
193         List<FlowAndStatisticsMapList> mapList = flowUpdate.getFlowAndStatisticsMapList();\r
194         Assert.assertEquals("Wrong flow stats size", 2, mapList.size());\r
195         FlowAndStatisticsMapList stat = mapList.get(0);\r
196         Assert.assertEquals("Wrong table-id", 1, stat.getTableId().intValue());\r
197         Assert.assertEquals("Wrong duration sec", 2, stat.getDuration().getSecond().getValue().intValue());\r
198         Assert.assertEquals("Wrong duration n sec", 3, stat.getDuration().getNanosecond().getValue().intValue());\r
199         Assert.assertEquals("Wrong priority", 4, stat.getPriority().intValue());\r
200         Assert.assertEquals("Wrong idle-timeout", 5, stat.getIdleTimeout().intValue());\r
201         Assert.assertEquals("Wrong hard-timeout", 6, stat.getHardTimeout().intValue());\r
202         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags expectedFlags = \r
203                 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026\r
204                 .FlowModFlags(!flags.isOFPFFCHECKOVERLAP(), !flags.isOFPFFNOBYTCOUNTS(), !flags.isOFPFFNOPKTCOUNTS(),\r
205                         !flags.isOFPFFRESETCOUNTS(), !flags.isOFPFFSENDFLOWREM());\r
206         Assert.assertEquals("Wrong flags", expectedFlags, stat.getFlags());\r
207         Assert.assertEquals("Wrong cookie", 7, stat.getCookie().getValue().intValue());\r
208         Assert.assertEquals("Wrong packet count", 8, stat.getPacketCount().getValue().intValue());\r
209         Assert.assertEquals("Wrong byte count", 9, stat.getByteCount().getValue().intValue());\r
210         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder emptyMatchBuilder = \r
211                 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder();\r
212         Match emptyMatch = emptyMatchBuilder.build();\r
213         Assert.assertEquals("Wrong match", emptyMatch, stat.getMatch());\r
214         Assert.assertEquals("Wrong instructions", 0, stat.getInstructions().getInstruction().size());\r
215         stat = mapList.get(1);\r
216         Assert.assertEquals("Wrong table-id", 10, stat.getTableId().intValue());\r
217         Assert.assertEquals("Wrong duration sec", 20, stat.getDuration().getSecond().getValue().intValue());\r
218         Assert.assertEquals("Wrong duration n sec", 30, stat.getDuration().getNanosecond().getValue().intValue());\r
219         Assert.assertEquals("Wrong priority", 40, stat.getPriority().intValue());\r
220         Assert.assertEquals("Wrong idle-timeout", 50, stat.getIdleTimeout().intValue());\r
221         Assert.assertEquals("Wrong hard-timeout", 60, stat.getHardTimeout().intValue());\r
222         expectedFlags = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026\r
223                 .FlowModFlags(flags.isOFPFFCHECKOVERLAP(), flags.isOFPFFNOBYTCOUNTS(), flags.isOFPFFNOPKTCOUNTS(),\r
224                         flags.isOFPFFRESETCOUNTS(), flags.isOFPFFSENDFLOWREM());\r
225         Assert.assertEquals("Wrong flags", expectedFlags, stat.getFlags());\r
226         Assert.assertEquals("Wrong cookie", 70, stat.getCookie().getValue().intValue());\r
227         Assert.assertEquals("Wrong packet count", 80, stat.getPacketCount().getValue().intValue());\r
228         Assert.assertEquals("Wrong byte count", 90, stat.getByteCount().getValue().intValue());\r
229         Assert.assertEquals("Wrong match", emptyMatch, stat.getMatch());\r
230         Assert.assertEquals("Wrong instructions", 0, stat.getInstructions().getInstruction().size());\r
231     }\r
232 }