Merge "BUG-2091: notification NodeRemoved and processing queue"
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / translator / MultipartReplyTableFeaturesToTableUpdatedTranslatorTest.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.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NextTableRelatedTableFeatureProperty;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NextTableRelatedTableFeaturePropertyBuilder;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.table.features.properties.container.table.feature.properties.NextTableIds;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.table.features.properties.container.table.feature.properties.NextTableIdsBuilder;\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.TableConfig;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableFeaturesPropType;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCaseBuilder;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeaturesBuilder;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.multipart.reply.table.features.TableFeatures;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.multipart.reply.table.features.TableFeaturesBuilder;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties;\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeaturePropertiesBuilder;\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;\r
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.NextTable;\r
47 import org.opendaylight.yangtools.yang.binding.DataObject;\r
48 \r
49 /**\r
50  * @author michal.polkorab\r
51  *\r
52  */\r
53 public class MultipartReplyTableFeaturesToTableUpdatedTranslatorTest {\r
54 \r
55     @Mock SwitchConnectionDistinguisher cookie;\r
56     @Mock SessionContext sc;\r
57     @Mock GetFeaturesOutput features;\r
58 \r
59     MultipartReplyTableFeaturesToTableUpdatedTranslator translator = new MultipartReplyTableFeaturesToTableUpdatedTranslator();\r
60 \r
61     /**\r
62      * Initializes mocks\r
63      */\r
64     @Before\r
65     public void startUp() {\r
66         MockitoAnnotations.initMocks(this);\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 {@link MultipartReplyTableFeaturesToTableUpdatedTranslator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
73      * with wrong inputs\r
74      */\r
75     @Test\r
76     public void testWrongInputs() {\r
77         HelloMessageBuilder helloBuilder = new HelloMessageBuilder();\r
78         HelloMessage helloMessage = helloBuilder.build();\r
79         List<DataObject> list = translator.translate(cookie, sc, helloMessage);\r
80         Assert.assertEquals("Wrong output", 0, list.size());\r
81         \r
82         MultipartReplyMessageBuilder builder = new MultipartReplyMessageBuilder();\r
83         builder.setType(MultipartType.OFPMPFLOW);\r
84         MultipartReplyMessage message = builder.build();\r
85         list = translator.translate(cookie, sc, message);\r
86         Assert.assertEquals("Wrong output", 0, list.size());\r
87     }\r
88 \r
89     /**\r
90      * Test {@link MultipartReplyTableFeaturesToTableUpdatedTranslator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
91      * with correct inputs (no table features)\r
92      */\r
93     @Test\r
94     public void testEmptyTableFeaturesWithCorrectInput() {\r
95         MultipartReplyMessageBuilder builder = new MultipartReplyMessageBuilder();\r
96         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);\r
97         builder.setXid(12345L);\r
98         builder.setType(MultipartType.OFPMPTABLEFEATURES);\r
99         builder.setFlags(new MultipartRequestFlags(false));\r
100         \r
101         MultipartReplyTableFeaturesCaseBuilder caseBuilder = new MultipartReplyTableFeaturesCaseBuilder();\r
102         MultipartReplyTableFeaturesBuilder featuresBuilder = new MultipartReplyTableFeaturesBuilder();\r
103         List<TableFeatures> features = new ArrayList<>();\r
104         featuresBuilder.setTableFeatures(features);\r
105         caseBuilder.setMultipartReplyTableFeatures(featuresBuilder.build());\r
106         builder.setMultipartReplyBody(caseBuilder.build());\r
107         MultipartReplyMessage message = builder.build();\r
108         \r
109         List<DataObject> list = translator.translate(cookie, sc, message);\r
110         Assert.assertEquals("Wrong output", 1, list.size());\r
111         TableUpdated tableUpdated = (TableUpdated) list.get(0);\r
112         Assert.assertEquals("Wrong table features size", 0, tableUpdated.getTableFeatures().size());\r
113     }\r
114 \r
115     /**\r
116      * Test {@link MultipartReplyTableFeaturesToTableUpdatedTranslator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
117      * with correct inputs\r
118      */\r
119     @Test\r
120     public void testTableFeaturesWithCorrectInput() {\r
121         MultipartReplyMessageBuilder builder = new MultipartReplyMessageBuilder();\r
122         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);\r
123         builder.setXid(12345L);\r
124         builder.setType(MultipartType.OFPMPTABLEFEATURES);\r
125         builder.setFlags(new MultipartRequestFlags(false));\r
126         \r
127         MultipartReplyTableFeaturesCaseBuilder caseBuilder = new MultipartReplyTableFeaturesCaseBuilder();\r
128         MultipartReplyTableFeaturesBuilder featuresBuilder = new MultipartReplyTableFeaturesBuilder();\r
129         List<TableFeatures> features = new ArrayList<>();\r
130         \r
131         TableFeaturesBuilder tableFeatBuilder = new TableFeaturesBuilder();\r
132         tableFeatBuilder.setTableId((short) 2);\r
133         tableFeatBuilder.setName("Fastest table in the world");\r
134         byte[] metadataMatch = new byte[]{0, 1, 2, 3, 4, 5, 6, 7};\r
135         tableFeatBuilder.setMetadataMatch(metadataMatch);\r
136         byte[] metadataWrite = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};\r
137         tableFeatBuilder.setMetadataWrite(metadataWrite);\r
138         tableFeatBuilder.setConfig(new TableConfig(false));\r
139         tableFeatBuilder.setMaxEntries(10L);\r
140         List<TableFeatureProperties> properties = new ArrayList<>();\r
141         TableFeaturePropertiesBuilder propBuilder = new TableFeaturePropertiesBuilder();\r
142         propBuilder.setType(TableFeaturesPropType.OFPTFPTNEXTTABLES);\r
143         NextTableRelatedTableFeaturePropertyBuilder tableBuilder = new NextTableRelatedTableFeaturePropertyBuilder();\r
144         List<NextTableIds> tableIds = new ArrayList<>();\r
145         NextTableIdsBuilder nextTableIdsBuilder = new NextTableIdsBuilder();\r
146         nextTableIdsBuilder.setTableId((short) 9);\r
147         tableIds.add(nextTableIdsBuilder.build());\r
148         nextTableIdsBuilder = new NextTableIdsBuilder();\r
149         nextTableIdsBuilder.setTableId((short) 10);\r
150         tableIds.add(nextTableIdsBuilder.build());\r
151         nextTableIdsBuilder = new NextTableIdsBuilder();\r
152         nextTableIdsBuilder.setTableId((short) 11);\r
153         tableIds.add(nextTableIdsBuilder.build());\r
154         tableBuilder.setNextTableIds(tableIds);\r
155         propBuilder.addAugmentation(NextTableRelatedTableFeatureProperty.class, tableBuilder.build());\r
156         properties.add(propBuilder.build());\r
157         tableFeatBuilder.setTableFeatureProperties(properties);\r
158         features.add(tableFeatBuilder.build());\r
159         featuresBuilder.setTableFeatures(features);\r
160         caseBuilder.setMultipartReplyTableFeatures(featuresBuilder.build());\r
161         builder.setMultipartReplyBody(caseBuilder.build());\r
162         MultipartReplyMessage message = builder.build();\r
163         \r
164         List<DataObject> list = translator.translate(cookie, sc, message);\r
165         Assert.assertEquals("Wrong output", 1, list.size());\r
166         TableUpdated tableUpdated = (TableUpdated) list.get(0);\r
167         Assert.assertEquals("Wrong table features size", 1, tableUpdated.getTableFeatures().size());\r
168         org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features\r
169         .TableFeatures feature = tableUpdated.getTableFeatures().get(0);\r
170         Assert.assertEquals("Wrong table-id", 2, feature.getTableId().intValue());\r
171         Assert.assertEquals("Wrong table name", "Fastest table in the world", feature.getName());\r
172         Assert.assertEquals("Wrong metadata match", new BigInteger(metadataMatch), feature.getMetadataMatch());\r
173         Assert.assertEquals("Wrong metadata write", new BigInteger(metadataWrite), feature.getMetadataWrite());\r
174         Assert.assertEquals("Wrong config", false, feature.getConfig().isDEPRECATEDMASK());\r
175         Assert.assertEquals("Wrong max entries", 10, feature.getMaxEntries().intValue());\r
176         Assert.assertEquals("Wrong properties size", 1, feature.getTableProperties().getTableFeatureProperties().size());\r
177         org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties\r
178         .TableFeatureProperties property = feature.getTableProperties().getTableFeatureProperties().get(0);\r
179         Assert.assertEquals("Wrong property type", "org.opendaylight.yang.gen.v1.urn.opendaylight.table.types"\r
180                 + ".rev131026.table.feature.prop.type.table.feature.prop.type.NextTable",\r
181                 property.getTableFeaturePropType().getImplementedInterface().getName());\r
182         NextTable nextTableProperty = (NextTable) property.getTableFeaturePropType();\r
183         Assert.assertEquals("Wrong next tables size", 3, nextTableProperty.getTables().getTableIds().size());\r
184         Assert.assertEquals("Wrong next tables size", 9, nextTableProperty.getTables().getTableIds().get(0).intValue());\r
185         Assert.assertEquals("Wrong next tables size", 10, nextTableProperty.getTables().getTableIds().get(1).intValue());\r
186         Assert.assertEquals("Wrong next tables size", 11, nextTableProperty.getTables().getTableIds().get(2).intValue());\r
187     }\r
188 }