2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.openflowjava.util.ByteBufUtils;
17 import org.opendaylight.openflowjava.util.ExperimenterSerializerKeyFactory;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ExperimenterIdMeterBand;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterMeterBandSubType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterBandCommons;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.MeterBand;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDropCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDscpRemarkCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandExperimenterCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.drop._case.MeterBandDrop;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.dscp.remark._case.MeterBandDscpRemark;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.experimenter._case.MeterBandExperimenter;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.Bands;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import java.util.List;
37 * Translates MeterMod messages
38 * @author timotej.kubas
39 * @author michal.polkorab
41 public class MeterModInputMessageFactory implements OFSerializer<MeterModInput>,
42 SerializerRegistryInjector {
44 private static final Logger LOG = LoggerFactory
45 .getLogger(MeterModInputMessageFactory.class);
46 private static final byte MESSAGE_TYPE = 29;
47 private static final short LENGTH_OF_METER_BANDS = 16;
48 private static final short PADDING_IN_METER_BAND_DROP = 4;
49 private static final short PADDING_IN_METER_BAND_DSCP_REMARK = 3;
50 private SerializerRegistry registry;
53 public void serialize(final MeterModInput message, final ByteBuf outBuffer) {
54 ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
55 outBuffer.writeShort(message.getCommand().getIntValue());
56 outBuffer.writeShort(createMeterFlagsBitmask(message.getFlags()));
57 outBuffer.writeInt(message.getMeterId().getValue().intValue());
58 serializeBands(message.getBands(), outBuffer);
59 ByteBufUtils.updateOFHeaderLength(outBuffer);
62 private static int createMeterFlagsBitmask(final MeterFlags flags) {
63 return ByteBufUtils.fillBitMask(0,
67 flags.isOFPMFSTATS());
70 private void serializeBands(final List<Bands> bands, final ByteBuf outBuffer) {
72 for (Bands currentBand : bands) {
73 MeterBand meterBand = currentBand.getMeterBand();
74 if (meterBand instanceof MeterBandDropCase) {
75 MeterBandDropCase dropBandCase = (MeterBandDropCase) meterBand;
76 MeterBandDrop dropBand = dropBandCase.getMeterBandDrop();
77 writeBandCommonFields(dropBand, outBuffer);
78 outBuffer.writeZero(PADDING_IN_METER_BAND_DROP);
79 } else if (meterBand instanceof MeterBandDscpRemarkCase) {
80 MeterBandDscpRemarkCase dscpRemarkBandCase = (MeterBandDscpRemarkCase) meterBand;
81 MeterBandDscpRemark dscpRemarkBand = dscpRemarkBandCase.getMeterBandDscpRemark();
82 writeBandCommonFields(dscpRemarkBand, outBuffer);
83 outBuffer.writeByte(dscpRemarkBand.getPrecLevel());
84 outBuffer.writeZero(PADDING_IN_METER_BAND_DSCP_REMARK);
85 } else if (meterBand instanceof MeterBandExperimenterCase) {
86 MeterBandExperimenterCase experimenterBandCase = (MeterBandExperimenterCase) meterBand;
87 MeterBandExperimenter experimenterBand = experimenterBandCase.getMeterBandExperimenter();
88 ExperimenterIdMeterBand expIdMeterBand = experimenterBand.getAugmentation(ExperimenterIdMeterBand.class);
89 if (expIdMeterBand != null) {
90 long expId = expIdMeterBand.getExperimenter().getValue();
91 Class<? extends ExperimenterMeterBandSubType> meterBandSubType = expIdMeterBand.getSubType();
93 OFSerializer<MeterBandExperimenterCase> serializer = registry.getSerializer(
94 ExperimenterSerializerKeyFactory.createMeterBandSerializerKey(
95 EncodeConstants.OF13_VERSION_ID, expId, meterBandSubType));
96 serializer.serialize(experimenterBandCase, outBuffer);
97 } catch (final IllegalStateException e) {
98 LOG.warn("Serializer for key: {} wasn't found, exception {}", ExperimenterSerializerKeyFactory.createMeterBandSerializerKey(
99 EncodeConstants.OF13_VERSION_ID, expId, meterBandSubType), e);
107 private static void writeBandCommonFields(final MeterBandCommons meterBand, final ByteBuf outBuffer) {
108 outBuffer.writeShort(meterBand.getType().getIntValue());
109 outBuffer.writeShort(LENGTH_OF_METER_BANDS);
110 outBuffer.writeInt(meterBand.getRate().intValue());
111 outBuffer.writeInt(meterBand.getBurstSize().intValue());
115 public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
116 registry = serializerRegistry;