Fixed minor issue in decoding instruction from flow statistics response
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / MeterConvertor.java
1 /**
2  * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. 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  * Contributor: usha.m.s@ericsson.com
9  */
10 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11
12 /****
13  *
14  * This class is used for converting the data from SAL layer to OF Library Layer for Meter Mod Command.
15  *
16  */
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.meter.update.UpdatedMeter;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.Drop;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.DscpRemark;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.Experimenter;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.MeterBandHeaders;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.meter.band.headers.MeterBandHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterBandType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterModCommand;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.MeterBand;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDropCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDscpRemarkCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandExperimenterCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.drop._case.MeterBandDropBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.dscp.remark._case.MeterBandDscpRemarkBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.experimenter._case.MeterBandExperimenterBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.Bands;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.BandsBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public final class MeterConvertor {
47     private static final Logger logger = LoggerFactory.getLogger(MeterConvertor.class);
48     private static final String PREFIX_SEPARATOR = "/";
49
50     private MeterConvertor() {
51
52     }
53
54     // Get all the data for the meter from the Yang/SAL-Layer
55     /**
56      * @param version
57      * @param Yang
58      *            Data source
59      * @return MeterModInput required by OF Library
60      */
61     public static MeterModInputBuilder toMeterModInput(
62             org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter source, short version) {
63
64         MeterModInputBuilder meterModInputBuilder = new MeterModInputBuilder();
65         List<Bands> bands = new ArrayList<Bands>();       
66         
67         if (source instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput) {
68             meterModInputBuilder.setCommand(MeterModCommand.OFPMCADD);
69         } else if (source instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput) {
70             meterModInputBuilder.setCommand(MeterModCommand.OFPMCDELETE);
71         } else if (source instanceof UpdatedMeter) {
72             meterModInputBuilder.setCommand(MeterModCommand.OFPMCMODIFY);
73         }
74
75         meterModInputBuilder.setMeterId(new MeterId(source.getMeterId().getValue()));
76
77         if (null != source.getFlags()) {
78             meterModInputBuilder.setFlags(new MeterFlags(source.getFlags().isMeterBurst(), source.getFlags()
79                 .isMeterKbps(), source.getFlags().isMeterPktps(), source.getFlags().isMeterStats()));
80         } else {
81
82             /*
83              * As per 0F1.3.1,The rate field indicates the rate value above
84              * which the corresponding band may apply to packets (see 5.7.1).
85              * The rate value is in kilobit per seconds, unless the flags eld
86              * includes OFPMF_PKTPS, in which case the rate is in packets per
87              * seconds.
88              */
89
90             meterModInputBuilder.setFlags(new MeterFlags(false, false, true, false));
91         }
92         if (source.getMeterBandHeaders() != null) {
93             getBandsFromSAL(source.getMeterBandHeaders(), bands);
94             meterModInputBuilder.setBands(bands);
95         } else {
96             logger.error("For this meter Id" + source.getMeterId().getValue() + ",no associated band data found!");
97         }
98         
99         meterModInputBuilder.setVersion(version);
100         return meterModInputBuilder;
101     }
102
103     private static void getBandsFromSAL(MeterBandHeaders meterBandHeaders, List<Bands> bands) {
104
105         Iterator<MeterBandHeader> bandHeadersIterator = meterBandHeaders.getMeterBandHeader().iterator();
106         MeterBandHeader meterBandHeader;
107
108         BandsBuilder bandsB = null;
109
110         while (bandHeadersIterator.hasNext()) {
111             meterBandHeader = bandHeadersIterator.next();
112             MeterBand meterBandItem = null;
113             // The band types :drop,DSCP_Remark or experimenter.
114             if (null != meterBandHeader.getMeterBandTypes() && 
115                     null != meterBandHeader.getMeterBandTypes().getFlags()) {
116
117                 if (meterBandHeader.getMeterBandTypes().getFlags().isOfpmbtDrop()) {
118                     if (meterBandHeader.getBandType() != null) {
119                         MeterBandDropCaseBuilder dropCaseBuilder = new MeterBandDropCaseBuilder();
120                         MeterBandDropBuilder meterBandDropBuilder = new MeterBandDropBuilder();
121                         meterBandDropBuilder.setType(MeterBandType.OFPMBTDROP);
122                         Drop drop = (Drop) meterBandHeader.getBandType();
123                         meterBandDropBuilder.setBurstSize(drop.getDropRate());
124                         meterBandDropBuilder.setRate(drop.getDropBurstSize());
125                         dropCaseBuilder.setMeterBandDrop(meterBandDropBuilder.build());
126                         meterBandItem = dropCaseBuilder.build();
127                         bandsB = new BandsBuilder();
128                         bandsB.setMeterBand(meterBandItem);
129                         bands.add(bandsB.build()); // Bands list
130
131                     } else {
132                         logger.error("BandType: " + MeterBandType.OFPMBTDROP + "No Band Data found");
133                     }
134                 } else if (meterBandHeader.getMeterBandTypes().getFlags().isOfpmbtDscpRemark()) {
135                     if (meterBandHeader.getBandType() != null) {
136                         MeterBandDscpRemarkCaseBuilder dscpCaseBuilder = new MeterBandDscpRemarkCaseBuilder();
137                         MeterBandDscpRemarkBuilder meterBandDscpRemarkBuilder = new MeterBandDscpRemarkBuilder();
138                         meterBandDscpRemarkBuilder.setType(MeterBandType.OFPMBTDSCPREMARK);
139                         DscpRemark dscpRemark = (DscpRemark) meterBandHeader.getBandType();
140                         meterBandDscpRemarkBuilder.setBurstSize(dscpRemark.getDscpRemarkBurstSize());
141                         meterBandDscpRemarkBuilder.setRate(dscpRemark.getDscpRemarkRate());
142                         meterBandDscpRemarkBuilder.setPrecLevel(dscpRemark.getPercLevel());
143                         dscpCaseBuilder.setMeterBandDscpRemark(meterBandDscpRemarkBuilder.build());
144                         meterBandItem = dscpCaseBuilder.build();
145                         bandsB = new BandsBuilder();
146                         bandsB.setMeterBand(meterBandItem);
147                         bands.add(bandsB.build()); // Bands list
148
149                     } else {
150                         logger.error("BandType: " + MeterBandType.OFPMBTDSCPREMARK + "No Band Data found");
151                     }
152                 } else if (meterBandHeader.getMeterBandTypes().getFlags().isOfpmbtExperimenter()) {
153                     if (meterBandHeader.getBandType() != null) {
154                         MeterBandExperimenterCaseBuilder experimenterCaseBuilder = new MeterBandExperimenterCaseBuilder();
155                         MeterBandExperimenterBuilder meterBandExperimenterBuilder = new MeterBandExperimenterBuilder();
156                         meterBandExperimenterBuilder.setType(MeterBandType.OFPMBTEXPERIMENTER);
157                         Experimenter experimenter = (Experimenter) meterBandHeader.getBandType();
158                         meterBandExperimenterBuilder.setBurstSize(experimenter.getExperimenterBurstSize());
159                         meterBandExperimenterBuilder.setRate(experimenter.getExperimenterRate());
160                         meterBandExperimenterBuilder.setExperimenter(experimenter.getExperimenter());
161                         experimenterCaseBuilder.setMeterBandExperimenter(meterBandExperimenterBuilder.build());
162                         meterBandItem = experimenterCaseBuilder.build();
163                         bandsB = new BandsBuilder();
164                         bandsB.setMeterBand(meterBandItem);
165                         bands.add(bandsB.build()); // Bands list
166
167                     } else {
168                         logger.error("BandType: " + MeterBandType.OFPMBTEXPERIMENTER + "No Band Data found");
169                     }
170                 }
171
172             } else {
173                 logger.error("Invalid meter band data found.");
174             }
175         }
176
177     }
178
179 }