2287a64abf2534cc69c9e7067266b796bf63e371
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / MultipartRequestInputFactory.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.List;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
19 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMaker;
20 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
22 import org.opendaylight.openflowjava.util.ExperimenterSerializerKeyFactory;
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.ExperimenterIdTableFeatureProperty;
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.NextTableRelatedTableFeatureProperty;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.OxmRelatedTableFeatureProperty;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.table.features.properties.container.table.feature.properties.NextTableIds;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableConfig;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableFeaturesPropType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.experimenter.core.ExperimenterDataOfChoice;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.MultipartRequestBody;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestAggregateCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestExperimenterCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupDescCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupFeaturesCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterCase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterConfigCase;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortDescCase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortStatsCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestQueueCase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.aggregate._case.MultipartRequestAggregate;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.experimenter._case.MultipartRequestExperimenter;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlow;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.group._case.MultipartRequestGroup;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.meter._case.MultipartRequestMeter;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.meter.config._case.MultipartRequestMeterConfig;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.port.stats._case.MultipartRequestPortStats;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.queue._case.MultipartRequestQueue;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeatures;
63 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;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties;
65
66 /**
67  * Translates MultipartRequest messages
68  * @author timotej.kubas
69  * @author michal.polkorab
70  */
71 public class MultipartRequestInputFactory implements OFSerializer<MultipartRequestInput>, SerializerRegistryInjector {
72     private static final byte MESSAGE_TYPE = 18;
73     private static final byte PADDING_IN_MULTIPART_REQUEST_MESSAGE = 4;
74     private static final byte INSTRUCTIONS_CODE = 0;
75     private static final byte INSTRUCTIONS_MISS_CODE = 1;
76     private static final byte NEXT_TABLE_CODE = 2;
77     private static final byte NEXT_TABLE_MISS_CODE = 3;
78     private static final byte WRITE_ACTIONS_CODE = 4;
79     private static final byte WRITE_ACTIONS_MISS_CODE = 5;
80     private static final byte APPLY_ACTIONS_CODE = 6;
81     private static final byte APPLY_ACTIONS_MISS_CODE = 7;
82     private static final byte MATCH_CODE = 8;
83     private static final byte WILDCARDS_CODE = 10;
84     private static final byte WRITE_SETFIELD_CODE = 12;
85     private static final byte WRITE_SETFIELD_MISS_CODE = 13;
86     private static final byte APPLY_SETFIELD_CODE = 14;
87     private static final byte APPLY_SETFIELD_MISS_CODE = 15;
88     private static final byte PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_01 = 3;
89     private static final byte PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_02 = 4;
90     private static final byte PADDING_IN_MULTIPART_REQUEST_AGREGGATE_BODY_01 = 3;
91     private static final byte PADDING_IN_MULTIPART_REQUEST_AGREGGATE_BODY_02 = 4;
92     private static final byte PADDING_IN_MULTIPART_REQUEST_PORTSTATS_BODY = 4;
93     private static final byte PADDING_IN_MULTIPART_REQUEST_GROUP_BODY = 4;
94     private static final byte PADDING_IN_MULTIPART_REQUEST_METER_BODY = 4;
95     private static final byte PADDING_IN_MULTIPART_REQUEST_METER_CONFIG_BODY = 4;
96     private static final byte PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY = 5;
97     private SerializerRegistry registry;
98
99     @Override
100     public void serialize(final MultipartRequestInput message, final ByteBuf outBuffer) {
101         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
102         outBuffer.writeShort(message.getType().getIntValue());
103         outBuffer.writeShort(createMultipartRequestFlagsBitmask(message.getFlags()));
104         outBuffer.writeZero(PADDING_IN_MULTIPART_REQUEST_MESSAGE);
105
106         if (message.getMultipartRequestBody() instanceof MultipartRequestDescCase){
107             serializeDescBody();
108         } else if (message.getMultipartRequestBody() instanceof MultipartRequestFlowCase) {
109             serializeFlowBody(message.getMultipartRequestBody(), outBuffer);
110         } else if (message.getMultipartRequestBody() instanceof MultipartRequestAggregateCase) {
111             serializeAggregateBody(message.getMultipartRequestBody(), outBuffer);
112         } else if (message.getMultipartRequestBody() instanceof MultipartRequestTableCase) {
113             serializeTableBody();
114         } else if (message.getMultipartRequestBody() instanceof MultipartRequestPortStatsCase) {
115             serializePortStatsBody(message.getMultipartRequestBody(), outBuffer);
116         } else if (message.getMultipartRequestBody() instanceof MultipartRequestQueueCase) {
117             serializeQueueBody(message.getMultipartRequestBody(), outBuffer);
118         } else if (message.getMultipartRequestBody() instanceof MultipartRequestGroupCase) {
119             serializeeGroupStatsBody(message.getMultipartRequestBody(), outBuffer);
120         } else if (message.getMultipartRequestBody() instanceof MultipartRequestGroupDescCase) {
121             serializeGroupDescBody();
122         } else if (message.getMultipartRequestBody() instanceof MultipartRequestGroupFeaturesCase) {
123             serializeGroupFeaturesBody();
124         } else if (message.getMultipartRequestBody() instanceof MultipartRequestMeterCase) {
125             serializeMeterBody(message.getMultipartRequestBody(), outBuffer);
126         } else if (message.getMultipartRequestBody() instanceof MultipartRequestMeterConfigCase) {
127             serializeMeterConfigBody(message.getMultipartRequestBody(), outBuffer);
128         } else if (message.getMultipartRequestBody() instanceof MultipartRequestMeterFeaturesCase) {
129             serializeMeterFeaturesBody();
130         } else if (message.getMultipartRequestBody() instanceof MultipartRequestTableFeaturesCase) {
131             serializeTableFeaturesBody(message.getMultipartRequestBody(), outBuffer);
132         } else if (message.getMultipartRequestBody() instanceof MultipartRequestPortDescCase) {
133             serializePortDescBody();
134         } else if (message.getMultipartRequestBody() instanceof MultipartRequestExperimenterCase) {
135                 serializeExperimenterBody(message, outBuffer);
136         }
137         ByteBufUtils.updateOFHeaderLength(outBuffer);
138     }
139
140     private void serializeExperimenterBody(final MultipartRequestInput message,
141             final ByteBuf outBuffer) {
142         MultipartRequestExperimenterCase expCase =
143                 (MultipartRequestExperimenterCase) message.getMultipartRequestBody();
144         MultipartRequestExperimenter experimenter = expCase.getMultipartRequestExperimenter();
145         final long expId = experimenter.getExperimenter().getValue().longValue();
146         final long expType = experimenter.getExpType().longValue();
147
148         // write experimenterId and type
149         outBuffer.writeInt((int) expId);
150         outBuffer.writeInt((int) expType);
151
152         // serialize experimenter data
153         OFSerializer<ExperimenterDataOfChoice> serializer = registry.getSerializer(
154                 ExperimenterSerializerKeyFactory.createMultipartRequestSerializerKey(
155                         EncodeConstants.OF13_VERSION_ID, expId, expType));
156         serializer.serialize(experimenter.getExperimenterDataOfChoice(), outBuffer);
157     }
158
159     private static int createMultipartRequestFlagsBitmask(final MultipartRequestFlags flags) {
160         return ByteBufUtils.fillBitMask(0, flags.isOFPMPFREQMORE());
161     }
162
163     /**
164      * @param multipartRequestBody
165      * @param output
166      */
167     private void serializeDescBody() {
168         // The body of MultiPartRequestDesc is empty
169     }
170
171     /**
172      * @param multipartRequestBody
173      * @param out
174      */
175     private void serializeTableBody() {
176      // The body of MultiPartTable is empty
177     }
178
179     /**
180      * @param multipartRequestBody
181      * @param out
182      */
183     private void serializeGroupDescBody() {
184      // The body of MultiPartRequestGroupDesc is empty
185     }
186
187     /**
188      * @param multipartRequestBody
189      * @param out
190      */
191     private void serializeGroupFeaturesBody() {
192      // The body of MultiPartRequestGroupFeatures is empty
193     }
194
195     /**
196      * @param multipartRequestBody
197      * @param out
198      */
199     private void serializeMeterFeaturesBody() {
200      // The body of MultiPartMeterFeatures is empty
201     }
202
203     /**
204      * @param multipartRequestBody
205      * @param out
206      */
207     private void serializePortDescBody() {
208      // The body of MultiPartPortDesc is empty
209     }
210
211     private void serializeFlowBody(final MultipartRequestBody multipartRequestBody, final ByteBuf output) {
212         MultipartRequestFlowCase flowCase = (MultipartRequestFlowCase) multipartRequestBody;
213         MultipartRequestFlow flow = flowCase.getMultipartRequestFlow();
214         output.writeByte(flow.getTableId().byteValue());
215         output.writeZero(PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_01);
216         output.writeInt(flow.getOutPort().intValue());
217         output.writeInt(flow.getOutGroup().intValue());
218         output.writeZero(PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_02);
219         output.writeLong(flow.getCookie().longValue());
220         output.writeLong(flow.getCookieMask().longValue());
221         OFSerializer<Match> serializer = registry.getSerializer(new MessageTypeKey<>(
222                 EncodeConstants.OF13_VERSION_ID, Match.class));
223         serializer.serialize(flow.getMatch(), output);
224     }
225
226     private void serializeAggregateBody(final MultipartRequestBody multipartRequestBody, final ByteBuf output) {
227         MultipartRequestAggregateCase aggregateCase = (MultipartRequestAggregateCase) multipartRequestBody;
228         MultipartRequestAggregate aggregate = aggregateCase.getMultipartRequestAggregate();
229         output.writeByte(aggregate.getTableId().byteValue());
230         output.writeZero(PADDING_IN_MULTIPART_REQUEST_AGREGGATE_BODY_01);
231         output.writeInt(aggregate.getOutPort().intValue());
232         output.writeInt(aggregate.getOutGroup().intValue());
233         output.writeZero(PADDING_IN_MULTIPART_REQUEST_AGREGGATE_BODY_02);
234         output.writeLong(aggregate.getCookie().longValue());
235         output.writeLong(aggregate.getCookieMask().longValue());
236         OFSerializer<Match> serializer = registry.getSerializer(new MessageTypeKey<>(
237                 EncodeConstants.OF13_VERSION_ID, Match.class));
238         serializer.serialize(aggregate.getMatch(), output);
239     }
240
241     private static void serializePortStatsBody(final MultipartRequestBody multipartRequestBody, final ByteBuf output) {
242         MultipartRequestPortStatsCase portstatsCase = (MultipartRequestPortStatsCase) multipartRequestBody;
243         MultipartRequestPortStats portstats = portstatsCase.getMultipartRequestPortStats();
244         output.writeInt(portstats.getPortNo().intValue());
245         output.writeZero(PADDING_IN_MULTIPART_REQUEST_PORTSTATS_BODY);
246     }
247
248     private static void serializeQueueBody(final MultipartRequestBody multipartRequestBody, final ByteBuf output) {
249         MultipartRequestQueueCase queueCase = (MultipartRequestQueueCase) multipartRequestBody;
250         MultipartRequestQueue queue = queueCase.getMultipartRequestQueue();
251         output.writeInt(queue.getPortNo().intValue());
252         output.writeInt(queue.getQueueId().intValue());
253     }
254
255     private static void serializeeGroupStatsBody(final MultipartRequestBody multipartRequestBody, final ByteBuf output) {
256         MultipartRequestGroupCase groupStatsCase = (MultipartRequestGroupCase) multipartRequestBody;
257         MultipartRequestGroup groupStats = groupStatsCase.getMultipartRequestGroup();
258         output.writeInt(groupStats.getGroupId().getValue().intValue());
259         output.writeZero(PADDING_IN_MULTIPART_REQUEST_GROUP_BODY);
260     }
261
262     private static void serializeMeterBody(final MultipartRequestBody multipartRequestBody, final ByteBuf output) {
263         MultipartRequestMeterCase meterCase = (MultipartRequestMeterCase) multipartRequestBody;
264         MultipartRequestMeter meter = meterCase.getMultipartRequestMeter();
265         output.writeInt(meter.getMeterId().getValue().intValue());
266         output.writeZero(PADDING_IN_MULTIPART_REQUEST_METER_BODY);
267     }
268
269     private static void serializeMeterConfigBody(final MultipartRequestBody multipartRequestBody, final ByteBuf output) {
270         MultipartRequestMeterConfigCase meterConfigCase = (MultipartRequestMeterConfigCase) multipartRequestBody;
271         MultipartRequestMeterConfig meterConfig = meterConfigCase.getMultipartRequestMeterConfig();
272         output.writeInt(meterConfig.getMeterId().getValue().intValue());
273         output.writeZero(PADDING_IN_MULTIPART_REQUEST_METER_CONFIG_BODY);
274     }
275
276     private void serializeTableFeaturesBody(final MultipartRequestBody multipartRequestBody, final ByteBuf output) {
277         if (multipartRequestBody != null) {
278             MultipartRequestTableFeaturesCase tableFeaturesCase = (MultipartRequestTableFeaturesCase) multipartRequestBody;
279             MultipartRequestTableFeatures tableFeatures = tableFeaturesCase.getMultipartRequestTableFeatures();
280             if(tableFeatures.getTableFeatures() != null) {
281                 for (TableFeatures currTableFeature : tableFeatures.getTableFeatures()) {
282                     int tableFeatureLengthIndex = output.writerIndex();
283                     output.writeShort(EncodeConstants.EMPTY_LENGTH);
284                     output.writeByte(currTableFeature.getTableId());
285                     output.writeZero(PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY);
286                     output.writeBytes(currTableFeature.getName().getBytes());
287                     output.writeZero(32 - currTableFeature.getName().getBytes().length);
288                     output.writeLong(currTableFeature.getMetadataMatch().longValue());
289                     output.writeLong(currTableFeature.getMetadataWrite().longValue());
290                     output.writeInt(createTableConfigBitmask(currTableFeature.getConfig()));
291                     output.writeInt(currTableFeature.getMaxEntries().intValue());
292                     writeTableFeatureProperties(output, currTableFeature.getTableFeatureProperties());
293                     output.setShort(tableFeatureLengthIndex, output.writerIndex() - tableFeatureLengthIndex);
294                 }
295             }
296         }
297     }
298
299     private void writeTableFeatureProperties(final ByteBuf output, final List<TableFeatureProperties> props) {
300         if (props != null) {
301             for (TableFeatureProperties property : props) {
302                 TableFeaturesPropType type = property.getType();
303                 if (type.equals(TableFeaturesPropType.OFPTFPTINSTRUCTIONS)) {
304                     writeInstructionRelatedTableProperty(output, property, INSTRUCTIONS_CODE);
305                 } else if (type.equals(TableFeaturesPropType.OFPTFPTINSTRUCTIONSMISS)) {
306                     writeInstructionRelatedTableProperty(output, property, INSTRUCTIONS_MISS_CODE);
307                 } else if (type.equals(TableFeaturesPropType.OFPTFPTNEXTTABLES)) {
308                     writeNextTableRelatedTableProperty(output, property, NEXT_TABLE_CODE);
309                 } else if (type.equals(TableFeaturesPropType.OFPTFPTNEXTTABLESMISS)) {
310                     writeNextTableRelatedTableProperty(output, property, NEXT_TABLE_MISS_CODE);
311                 } else if (type.equals(TableFeaturesPropType.OFPTFPTWRITEACTIONS)) {
312                     writeActionsRelatedTableProperty(output, property, WRITE_ACTIONS_CODE);
313                 } else if (type.equals(TableFeaturesPropType.OFPTFPTWRITEACTIONSMISS)) {
314                     writeActionsRelatedTableProperty(output, property, WRITE_ACTIONS_MISS_CODE);
315                 } else if (type.equals(TableFeaturesPropType.OFPTFPTAPPLYACTIONS)) {
316                     writeActionsRelatedTableProperty(output, property, APPLY_ACTIONS_CODE);
317                 } else if (type.equals(TableFeaturesPropType.OFPTFPTAPPLYACTIONSMISS)) {
318                     writeActionsRelatedTableProperty(output, property, APPLY_ACTIONS_MISS_CODE);
319                 } else if (type.equals(TableFeaturesPropType.OFPTFPTMATCH)) {
320                     writeOxmRelatedTableProperty(output, property, MATCH_CODE);
321                 } else if (type.equals(TableFeaturesPropType.OFPTFPTWILDCARDS)) {
322                     writeOxmRelatedTableProperty(output, property, WILDCARDS_CODE);
323                 } else if (type.equals(TableFeaturesPropType.OFPTFPTWRITESETFIELD)) {
324                     writeOxmRelatedTableProperty(output, property, WRITE_SETFIELD_CODE);
325                 } else if (type.equals(TableFeaturesPropType.OFPTFPTWRITESETFIELDMISS)) {
326                     writeOxmRelatedTableProperty(output, property, WRITE_SETFIELD_MISS_CODE);
327                 } else if (type.equals(TableFeaturesPropType.OFPTFPTAPPLYSETFIELD)) {
328                     writeOxmRelatedTableProperty(output, property, APPLY_SETFIELD_CODE);
329                 } else if (type.equals(TableFeaturesPropType.OFPTFPTAPPLYSETFIELDMISS)) {
330                     writeOxmRelatedTableProperty(output, property, APPLY_SETFIELD_MISS_CODE);
331                 } else if (type.equals(TableFeaturesPropType.OFPTFPTEXPERIMENTER)) {
332                     writeExperimenterRelatedTableProperty(output, property);
333                 } else if (type.equals(TableFeaturesPropType.OFPTFPTEXPERIMENTERMISS)) {
334                     writeExperimenterRelatedTableProperty(output, property);
335                 }
336             }
337         }
338     }
339
340     private void writeInstructionRelatedTableProperty(final ByteBuf output,
341             final TableFeatureProperties property, final byte code) {
342         int startIndex = output.writerIndex();
343         output.writeShort(code);
344         int lengthIndex = output.writerIndex();
345         output.writeShort(EncodeConstants.EMPTY_LENGTH);
346         List<Instruction> instructions = property.
347                 getAugmentation(InstructionRelatedTableFeatureProperty.class).getInstruction();
348         if (instructions != null) {
349             TypeKeyMaker<Instruction> keyMaker = TypeKeyMakerFactory
350                     .createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
351             ListSerializer.serializeHeaderList(instructions, keyMaker, registry, output);
352         }
353         int length = output.writerIndex() - startIndex;
354         output.setShort(lengthIndex, length);
355         output.writeZero(paddingNeeded(length));
356     }
357
358     private static void writeNextTableRelatedTableProperty(final ByteBuf output,
359             final TableFeatureProperties property, final byte code) {
360         int startIndex = output.writerIndex();
361         output.writeShort(code);
362         int lengthIndex = output.writerIndex();
363         output.writeShort(EncodeConstants.EMPTY_LENGTH);
364         List<NextTableIds> nextTableIds = property.
365                 getAugmentation(NextTableRelatedTableFeatureProperty.class).getNextTableIds();
366         if (nextTableIds != null) {
367             for (NextTableIds next : nextTableIds) {
368                 output.writeByte(next.getTableId());
369             }
370         }
371         int length = output.writerIndex() - startIndex;
372         output.setShort(lengthIndex, length);
373         output.writeZero(paddingNeeded(length));
374     }
375
376     private static int paddingNeeded(final int length) {
377         int paddingRemainder = length % EncodeConstants.PADDING;
378         int result = 0;
379         if (paddingRemainder != 0) {
380             result = EncodeConstants.PADDING - paddingRemainder;
381         }
382         return result;
383     }
384
385     private void writeActionsRelatedTableProperty(final ByteBuf output,
386             final TableFeatureProperties property, final byte code) {
387         int startIndex = output.writerIndex();
388         output.writeShort(code);
389         int lengthIndex = output.writerIndex();
390         output.writeShort(EncodeConstants.EMPTY_LENGTH);
391         List<Action> actions = property.
392                 getAugmentation(ActionRelatedTableFeatureProperty.class).getAction();
393         if (actions != null) {
394             TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory
395                     .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID);
396             ListSerializer.serializeHeaderList(actions, keyMaker, registry, output);
397         }
398         int length = output.writerIndex() - startIndex;
399         output.setShort(lengthIndex, length);
400         output.writeZero(paddingNeeded(length));
401     }
402
403     private void writeOxmRelatedTableProperty(final ByteBuf output,
404             final TableFeatureProperties property, final byte code) {
405         int startIndex = output.writerIndex();
406         output.writeShort(code);
407         int lengthIndex = output.writerIndex();
408         output.writeShort(EncodeConstants.EMPTY_LENGTH);
409         List<MatchEntry> entries = property.
410                 getAugmentation(OxmRelatedTableFeatureProperty.class).getMatchEntry();
411         if (entries != null) {
412             TypeKeyMaker<MatchEntry> keyMaker = TypeKeyMakerFactory
413                     .createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
414             ListSerializer.serializeHeaderList(entries, keyMaker, registry, output);
415         }
416         int length = output.writerIndex() - startIndex;
417         output.setShort(lengthIndex, length);
418         output.writeZero(paddingNeeded(length));
419     }
420
421     private void writeExperimenterRelatedTableProperty(final ByteBuf output,
422             final TableFeatureProperties property) {
423         long expId = property.getAugmentation(ExperimenterIdTableFeatureProperty.class).getExperimenter().getValue();
424         OFSerializer<TableFeatureProperties> serializer = registry.getSerializer(
425                 ExperimenterSerializerKeyFactory.createMultipartRequestTFSerializerKey(
426                         EncodeConstants.OF13_VERSION_ID, expId));
427         serializer.serialize(property, output);
428     }
429
430     private static int createTableConfigBitmask(final TableConfig tableConfig) {
431         return ByteBufUtils.fillBitMask(3, tableConfig.isOFPTCDEPRECATEDMASK());
432     }
433
434     @Override
435     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
436         this.registry = serializerRegistry;
437     }
438 }