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 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.util.EncodeConstants;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.openflowjava.util.ExperimenterSerializerKeyFactory;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ExperimenterIdMeterBand;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterMeterBandSubType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterBandCommons;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.MeterBand;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDropCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDscpRemarkCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandExperimenterCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.drop._case.MeterBandDrop;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.dscp.remark._case.MeterBandDscpRemark;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.experimenter._case.MeterBandExperimenter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.Bands;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * 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 =
89 experimenterBand.augmentation(ExperimenterIdMeterBand.class);
90 if (expIdMeterBand != null) {
91 long expId = expIdMeterBand.getExperimenter().getValue();
92 Class<? extends ExperimenterMeterBandSubType> meterBandSubType = expIdMeterBand.getSubType();
94 OFSerializer<MeterBandExperimenterCase> serializer = registry.getSerializer(
95 ExperimenterSerializerKeyFactory.createMeterBandSerializerKey(
96 EncodeConstants.OF13_VERSION_ID, expId, meterBandSubType));
97 serializer.serialize(experimenterBandCase, outBuffer);
98 } catch (final IllegalStateException e) {
99 LOG.warn("Serializer for key: {} wasn't found, exception {}",
100 ExperimenterSerializerKeyFactory.createMeterBandSerializerKey(
101 EncodeConstants.OF13_VERSION_ID, expId, meterBandSubType), e);
109 private static void writeBandCommonFields(final MeterBandCommons meterBand, final ByteBuf outBuffer) {
110 outBuffer.writeShort(meterBand.getType().getIntValue());
111 outBuffer.writeShort(LENGTH_OF_METER_BANDS);
112 outBuffer.writeInt(meterBand.getRate().intValue());
113 outBuffer.writeInt(meterBand.getBurstSize().intValue());
117 public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
118 registry = serializerRegistry;