Bug 2756 - Match model update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / MeterModInputMessageFactory.java
index 92219661ae8a8d38c1afe6905fc1af63b3645355..770c0cfd18fb7e226e9d248fdc12e4dfaf7bae43 100644 (file)
@@ -10,12 +10,15 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
 
 import io.netty.buffer.ByteBuf;
 
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
-import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
-import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+import org.opendaylight.openflowjava.util.ExperimenterSerializerKeyFactory;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ExperimenterIdMeterBand;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterBandCommons;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
@@ -33,65 +36,34 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
  * @author timotej.kubas
  * @author michal.polkorab
  */
-public class MeterModInputMessageFactory implements OFSerializer<MeterModInput> {
+public class MeterModInputMessageFactory implements OFSerializer<MeterModInput>,
+        SerializerRegistryInjector {
+
     private static final byte MESSAGE_TYPE = 29;
-    private static final int MESSAGE_LENGTH = 16;
     private static final short LENGTH_OF_METER_BANDS = 16;
     private static final short PADDING_IN_METER_BAND_DROP = 4;
     private static final short PADDING_IN_METER_BAND_DSCP_REMARK = 3;
-    private static MeterModInputMessageFactory instance;
-    
-    private MeterModInputMessageFactory() {
-        // singleton
-    }
-    
-    /**
-     * @return singleton factory
-     */
-    public static synchronized MeterModInputMessageFactory getInstance() {
-        if (instance == null) {
-            instance = new MeterModInputMessageFactory();
-        }
-        return instance;
-    }
-    
-    @Override
-    public void messageToBuffer(short version, ByteBuf out,
-            MeterModInput message) {
-        ByteBufUtils.writeOFHeader(instance, message, out);
-        out.writeShort(message.getCommand().getIntValue());
-        out.writeShort(createMeterFlagsBitmask(message.getFlags()));
-        out.writeInt(message.getMeterId().getValue().intValue());
-        encodeBands(message.getBands(), out);
-    }
+    private SerializerRegistry registry;
 
     @Override
-    public int computeLength(MeterModInput message) {
-        int length = MESSAGE_LENGTH;
-        if (message.getBands() != null) {
-            length += message.getBands().size() * LENGTH_OF_METER_BANDS;
-        }
-        return length;
+    public void serialize(final MeterModInput message, final ByteBuf outBuffer) {
+        ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
+        outBuffer.writeShort(message.getCommand().getIntValue());
+        outBuffer.writeShort(createMeterFlagsBitmask(message.getFlags()));
+        outBuffer.writeInt(message.getMeterId().getValue().intValue());
+        serializeBands(message.getBands(), outBuffer);
+        ByteBufUtils.updateOFHeaderLength(outBuffer);
     }
 
-    @Override
-    public byte getMessageType() {
-        return MESSAGE_TYPE;
+    private static int createMeterFlagsBitmask(final MeterFlags flags) {
+        return ByteBufUtils.fillBitMask(0,
+                flags.isOFPMFKBPS(),
+                flags.isOFPMFPKTPS(),
+                flags.isOFPMFBURST(),
+                flags.isOFPMFSTATS());
     }
 
-    private static int createMeterFlagsBitmask(MeterFlags flags) {
-        int meterFlagBitmask = 0;
-        Map<Integer, Boolean> meterModFlagsMap = new HashMap<>();
-        meterModFlagsMap.put(0, flags.isOFPMFKBPS());
-        meterModFlagsMap.put(1, flags.isOFPMFPKTPS());
-        meterModFlagsMap.put(2, flags.isOFPMFBURST());
-        meterModFlagsMap.put(3, flags.isOFPMFSTATS());
-        
-        meterFlagBitmask = ByteBufUtils.fillBitMaskFromMap(meterModFlagsMap);
-        return meterFlagBitmask;
-    }
-    
-    private static void encodeBands(List<Bands> bands, ByteBuf outBuffer) {
+    private void serializeBands(final List<Bands> bands, final ByteBuf outBuffer) {
         if (bands != null) {
             for (Bands currentBand : bands) {
                 MeterBand meterBand = currentBand.getMeterBand();
@@ -99,28 +71,36 @@ public class MeterModInputMessageFactory implements OFSerializer<MeterModInput>
                     MeterBandDropCase dropBandCase = (MeterBandDropCase) meterBand;
                     MeterBandDrop dropBand = dropBandCase.getMeterBandDrop();
                     writeBandCommonFields(dropBand, outBuffer);
-                    ByteBufUtils.padBuffer(PADDING_IN_METER_BAND_DROP, outBuffer);
+                    outBuffer.writeZero(PADDING_IN_METER_BAND_DROP);
                 } else if (meterBand instanceof MeterBandDscpRemarkCase) {
                     MeterBandDscpRemarkCase dscpRemarkBandCase = (MeterBandDscpRemarkCase) meterBand;
                     MeterBandDscpRemark dscpRemarkBand = dscpRemarkBandCase.getMeterBandDscpRemark();
                     writeBandCommonFields(dscpRemarkBand, outBuffer);
                     outBuffer.writeByte(dscpRemarkBand.getPrecLevel());
-                    ByteBufUtils.padBuffer(PADDING_IN_METER_BAND_DSCP_REMARK, outBuffer);
+                    outBuffer.writeZero(PADDING_IN_METER_BAND_DSCP_REMARK);
                 } else if (meterBand instanceof MeterBandExperimenterCase) {
                     MeterBandExperimenterCase experimenterBandCase = (MeterBandExperimenterCase) meterBand;
                     MeterBandExperimenter experimenterBand = experimenterBandCase.getMeterBandExperimenter();
-                    writeBandCommonFields(experimenterBand, outBuffer);
-                    outBuffer.writeInt(experimenterBand.getExperimenter().intValue());
+                    long expId = experimenterBand.getAugmentation(ExperimenterIdMeterBand.class)
+                            .getExperimenter().getValue();
+                    OFSerializer<MeterBandExperimenterCase> serializer = registry.getSerializer(
+                            ExperimenterSerializerKeyFactory.createMeterBandSerializerKey(
+                                    EncodeConstants.OF13_VERSION_ID, expId));
+                    serializer.serialize(experimenterBandCase, outBuffer);
                 }
             }
         }
     }
-    
-    private static void writeBandCommonFields(MeterBandCommons meterBand, ByteBuf outBuffer) {
+
+    private static void writeBandCommonFields(final MeterBandCommons meterBand, final ByteBuf outBuffer) {
         outBuffer.writeShort(meterBand.getType().getIntValue());
         outBuffer.writeShort(LENGTH_OF_METER_BANDS);
         outBuffer.writeInt(meterBand.getRate().intValue());
         outBuffer.writeInt(meterBand.getBurstSize().intValue());
     }
-    
-}
+
+    @Override
+    public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
+        registry = serializerRegistry;
+    }
+}
\ No newline at end of file