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