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