Extend openflow-protocol-impl serialization
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / MultipartRequestTableFeaturesInputMessageFactoryTest.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
22 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionRelatedTableFeatureProperty;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionRelatedTableFeaturePropertyBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.InstructionRelatedTableFeatureProperty;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.InstructionRelatedTableFeaturePropertyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.NextTableRelatedTableFeatureProperty;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.NextTableRelatedTableFeaturePropertyBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.OxmRelatedTableFeatureProperty;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.OxmRelatedTableFeaturePropertyBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.table.features.properties.container.table.feature.properties.NextTableIds;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.table.features.properties.container.table.feature.properties.NextTableIdsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableConfig;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableFeaturesPropType;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPhyPort;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.multipart.request.table.features.TableFeatures;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.multipart.request.table.features.TableFeaturesBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeaturePropertiesBuilder;
57
58 /**
59  * @author giuseppex.petralia@intel.com
60  *
61  */
62 public class MultipartRequestTableFeaturesInputMessageFactoryTest {
63     private OFDeserializer<MultipartRequestInput> factory;
64
65     @Before
66     public void startUp() {
67         DeserializerRegistry desRegistry = new DeserializerRegistryImpl();
68         desRegistry.init();
69         factory = desRegistry
70                 .getDeserializer(new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, 18, MultipartRequestInput.class));
71     }
72
73     @Test
74     public void test() {
75         ByteBuf bb = BufferHelper.buildBuffer("00 0c 00 01 00 00 00 00 00 68 01 00 00 00 00 00 4e 61 6d "
76                 + "65 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
77                 + "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 10 00 01 "
78                 + "00 04 00 02 00 04 00 04 00 04 00 02 00 05 01 00 00 00 00 04 00 08 00 00 00 04 00 08 00 08 80 00 02 04 ");
79         MultipartRequestInput deserializedMessage = BufferHelper.deserialize(factory, bb);
80         BufferHelper.checkHeaderV13(deserializedMessage);
81         Assert.assertEquals("Wrong type", MultipartType.forValue(12), deserializedMessage.getType());
82         Assert.assertEquals("Wrong flags", new MultipartRequestFlags(true), deserializedMessage.getFlags());
83         Assert.assertEquals("Wrong body", createTableFeatures(), deserializedMessage.getMultipartRequestBody());
84     }
85
86     public MultipartRequestTableFeaturesCase createTableFeatures() {
87         MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
88         MultipartRequestTableFeaturesBuilder builder = new MultipartRequestTableFeaturesBuilder();
89         builder.setTableFeatures(createTableFeaturesList());
90         caseBuilder.setMultipartRequestTableFeatures(builder.build());
91         return caseBuilder.build();
92
93     }
94
95     public List<TableFeatures> createTableFeaturesList() {
96         List<TableFeatures> list = new ArrayList<>();
97         TableFeaturesBuilder builder = new TableFeaturesBuilder();
98         builder.setTableId((short) 1);
99         builder.setName("Name");
100         builder.setMetadataWrite(new BigInteger("1"));
101         builder.setMetadataMatch(new BigInteger("1"));
102         builder.setMaxEntries(1L);
103         builder.setConfig(new TableConfig(false));
104         builder.setTableFeatureProperties(createTableFeatureProperties());
105         list.add(builder.build());
106         return list;
107     }
108
109     public List<TableFeatureProperties> createTableFeatureProperties() {
110         List<TableFeatureProperties> list = new ArrayList<>();
111         TableFeaturePropertiesBuilder builder = new TableFeaturePropertiesBuilder();
112         builder.setType(TableFeaturesPropType.forValue(0));
113         InstructionRelatedTableFeaturePropertyBuilder insBuilder = new InstructionRelatedTableFeaturePropertyBuilder();
114         insBuilder.setInstruction(createInstructions());
115         builder.addAugmentation(InstructionRelatedTableFeatureProperty.class, insBuilder.build());
116         list.add(builder.build());
117
118         builder = new TableFeaturePropertiesBuilder();
119         builder.setType(TableFeaturesPropType.forValue(2));
120         NextTableRelatedTableFeaturePropertyBuilder nextBuilder = new NextTableRelatedTableFeaturePropertyBuilder();
121         nextBuilder.setNextTableIds(createNextTableIds());
122         builder.addAugmentation(NextTableRelatedTableFeatureProperty.class, nextBuilder.build());
123         list.add(builder.build());
124
125         builder = new TableFeaturePropertiesBuilder();
126         builder.setType(TableFeaturesPropType.forValue(4));
127         ActionRelatedTableFeaturePropertyBuilder actionBuilder = new ActionRelatedTableFeaturePropertyBuilder();
128         actionBuilder.setAction(createAction());
129         builder.addAugmentation(ActionRelatedTableFeatureProperty.class, actionBuilder.build());
130         list.add(builder.build());
131
132         builder = new TableFeaturePropertiesBuilder();
133         builder.setType(TableFeaturesPropType.forValue(8));
134         OxmRelatedTableFeaturePropertyBuilder oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder();
135         oxmBuilder.setMatchEntry(createMatchEntries());
136         builder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build());
137         list.add(builder.build());
138
139         return list;
140     }
141
142     public List<MatchEntry> createMatchEntries() {
143         List<MatchEntry> entries = new ArrayList<>();
144         MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
145         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
146         entriesBuilder.setOxmMatchField(InPhyPort.class);
147         entriesBuilder.setHasMask(false);
148         entries.add(entriesBuilder.build());
149         return entries;
150     }
151
152     public List<Action> createAction() {
153         List<Action> actions = new ArrayList<>();
154         ActionBuilder actionBuilder = new ActionBuilder();
155         OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
156         actionBuilder.setActionChoice(caseBuilder.build());
157         actions.add(actionBuilder.build());
158         return actions;
159     }
160
161     public List<NextTableIds> createNextTableIds() {
162         List<NextTableIds> list = new ArrayList<>();
163         NextTableIdsBuilder builder = new NextTableIdsBuilder();
164         builder.setTableId((short) 1);
165         list.add(builder.build());
166         return list;
167     }
168
169     public List<Instruction> createInstructions() {
170         List<Instruction> instructions = new ArrayList<>();
171         InstructionBuilder insBuilder = new InstructionBuilder();
172         GotoTableCaseBuilder goToCaseBuilder = new GotoTableCaseBuilder();
173         insBuilder.setInstructionChoice(goToCaseBuilder.build());
174         instructions.add(insBuilder.build());
175         WriteMetadataCaseBuilder metadataCaseBuilder = new WriteMetadataCaseBuilder();
176         insBuilder.setInstructionChoice(metadataCaseBuilder.build());
177         instructions.add(insBuilder.build());
178         insBuilder = new InstructionBuilder();
179         ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
180         insBuilder.setInstructionChoice(applyActionsCaseBuilder.build());
181         instructions.add(insBuilder.build());
182         return instructions;
183     }
184 }