Yang model fixed
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / MeterModInputMessageFactory.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
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
18 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterBandCommons;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.MeterBand;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDropCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDscpRemarkCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandExperimenterCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.drop._case.MeterBandDrop;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.dscp.remark._case.MeterBandDscpRemark;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.experimenter._case.MeterBandExperimenter;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.Bands;
30
31 /**
32  * Translates MeterMod messages
33  * @author timotej.kubas
34  * @author michal.polkorab
35  */
36 public class MeterModInputMessageFactory implements OFSerializer<MeterModInput> {
37     private static final byte MESSAGE_TYPE = 29;
38     private static final int MESSAGE_LENGTH = 16;
39     private static final short LENGTH_OF_METER_BANDS = 16;
40     private static final short PADDING_IN_METER_BAND_DROP = 4;
41     private static final short PADDING_IN_METER_BAND_DSCP_REMARK = 3;
42     private static MeterModInputMessageFactory instance;
43     
44     private MeterModInputMessageFactory() {
45         // singleton
46     }
47     
48     /**
49      * @return singleton factory
50      */
51     public static synchronized MeterModInputMessageFactory getInstance() {
52         if (instance == null) {
53             instance = new MeterModInputMessageFactory();
54         }
55         return instance;
56     }
57     
58     @Override
59     public void messageToBuffer(short version, ByteBuf out,
60             MeterModInput message) {
61         ByteBufUtils.writeOFHeader(instance, message, out);
62         out.writeShort(message.getCommand().getIntValue());
63         out.writeShort(createMeterFlagsBitmask(message.getFlags()));
64         out.writeInt(message.getMeterId().getValue().intValue());
65         encodeBands(message.getBands(), out);
66     }
67
68     @Override
69     public int computeLength(MeterModInput message) {
70         int length = MESSAGE_LENGTH;
71         if (message.getBands() != null) {
72             length += message.getBands().size() * LENGTH_OF_METER_BANDS;
73         }
74         return length;
75     }
76
77     @Override
78     public byte getMessageType() {
79         return MESSAGE_TYPE;
80     }
81
82     private static int createMeterFlagsBitmask(MeterFlags flags) {
83         int meterFlagBitmask = 0;
84         Map<Integer, Boolean> meterModFlagsMap = new HashMap<>();
85         meterModFlagsMap.put(0, flags.isOFPMFKBPS());
86         meterModFlagsMap.put(1, flags.isOFPMFPKTPS());
87         meterModFlagsMap.put(2, flags.isOFPMFBURST());
88         meterModFlagsMap.put(3, flags.isOFPMFSTATS());
89         
90         meterFlagBitmask = ByteBufUtils.fillBitMaskFromMap(meterModFlagsMap);
91         return meterFlagBitmask;
92     }
93     
94     private static void encodeBands(List<Bands> bands, ByteBuf outBuffer) {
95         if (bands != null) {
96             for (Bands currentBand : bands) {
97                 MeterBand meterBand = currentBand.getMeterBand();
98                 if (meterBand instanceof MeterBandDropCase) {
99                     MeterBandDropCase dropBandCase = (MeterBandDropCase) meterBand;
100                     MeterBandDrop dropBand = dropBandCase.getMeterBandDrop();
101                     writeBandCommonFields(dropBand, outBuffer);
102                     ByteBufUtils.padBuffer(PADDING_IN_METER_BAND_DROP, outBuffer);
103                 } else if (meterBand instanceof MeterBandDscpRemarkCase) {
104                     MeterBandDscpRemarkCase dscpRemarkBandCase = (MeterBandDscpRemarkCase) meterBand;
105                     MeterBandDscpRemark dscpRemarkBand = dscpRemarkBandCase.getMeterBandDscpRemark();
106                     writeBandCommonFields(dscpRemarkBand, outBuffer);
107                     outBuffer.writeByte(dscpRemarkBand.getPrecLevel());
108                     ByteBufUtils.padBuffer(PADDING_IN_METER_BAND_DSCP_REMARK, outBuffer);
109                 } else if (meterBand instanceof MeterBandExperimenterCase) {
110                     MeterBandExperimenterCase experimenterBandCase = (MeterBandExperimenterCase) meterBand;
111                     MeterBandExperimenter experimenterBand = experimenterBandCase.getMeterBandExperimenter();
112                     writeBandCommonFields(experimenterBand, outBuffer);
113                     outBuffer.writeInt(experimenterBand.getExperimenter().intValue());
114                 }
115             }
116         }
117     }
118     
119     private static void writeBandCommonFields(MeterBandCommons meterBand, ByteBuf outBuffer) {
120         outBuffer.writeShort(meterBand.getType().getIntValue());
121         outBuffer.writeShort(LENGTH_OF_METER_BANDS);
122         outBuffer.writeInt(meterBand.getRate().intValue());
123         outBuffer.writeInt(meterBand.getBurstSize().intValue());
124     }
125     
126 }