Migrate IetfYangUtil.bytesFor() users
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / MultipartReplyMessageFactory.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.serialization.factories;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
13 import java.nio.charset.StandardCharsets;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Objects;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
21 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
22 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
23 import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
24 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMaker;
25 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory;
26 import org.opendaylight.openflowjava.util.ByteBufUtils;
27 import org.opendaylight.openflowjava.util.ExperimenterSerializerKeyFactory;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionRelatedTableFeatureProperty;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ExperimenterIdTableFeatureProperty;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.InstructionRelatedTableFeatureProperty;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.NextTableRelatedTableFeatureProperty;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.OxmRelatedTableFeatureProperty;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.table.features.properties.container.table.feature.properties.NextTableIds;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupCapabilities;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupTypes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterBandTypeBitmap;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableConfig;
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.grouping.Match;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterBandCommons;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.MeterBand;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDropCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDscpRemarkCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandExperimenterCase;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.drop._case.MeterBandDrop;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.dscp.remark._case.MeterBandDscpRemark;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.experimenter._case.MeterBandExperimenter;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupCase;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupDescCase;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterCase;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterConfigCase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortStatsCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableCase;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.aggregate._case.MultipartReplyAggregate;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group._case.MultipartReplyGroup;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group._case.multipart.reply.group.GroupStats;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group._case.multipart.reply.group.group.stats.BucketStats;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.desc._case.MultipartReplyGroupDesc;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.desc._case.multipart.reply.group.desc.GroupDesc;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.MultipartReplyMeter;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.MeterStats;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.meter.stats.MeterBandStats;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.MultipartReplyMeterConfig;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.multipart.reply.meter.config.MeterConfig;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.multipart.reply.meter.config.meter.config.Bands;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDesc;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.Ports;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.MultipartReplyPortStats;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.multipart.reply.port.stats.PortStats;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueue;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.multipart.reply.queue.QueueStats;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.MultipartReplyTable;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.multipart.reply.table.TableStats;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
100 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;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties;
102 import org.opendaylight.yangtools.yang.common.Uint32;
103
104 /**
105  * Translates MultipartReply messages.
106  *
107  * @author giuseppex.petralia@intel.com
108  */
109 @SuppressFBWarnings("UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR") // FB doesn't recognize Objects.requireNonNull
110 public class MultipartReplyMessageFactory implements OFSerializer<MultipartReplyMessage>, SerializerRegistryInjector {
111
112     private static final byte MESSAGE_TYPE = 19;
113     private static final byte PADDING = 4;
114     private static final byte PORT_DESC_PADDING_1 = 4;
115     private static final byte PORT_DESC_PADDING_2 = 2;
116     private static final int FLOW_STATS_LENGTH_INDEX = 0;
117     private static final byte FLOW_STATS_PADDING_1 = 1;
118     private static final byte FLOW_STATS_PADDING_2 = 6;
119     private static final byte AGGREGATE_PADDING = 4;
120     private static final byte TABLE_PADDING = 3;
121     private static final byte PORT_STATS_PADDING = 4;
122     private static final byte GROUP_STATS_PADDING_1 = 2;
123     private static final byte GROUP_STATS_PADDING_2 = 4;
124     private static final int GROUP_STATS_LENGTH_INDEX = 0;
125     private static final int GROUP_DESC_LENGTH_INDEX = 0;
126     private static final int BUCKET_LENGTH_INDEX = 0;
127     private static final byte GROUP_DESC_PADDING = 1;
128     private static final byte BUCKET_PADDING = 4;
129     private static final int METER_LENGTH_INDEX = 4;
130     private static final byte METER_PADDING = 6;
131     private static final int METER_CONFIG_LENGTH_INDEX = 0;
132     private static final short LENGTH_OF_METER_BANDS = 16;
133     private static final byte METER_FEATURES_PADDING = 2;
134     private static final int TABLE_FEATURES_LENGTH_INDEX = 0;
135     private static final byte TABLE_FEATURES_PADDING = 5;
136     private static final byte INSTRUCTIONS_CODE = 0;
137     private static final byte INSTRUCTIONS_MISS_CODE = 1;
138     private static final byte NEXT_TABLE_CODE = 2;
139     private static final byte NEXT_TABLE_MISS_CODE = 3;
140     private static final byte WRITE_ACTIONS_CODE = 4;
141     private static final byte WRITE_ACTIONS_MISS_CODE = 5;
142     private static final byte APPLY_ACTIONS_CODE = 6;
143     private static final byte APPLY_ACTIONS_MISS_CODE = 7;
144     private static final byte MATCH_CODE = 8;
145     private static final byte WILDCARDS_CODE = 10;
146     private static final byte WRITE_SETFIELD_CODE = 12;
147     private static final byte WRITE_SETFIELD_MISS_CODE = 13;
148     private static final byte APPLY_SETFIELD_CODE = 14;
149     private static final byte APPLY_SETFIELD_MISS_CODE = 15;
150
151     private SerializerRegistry registry;
152
153     @Override
154     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
155         this.registry = serializerRegistry;
156     }
157
158     @Override
159     public void serialize(final MultipartReplyMessage message, final ByteBuf outBuffer) {
160         Objects.requireNonNull(registry);
161
162         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
163         outBuffer.writeShort(message.getType().getIntValue());
164         writeFlags(message.getFlags(), outBuffer);
165         outBuffer.writeZero(PADDING);
166         switch (message.getType()) {
167             case OFPMPDESC:
168                 serializeDescBody(message.getMultipartReplyBody(), outBuffer);
169                 break;
170             case OFPMPFLOW:
171                 serializeFlowBody(message.getMultipartReplyBody(), outBuffer, message);
172                 break;
173             case OFPMPAGGREGATE:
174                 serializeAggregateBody(message.getMultipartReplyBody(), outBuffer);
175                 break;
176             case OFPMPTABLE:
177                 serializeTableBody(message.getMultipartReplyBody(), outBuffer);
178                 break;
179             case OFPMPPORTSTATS:
180                 serializePortStatsBody(message.getMultipartReplyBody(), outBuffer);
181                 break;
182             case OFPMPQUEUE:
183                 serializeQueueBody(message.getMultipartReplyBody(), outBuffer);
184                 break;
185             case OFPMPGROUP:
186                 serializeGroupBody(message.getMultipartReplyBody(), outBuffer);
187                 break;
188             case OFPMPGROUPDESC:
189                 serializeGroupDescBody(message.getMultipartReplyBody(), outBuffer, message);
190                 break;
191             case OFPMPGROUPFEATURES:
192                 serializeGroupFeaturesBody(message.getMultipartReplyBody(), outBuffer);
193                 break;
194             case OFPMPMETER:
195                 serializeMeterBody(message.getMultipartReplyBody(), outBuffer);
196                 break;
197             case OFPMPMETERCONFIG:
198                 serializeMeterConfigBody(message.getMultipartReplyBody(), outBuffer);
199                 break;
200             case OFPMPMETERFEATURES:
201                 serializeMeterFeaturesBody(message.getMultipartReplyBody(), outBuffer);
202                 break;
203             case OFPMPTABLEFEATURES:
204                 serializeTableFeaturesBody(message.getMultipartReplyBody(), outBuffer);
205                 break;
206             case OFPMPPORTDESC:
207                 serializePortDescBody(message.getMultipartReplyBody(), outBuffer);
208                 break;
209             case OFPMPEXPERIMENTER:
210                 serializeExperimenterBody(message.getMultipartReplyBody(), outBuffer);
211                 break;
212             default:
213                 break;
214         }
215         ByteBufUtils.updateOFHeaderLength(outBuffer);
216     }
217
218     private void serializeExperimenterBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
219 //        MultipartReplyExperimenterCase experimenterCase = (MultipartReplyExperimenterCase) body;
220 //        MultipartReplyExperimenter experimenterBody = experimenterCase.getMultipartReplyExperimenter();
221         // TODO: experimenterBody does not have get methods
222     }
223
224     private void writeFlags(final MultipartRequestFlags flags, final ByteBuf outBuffer) {
225         Map<Integer, Boolean> map = new HashMap<>();
226         map.put(0, flags.isOFPMPFREQMORE());
227         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
228         outBuffer.writeShort(bitmap);
229     }
230
231     private void serializeTableFeaturesBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
232         MultipartReplyTableFeaturesCase tableFeaturesCase = (MultipartReplyTableFeaturesCase) body;
233         MultipartReplyTableFeatures tableFeatures = tableFeaturesCase.getMultipartReplyTableFeatures();
234         for (TableFeatures tableFeature : tableFeatures.getTableFeatures()) {
235             ByteBuf tableFeatureBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
236             tableFeatureBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
237             tableFeatureBuff.writeByte(tableFeature.getTableId().toJava());
238             tableFeatureBuff.writeZero(TABLE_FEATURES_PADDING);
239             write32String(tableFeature.getName(), tableFeatureBuff);
240             tableFeatureBuff.writeBytes(tableFeature.getMetadataMatch());
241             tableFeatureBuff.writeZero(64 - tableFeature.getMetadataMatch().length);
242             tableFeatureBuff.writeBytes(tableFeature.getMetadataWrite());
243             tableFeatureBuff.writeZero(64 - tableFeature.getMetadataWrite().length);
244             writeTableConfig(tableFeature.getConfig(), tableFeatureBuff);
245             tableFeatureBuff.writeInt(tableFeature.getMaxEntries().intValue());
246             for (TableFeatureProperties tableFeatureProp : tableFeature.getTableFeatureProperties()) {
247                 switch (tableFeatureProp.getType()) {
248                     case OFPTFPTINSTRUCTIONS:
249                         writeInstructionRelatedTableProperty(tableFeatureBuff, tableFeatureProp, INSTRUCTIONS_CODE);
250                         break;
251                     case OFPTFPTINSTRUCTIONSMISS:
252                         writeInstructionRelatedTableProperty(tableFeatureBuff, tableFeatureProp,
253                                 INSTRUCTIONS_MISS_CODE);
254                         break;
255                     case OFPTFPTNEXTTABLES:
256                         writeNextTableRelatedTableProperty(tableFeatureBuff, tableFeatureProp, NEXT_TABLE_CODE);
257                         break;
258                     case OFPTFPTNEXTTABLESMISS:
259                         writeNextTableRelatedTableProperty(tableFeatureBuff, tableFeatureProp, NEXT_TABLE_MISS_CODE);
260                         break;
261                     case OFPTFPTWRITEACTIONS:
262                         writeActionsRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WRITE_ACTIONS_CODE);
263                         break;
264                     case OFPTFPTWRITEACTIONSMISS:
265                         writeActionsRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WRITE_ACTIONS_MISS_CODE);
266                         break;
267                     case OFPTFPTAPPLYACTIONS:
268                         writeActionsRelatedTableProperty(tableFeatureBuff, tableFeatureProp, APPLY_ACTIONS_CODE);
269                         break;
270                     case OFPTFPTAPPLYACTIONSMISS:
271                         writeActionsRelatedTableProperty(tableFeatureBuff, tableFeatureProp, APPLY_ACTIONS_MISS_CODE);
272                         break;
273                     case OFPTFPTMATCH:
274                         writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, MATCH_CODE);
275                         break;
276                     case OFPTFPTWILDCARDS:
277                         writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WILDCARDS_CODE);
278                         break;
279                     case OFPTFPTWRITESETFIELD:
280                         writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WRITE_SETFIELD_CODE);
281                         break;
282                     case OFPTFPTWRITESETFIELDMISS:
283                         writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WRITE_SETFIELD_MISS_CODE);
284                         break;
285                     case OFPTFPTAPPLYSETFIELD:
286                         writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, APPLY_SETFIELD_CODE);
287                         break;
288                     case OFPTFPTAPPLYSETFIELDMISS:
289                         writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, APPLY_SETFIELD_MISS_CODE);
290                         break;
291                     case OFPTFPTEXPERIMENTER:
292                     case OFPTFPTEXPERIMENTERMISS:
293                         writeExperimenterRelatedTableProperty(tableFeatureBuff, tableFeatureProp);
294                         break;
295                     default:
296                         break;
297                 }
298             }
299             tableFeatureBuff.setShort(TABLE_FEATURES_LENGTH_INDEX, tableFeatureBuff.readableBytes());
300             outBuffer.writeBytes(tableFeatureBuff);
301         }
302     }
303
304     private void writeExperimenterRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property) {
305         long expId = property.augmentation(ExperimenterIdTableFeatureProperty.class).getExperimenter().getValue()
306             .toJava();
307         OFSerializer<TableFeatureProperties> serializer = registry.getSerializer(ExperimenterSerializerKeyFactory
308                 .createMultipartRequestTFSerializerKey(EncodeConstants.OF13_VERSION_ID, expId));
309         serializer.serialize(property, output);
310     }
311
312     private void writeOxmRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property,
313             final byte code) {
314         final int startIndex = output.writerIndex();
315         output.writeShort(code);
316         int lengthIndex = output.writerIndex();
317         output.writeShort(EncodeConstants.EMPTY_LENGTH);
318         List<MatchEntry> entries = property.augmentation(OxmRelatedTableFeatureProperty.class).getMatchEntry();
319         if (entries != null) {
320             TypeKeyMaker<MatchEntry> keyMaker = TypeKeyMakerFactory
321                     .createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
322             ListSerializer.serializeHeaderList(entries, keyMaker, registry, output);
323         }
324         int length = output.writerIndex() - startIndex;
325         output.setShort(lengthIndex, length);
326         output.writeZero(paddingNeeded(length));
327     }
328
329     private void writeActionsRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property,
330             final byte code) {
331         final int startIndex = output.writerIndex();
332         output.writeShort(code);
333         int lengthIndex = output.writerIndex();
334         output.writeShort(EncodeConstants.EMPTY_LENGTH);
335         List<Action> actions = property.augmentation(ActionRelatedTableFeatureProperty.class).getAction();
336         if (actions != null) {
337             TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF13_VERSION_ID);
338             ListSerializer.serializeHeaderList(actions, keyMaker, registry, output);
339         }
340         int length = output.writerIndex() - startIndex;
341         output.setShort(lengthIndex, length);
342         output.writeZero(paddingNeeded(length));
343     }
344
345     private static void writeNextTableRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property,
346             final byte code) {
347         final int startIndex = output.writerIndex();
348         output.writeShort(code);
349         int lengthIndex = output.writerIndex();
350         output.writeShort(EncodeConstants.EMPTY_LENGTH);
351         List<NextTableIds> nextTableIds = property.augmentation(NextTableRelatedTableFeatureProperty.class)
352                 .getNextTableIds();
353         if (nextTableIds != null) {
354             for (NextTableIds next : nextTableIds) {
355                 output.writeByte(next.getTableId().toJava());
356             }
357         }
358         int length = output.writerIndex() - startIndex;
359         output.setShort(lengthIndex, length);
360         output.writeZero(paddingNeeded(length));
361     }
362
363     private void writeInstructionRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property,
364             final byte code) {
365         final int startIndex = output.writerIndex();
366         output.writeShort(code);
367         int lengthIndex = output.writerIndex();
368         output.writeShort(EncodeConstants.EMPTY_LENGTH);
369         List<Instruction> instructions = property.augmentation(InstructionRelatedTableFeatureProperty.class)
370                 .getInstruction();
371         if (instructions != null) {
372             TypeKeyMaker<Instruction> keyMaker = TypeKeyMakerFactory
373                     .createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
374             ListSerializer.serializeHeaderList(instructions, keyMaker, registry, output);
375         }
376         int length = output.writerIndex() - startIndex;
377         output.setShort(lengthIndex, length);
378         output.writeZero(paddingNeeded(length));
379     }
380
381     private static int paddingNeeded(final int length) {
382         int paddingRemainder = length % EncodeConstants.PADDING;
383         int result = 0;
384         if (paddingRemainder != 0) {
385             result = EncodeConstants.PADDING - paddingRemainder;
386         }
387         return result;
388     }
389
390     private void writeTableConfig(final TableConfig tableConfig, final ByteBuf outBuffer) {
391         Map<Integer, Boolean> map = new HashMap<>();
392         map.put(0, tableConfig.isOFPTCDEPRECATEDMASK());
393         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
394         outBuffer.writeInt(bitmap);
395     }
396
397     private void serializeMeterFeaturesBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
398         MultipartReplyMeterFeaturesCase meterFeaturesCase = (MultipartReplyMeterFeaturesCase) body;
399         MultipartReplyMeterFeatures meterFeatures = meterFeaturesCase.getMultipartReplyMeterFeatures();
400         outBuffer.writeInt(meterFeatures.getMaxMeter().intValue());
401         writeBandTypes(meterFeatures.getBandTypes(), outBuffer);
402         writeMeterFlags(meterFeatures.getCapabilities(), outBuffer);
403         outBuffer.writeByte(meterFeatures.getMaxBands().toJava());
404         outBuffer.writeByte(meterFeatures.getMaxColor().toJava());
405         outBuffer.writeZero(METER_FEATURES_PADDING);
406     }
407
408     private void writeBandTypes(final MeterBandTypeBitmap bandTypes, final ByteBuf outBuffer) {
409         Map<Integer, Boolean> map = new HashMap<>();
410         map.put(0, bandTypes.isOFPMBTDROP());
411         map.put(1, bandTypes.isOFPMBTDSCPREMARK());
412         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
413         outBuffer.writeInt(bitmap);
414     }
415
416     private void serializeMeterConfigBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
417         MultipartReplyMeterConfigCase meterConfigCase = (MultipartReplyMeterConfigCase) body;
418         MultipartReplyMeterConfig meter = meterConfigCase.getMultipartReplyMeterConfig();
419         for (MeterConfig meterConfig : meter.getMeterConfig()) {
420             ByteBuf meterConfigBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
421             meterConfigBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
422             writeMeterFlags(meterConfig.getFlags(), meterConfigBuff);
423             meterConfigBuff.writeInt(meterConfig.getMeterId().getValue().intValue());
424             for (Bands currentBand : meterConfig.getBands()) {
425                 MeterBand meterBand = currentBand.getMeterBand();
426                 if (meterBand instanceof MeterBandDropCase) {
427                     MeterBandDropCase dropBandCase = (MeterBandDropCase) meterBand;
428                     MeterBandDrop dropBand = dropBandCase.getMeterBandDrop();
429                     writeBandCommonFields(dropBand, meterConfigBuff);
430                 } else if (meterBand instanceof MeterBandDscpRemarkCase) {
431                     MeterBandDscpRemarkCase dscpRemarkBandCase = (MeterBandDscpRemarkCase) meterBand;
432                     MeterBandDscpRemark dscpRemarkBand = dscpRemarkBandCase.getMeterBandDscpRemark();
433                     writeBandCommonFields(dscpRemarkBand, meterConfigBuff);
434                 } else if (meterBand instanceof MeterBandExperimenterCase) {
435                     MeterBandExperimenterCase experimenterBandCase = (MeterBandExperimenterCase) meterBand;
436                     MeterBandExperimenter experimenterBand = experimenterBandCase.getMeterBandExperimenter();
437                     writeBandCommonFields(experimenterBand, meterConfigBuff);
438                 }
439             }
440             meterConfigBuff.setShort(METER_CONFIG_LENGTH_INDEX, meterConfigBuff.readableBytes());
441             outBuffer.writeBytes(meterConfigBuff);
442         }
443     }
444
445     private static void writeBandCommonFields(final MeterBandCommons meterBand, final ByteBuf outBuffer) {
446         outBuffer.writeShort(meterBand.getType().getIntValue());
447         outBuffer.writeShort(LENGTH_OF_METER_BANDS);
448         outBuffer.writeInt(meterBand.getRate().intValue());
449         outBuffer.writeInt(meterBand.getBurstSize().intValue());
450     }
451
452     private void writeMeterFlags(final MeterFlags flags, final ByteBuf outBuffer) {
453         Map<Integer, Boolean> map = new HashMap<>();
454         map.put(0, flags.isOFPMFKBPS());
455         map.put(1, flags.isOFPMFPKTPS());
456         map.put(2, flags.isOFPMFBURST());
457         map.put(3, flags.isOFPMFSTATS());
458         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
459         outBuffer.writeShort(bitmap);
460     }
461
462     private void serializeMeterBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
463         MultipartReplyMeterCase meterCase = (MultipartReplyMeterCase) body;
464         MultipartReplyMeter meter = meterCase.getMultipartReplyMeter();
465         for (MeterStats meterStats : meter.getMeterStats()) {
466             ByteBuf meterStatsBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
467             meterStatsBuff.writeInt(meterStats.getMeterId().getValue().intValue());
468             meterStatsBuff.writeInt(EncodeConstants.EMPTY_LENGTH);
469             meterStatsBuff.writeZero(METER_PADDING);
470             meterStatsBuff.writeInt(meterStats.getFlowCount().intValue());
471             meterStatsBuff.writeLong(meterStats.getPacketInCount().longValue());
472             meterStatsBuff.writeLong(meterStats.getByteInCount().longValue());
473             meterStatsBuff.writeInt(meterStats.getDurationSec().intValue());
474             meterStatsBuff.writeInt(meterStats.getDurationNsec().intValue());
475             for (MeterBandStats meterBandStats : meterStats.getMeterBandStats()) {
476                 meterStatsBuff.writeLong(meterBandStats.getPacketBandCount().longValue());
477                 meterStatsBuff.writeLong(meterBandStats.getByteBandCount().longValue());
478             }
479             meterStatsBuff.setInt(METER_LENGTH_INDEX, meterStatsBuff.readableBytes());
480             outBuffer.writeBytes(meterStatsBuff);
481         }
482     }
483
484     private void serializeGroupFeaturesBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
485         MultipartReplyGroupFeaturesCase groupFeaturesCase = (MultipartReplyGroupFeaturesCase) body;
486         MultipartReplyGroupFeatures groupFeatures = groupFeaturesCase.getMultipartReplyGroupFeatures();
487         writeGroupTypes(groupFeatures.getTypes(), outBuffer);
488         writeGroupCapabilities(groupFeatures.getCapabilities(), outBuffer);
489         for (Uint32 maxGroups : groupFeatures.getMaxGroups()) {
490             outBuffer.writeInt(maxGroups.intValue());
491         }
492         for (ActionType action : groupFeatures.getActionsBitmap()) {
493             writeActionType(action, outBuffer);
494         }
495     }
496
497     private void writeActionType(final ActionType action, final ByteBuf outBuffer) {
498         Map<Integer, Boolean> map = new HashMap<>();
499         map.put(0, action.isOFPATOUTPUT());
500         map.put(1, action.isOFPATCOPYTTLOUT());
501         map.put(2, action.isOFPATCOPYTTLIN());
502         map.put(3, action.isOFPATSETMPLSTTL());
503         map.put(4, action.isOFPATDECMPLSTTL());
504         map.put(5, action.isOFPATPUSHVLAN());
505         map.put(6, action.isOFPATPOPVLAN());
506         map.put(7, action.isOFPATPUSHMPLS());
507         map.put(8, action.isOFPATPOPMPLS());
508         map.put(9, action.isOFPATSETQUEUE());
509         map.put(10, action.isOFPATGROUP());
510         map.put(11, action.isOFPATSETNWTTL());
511         map.put(12, action.isOFPATDECNWTTL());
512         map.put(13, action.isOFPATSETFIELD());
513         map.put(14, action.isOFPATPUSHPBB());
514         map.put(15, action.isOFPATPOPPBB());
515         map.put(16, action.isOFPATEXPERIMENTER());
516         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
517         outBuffer.writeInt(bitmap);
518     }
519
520     private void writeGroupCapabilities(final GroupCapabilities capabilities, final ByteBuf outBuffer) {
521         Map<Integer, Boolean> map = new HashMap<>();
522         map.put(0, capabilities.isOFPGFCSELECTWEIGHT());
523         map.put(1, capabilities.isOFPGFCSELECTLIVENESS());
524         map.put(2, capabilities.isOFPGFCCHAINING());
525         map.put(3, capabilities.isOFPGFCCHAININGCHECKS());
526         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
527         outBuffer.writeInt(bitmap);
528     }
529
530     private void writeGroupTypes(final GroupTypes types, final ByteBuf outBuffer) {
531         Map<Integer, Boolean> map = new HashMap<>();
532         map.put(0, types.isOFPGTALL());
533         map.put(1, types.isOFPGTSELECT());
534         map.put(2, types.isOFPGTINDIRECT());
535         map.put(3, types.isOFPGTFF());
536         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
537         outBuffer.writeInt(bitmap);
538     }
539
540     private void serializeGroupDescBody(final MultipartReplyBody body, final ByteBuf outBuffer,
541             final MultipartReplyMessage message) {
542         MultipartReplyGroupDescCase groupDescCase = (MultipartReplyGroupDescCase) body;
543         MultipartReplyGroupDesc group = groupDescCase.getMultipartReplyGroupDesc();
544         for (GroupDesc groupDesc : group.getGroupDesc()) {
545             ByteBuf groupDescBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
546             groupDescBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
547             groupDescBuff.writeByte(groupDesc.getType().getIntValue());
548             groupDescBuff.writeZero(GROUP_DESC_PADDING);
549             groupDescBuff.writeInt(groupDesc.getGroupId().getValue().intValue());
550             for (BucketsList bucket : groupDesc.getBucketsList()) {
551                 ByteBuf bucketBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
552                 bucketBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
553                 bucketBuff.writeShort(bucket.getWeight().toJava());
554                 bucketBuff.writeInt(bucket.getWatchPort().getValue().intValue());
555                 bucketBuff.writeInt(bucket.getWatchGroup().intValue());
556                 bucketBuff.writeZero(BUCKET_PADDING);
557                 ListSerializer.serializeList(bucket.getAction(),
558                         TypeKeyMakerFactory.createActionKeyMaker(message.getVersion().toJava()), registry, bucketBuff);
559                 bucketBuff.setShort(BUCKET_LENGTH_INDEX, bucketBuff.readableBytes());
560                 groupDescBuff.writeBytes(bucketBuff);
561             }
562             groupDescBuff.setShort(GROUP_DESC_LENGTH_INDEX, groupDescBuff.readableBytes());
563             outBuffer.writeBytes(groupDescBuff);
564         }
565     }
566
567     private void serializeGroupBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
568         MultipartReplyGroupCase groupCase = (MultipartReplyGroupCase) body;
569         MultipartReplyGroup group = groupCase.getMultipartReplyGroup();
570         for (GroupStats groupStats : group.getGroupStats()) {
571             ByteBuf groupStatsBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
572             groupStatsBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
573             groupStatsBuff.writeZero(GROUP_STATS_PADDING_1);
574             groupStatsBuff.writeInt(groupStats.getGroupId().getValue().intValue());
575             groupStatsBuff.writeInt(groupStats.getRefCount().intValue());
576             groupStatsBuff.writeZero(GROUP_STATS_PADDING_2);
577             groupStatsBuff.writeLong(groupStats.getPacketCount().longValue());
578             groupStatsBuff.writeLong(groupStats.getByteCount().longValue());
579             groupStatsBuff.writeInt(groupStats.getDurationSec().intValue());
580             groupStatsBuff.writeInt(groupStats.getDurationNsec().intValue());
581             for (BucketStats bucketStats : groupStats.getBucketStats()) {
582                 groupStatsBuff.writeLong(bucketStats.getPacketCount().longValue());
583                 groupStatsBuff.writeLong(bucketStats.getByteCount().longValue());
584             }
585             groupStatsBuff.setShort(GROUP_STATS_LENGTH_INDEX, groupStatsBuff.readableBytes());
586             outBuffer.writeBytes(groupStatsBuff);
587         }
588     }
589
590     private void serializeQueueBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
591         MultipartReplyQueueCase queueCase = (MultipartReplyQueueCase) body;
592         MultipartReplyQueue queue = queueCase.getMultipartReplyQueue();
593         for (QueueStats queueStats : queue.getQueueStats()) {
594             outBuffer.writeInt(queueStats.getPortNo().intValue());
595             outBuffer.writeInt(queueStats.getQueueId().intValue());
596             outBuffer.writeLong(queueStats.getTxBytes().longValue());
597             outBuffer.writeLong(queueStats.getTxPackets().longValue());
598             outBuffer.writeLong(queueStats.getTxErrors().longValue());
599             outBuffer.writeInt(queueStats.getDurationSec().intValue());
600             outBuffer.writeInt(queueStats.getDurationNsec().intValue());
601         }
602     }
603
604     private void serializePortStatsBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
605         MultipartReplyPortStatsCase portStatsCase = (MultipartReplyPortStatsCase) body;
606         MultipartReplyPortStats portStats = portStatsCase.getMultipartReplyPortStats();
607         for (PortStats portStat : portStats.getPortStats()) {
608             outBuffer.writeInt(portStat.getPortNo().intValue());
609             outBuffer.writeZero(PORT_STATS_PADDING);
610             outBuffer.writeLong(portStat.getRxPackets().longValue());
611             outBuffer.writeLong(portStat.getTxPackets().longValue());
612             outBuffer.writeLong(portStat.getRxBytes().longValue());
613             outBuffer.writeLong(portStat.getTxBytes().longValue());
614             outBuffer.writeLong(portStat.getRxDropped().longValue());
615             outBuffer.writeLong(portStat.getTxDropped().longValue());
616             outBuffer.writeLong(portStat.getRxErrors().longValue());
617             outBuffer.writeLong(portStat.getTxErrors().longValue());
618             outBuffer.writeLong(portStat.getRxFrameErr().longValue());
619             outBuffer.writeLong(portStat.getRxOverErr().longValue());
620             outBuffer.writeLong(portStat.getRxCrcErr().longValue());
621             outBuffer.writeLong(portStat.getCollisions().longValue());
622             outBuffer.writeInt(portStat.getDurationSec().intValue());
623             outBuffer.writeInt(portStat.getDurationNsec().intValue());
624         }
625     }
626
627     private void serializeTableBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
628         MultipartReplyTableCase tableCase = (MultipartReplyTableCase) body;
629         MultipartReplyTable table = tableCase.getMultipartReplyTable();
630         for (TableStats tableStats : table.getTableStats()) {
631             outBuffer.writeByte(tableStats.getTableId().toJava());
632             outBuffer.writeZero(TABLE_PADDING);
633             outBuffer.writeInt(tableStats.getActiveCount().intValue());
634             outBuffer.writeLong(tableStats.getLookupCount().longValue());
635             outBuffer.writeLong(tableStats.getMatchedCount().longValue());
636         }
637     }
638
639     private void serializeAggregateBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
640         MultipartReplyAggregateCase aggregateCase = (MultipartReplyAggregateCase) body;
641         MultipartReplyAggregate aggregate = aggregateCase.getMultipartReplyAggregate();
642         outBuffer.writeLong(aggregate.getPacketCount().longValue());
643         outBuffer.writeLong(aggregate.getByteCount().longValue());
644         outBuffer.writeInt(aggregate.getFlowCount().intValue());
645         outBuffer.writeZero(AGGREGATE_PADDING);
646     }
647
648     private void serializeFlowBody(final MultipartReplyBody body, final ByteBuf outBuffer,
649             final MultipartReplyMessage message) {
650         MultipartReplyFlowCase flowCase = (MultipartReplyFlowCase) body;
651         MultipartReplyFlow flow = flowCase.getMultipartReplyFlow();
652         for (FlowStats flowStats : flow.getFlowStats()) {
653             ByteBuf flowStatsBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
654             flowStatsBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
655             flowStatsBuff.writeByte((byte)flowStats.getTableId().longValue());
656             flowStatsBuff.writeZero(FLOW_STATS_PADDING_1);
657             flowStatsBuff.writeInt(flowStats.getDurationSec().intValue());
658             flowStatsBuff.writeInt(flowStats.getDurationNsec().intValue());
659             flowStatsBuff.writeShort(flowStats.getPriority().toJava());
660             flowStatsBuff.writeShort(flowStats.getIdleTimeout().toJava());
661             flowStatsBuff.writeShort(flowStats.getHardTimeout().toJava());
662             flowStatsBuff.writeZero(FLOW_STATS_PADDING_2);
663             flowStatsBuff.writeLong(flowStats.getCookie().longValue());
664             flowStatsBuff.writeLong(flowStats.getPacketCount().longValue());
665             flowStatsBuff.writeLong(flowStats.getByteCount().longValue());
666             OFSerializer<Match> matchSerializer = registry.getSerializer(
667                     new MessageTypeKey<>(message.getVersion().toJava(), Match.class));
668             matchSerializer.serialize(flowStats.getMatch(), flowStatsBuff);
669             ListSerializer.serializeList(flowStats.getInstruction(),
670                     TypeKeyMakerFactory.createInstructionKeyMaker(message.getVersion().toJava()), registry,
671                     flowStatsBuff);
672
673             flowStatsBuff.setShort(FLOW_STATS_LENGTH_INDEX, flowStatsBuff.readableBytes());
674             outBuffer.writeBytes(flowStatsBuff);
675         }
676     }
677
678     private void serializeDescBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
679         MultipartReplyDescCase descCase = (MultipartReplyDescCase) body;
680         MultipartReplyDesc desc = descCase.getMultipartReplyDesc();
681         write256String(desc.getMfrDesc(), outBuffer);
682         write256String(desc.getHwDesc(), outBuffer);
683         write256String(desc.getSwDesc(), outBuffer);
684         write32String(desc.getSerialNum(), outBuffer);
685         write256String(desc.getDpDesc(), outBuffer);
686     }
687
688     private void write256String(final String toWrite, final ByteBuf outBuffer) {
689         byte[] nameBytes = toWrite.getBytes(StandardCharsets.UTF_8);
690         if (nameBytes.length < 256) {
691             byte[] nameBytesPadding = new byte[256];
692             int index = 0;
693             for (byte b : nameBytes) {
694                 nameBytesPadding[index] = b;
695                 index++;
696             }
697             for (; index < 256; index++) {
698                 nameBytesPadding[index] = 0x0;
699             }
700             outBuffer.writeBytes(nameBytesPadding);
701         } else {
702             outBuffer.writeBytes(nameBytes);
703         }
704     }
705
706     private void write32String(final String toWrite, final ByteBuf outBuffer) {
707         byte[] nameBytes = toWrite.getBytes(StandardCharsets.UTF_8);
708         if (nameBytes.length < 32) {
709             byte[] nameBytesPadding = new byte[32];
710             int index = 0;
711             for (byte b : nameBytes) {
712                 nameBytesPadding[index] = b;
713                 index++;
714             }
715             for (; index < 32; index++) {
716                 nameBytesPadding[index] = 0x0;
717             }
718             outBuffer.writeBytes(nameBytesPadding);
719         } else {
720             outBuffer.writeBytes(nameBytes);
721         }
722     }
723
724     private void serializePortDescBody(final MultipartReplyBody body, final ByteBuf outBuffer) {
725         MultipartReplyPortDescCase portCase = (MultipartReplyPortDescCase) body;
726         MultipartReplyPortDesc portDesc = portCase.getMultipartReplyPortDesc();
727         for (Ports port : portDesc.getPorts()) {
728             outBuffer.writeInt(port.getPortNo().intValue()); // Assuming PortNo
729                                                              // = PortId
730             outBuffer.writeZero(PORT_DESC_PADDING_1);
731             outBuffer.writeBytes(IetfYangUtil.INSTANCE.macAddressBytes(port.getHwAddr()));
732             outBuffer.writeZero(PORT_DESC_PADDING_2);
733             writeName(port.getName(), outBuffer);
734             writePortConfig(port.getConfig(), outBuffer);
735             writePortState(port.getState(), outBuffer);
736             writePortFeatures(port.getCurrentFeatures(), outBuffer);
737             writePortFeatures(port.getAdvertisedFeatures(), outBuffer);
738             writePortFeatures(port.getSupportedFeatures(), outBuffer);
739             writePortFeatures(port.getPeerFeatures(), outBuffer);
740             outBuffer.writeInt(port.getCurrSpeed().intValue());
741             outBuffer.writeInt(port.getMaxSpeed().intValue());
742         }
743     }
744
745     private void writeName(final String name, final ByteBuf outBuffer) {
746         byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
747         if (nameBytes.length < 16) {
748             byte[] nameBytesPadding = new byte[16];
749             int index = 0;
750             for (byte b : nameBytes) {
751                 nameBytesPadding[index] = b;
752                 index++;
753             }
754             for (; index < 16; index++) {
755                 nameBytesPadding[index] = 0x0;
756             }
757             outBuffer.writeBytes(nameBytesPadding);
758         } else {
759             outBuffer.writeBytes(nameBytes);
760         }
761
762     }
763
764     private void writePortConfig(final PortConfig config, final ByteBuf outBuffer) {
765         Map<Integer, Boolean> map = new HashMap<>();
766         map.put(0, config.isPortDown());
767         map.put(2, config.isNoRecv());
768         map.put(5, config.isNoFwd());
769         map.put(6, config.isNoPacketIn());
770         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
771         outBuffer.writeInt(bitmap);
772     }
773
774     private void writePortState(final PortState state, final ByteBuf outBuffer) {
775         Map<Integer, Boolean> map = new HashMap<>();
776         map.put(0, state.isLinkDown());
777         map.put(1, state.isBlocked());
778         map.put(2, state.isLive());
779         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
780         outBuffer.writeInt(bitmap);
781     }
782
783     private void writePortFeatures(final PortFeatures features, final ByteBuf outBuffer) {
784         Map<Integer, Boolean> map = new HashMap<>();
785         map.put(0, features.is_10mbHd());
786         map.put(1, features.is_10mbFd());
787         map.put(2, features.is_100mbHd());
788         map.put(3, features.is_100mbFd());
789         map.put(4, features.is_1gbHd());
790         map.put(5, features.is_1gbFd());
791         map.put(6, features.is_10gbFd());
792         map.put(7, features.is_40gbFd());
793         map.put(8, features.is_100gbFd());
794         map.put(9, features.is_1tbFd());
795         map.put(10, features.isOther());
796         map.put(11, features.isCopper());
797         map.put(12, features.isFiber());
798         map.put(13, features.isAutoneg());
799         map.put(14, features.isPause());
800         map.put(15, features.isPauseAsym());
801         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
802         outBuffer.writeInt(bitmap);
803     }
804 }