Convertor:MeterConverter in modeldrivenimpl corrected to MeterConvertor.
[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  * This class is used for converting the data from SAL layer to OF Library Layer for Meter Mod Command.
5  *
6  */
7
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterFlags.Flags;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.Drop;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.DscpRemark;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.Experimenter;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.MeterBandHeaders;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.meter.band.headers.MeterBandHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterBandType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterModCommand;
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.MeterModInputBuilder;
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.MeterBandDropBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDscpRemarkBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandExperimenterBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.Bands;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.BandsBuilder;
30 //import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.meter.band.headers.MeterBandHeaderBuilder;
31
32
33 public final class MeterConvertor {
34
35          private static List<Bands> bands;
36          private static MeterModInputBuilder meterModInputBuilder;
37          private static MeterFlags flags;
38          private MeterConvertor(){
39
40          }
41
42          //Get all the data for the meter from the Yang/SAL-Layer
43          /**
44          * @param Yang Data source
45          * @return MeterModInput required by OF Library
46          */
47         public static MeterModInput  toMeterModInput(org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter source ) {
48
49                  meterModInputBuilder = new MeterModInputBuilder();
50
51
52                  if(source instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput)
53                                  meterModInputBuilder.setCommand(MeterModCommand.OFPMCADD);
54                         if(source instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput)
55                                 meterModInputBuilder.setCommand(MeterModCommand.OFPMCDELETE);
56                         if(source instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput)
57                                 meterModInputBuilder.setCommand(MeterModCommand.OFPMCMODIFY);
58
59                         meterModInputBuilder.setMeterId(new MeterId(source.getMeterId().getValue()));
60
61                         getFlagsFromSAL(source.getFlags());
62                                 meterModInputBuilder.setFlags(flags);
63                         getBandsFromSAL(source.getMeterBandHeaders());
64                                  meterModInputBuilder.setBands(bands);
65                  return meterModInputBuilder.build();
66          }
67
68
69
70          private static void getBandsFromSAL(MeterBandHeaders meterBandHeaders) {
71
72                 Iterator <MeterBandHeader> bandHeadersIterator  = meterBandHeaders.getMeterBandHeader().iterator();
73                 MeterBandHeader meterBandHeader;
74
75                 BandsBuilder bandsB = null;
76                 //MeterBandHeaderBuilder meterBandHeaderBuilder = null ;
77                 bands = new ArrayList<Bands>();
78                 while(bandHeadersIterator.hasNext())
79                 {
80                                 meterBandHeader = bandHeadersIterator.next();
81                                 MeterBand meterBandItem = null;
82                                 //The band types :drop,DSCP_Remark or experimenter.
83                                 //meterBandHeaderBuilder = new MeterBandHeaderBuilder();
84
85
86                                 if(meterBandHeader.getMeterBandTypes().getFlags().isOfpmbtDrop()){
87
88                                         MeterBandDropBuilder meterBandDropBuilder = new MeterBandDropBuilder();
89                                         meterBandDropBuilder.setType(MeterBandType.OFPMBTDROP);
90
91                                         Drop drop = (Drop)meterBandHeader.getBandType();
92
93                                         meterBandDropBuilder.setBurstSize(drop.getRate());
94                                         meterBandDropBuilder.setRate(drop.getBurstSize());
95                                         meterBandItem= meterBandDropBuilder.build();
96
97                                 }
98                                 else if(meterBandHeader.getMeterBandTypes().getFlags().isOfpmbtDscpRemark())
99                                 {
100                                         MeterBandDscpRemarkBuilder meterBandDscpRemarkBuilder = new MeterBandDscpRemarkBuilder();
101                                         meterBandDscpRemarkBuilder.setType(MeterBandType.OFPMBTDSCPREMARK);
102
103                                         DscpRemark dscpRemark = (DscpRemark)meterBandHeader.getBandType();
104
105                                         meterBandDscpRemarkBuilder.setBurstSize(dscpRemark.getBurstSize());
106                                         meterBandDscpRemarkBuilder.setRate(dscpRemark.getRate());
107                                         meterBandDscpRemarkBuilder.setPrecLevel(dscpRemark.getPercLevel());
108                                         meterBandItem = meterBandDscpRemarkBuilder.build();
109
110                                 }
111                                 else if(meterBandHeader.getMeterBandTypes().getFlags().isOfpmbtExperimenter())
112                                 {
113                                         MeterBandExperimenterBuilder meterBandExperimenterBuilder = new MeterBandExperimenterBuilder();
114                                         meterBandExperimenterBuilder.setType(MeterBandType.OFPMBTEXPERIMENTER);
115                                         Experimenter experimenter = (Experimenter)meterBandHeader.getBandType();
116                                         meterBandExperimenterBuilder.setBurstSize(experimenter.getBurstSize());
117                                         meterBandExperimenterBuilder.setRate(experimenter.getRate());
118                                         meterBandExperimenterBuilder.setExperimenter(experimenter.getExperimenter());
119                                         meterBandItem = meterBandExperimenterBuilder.build();
120
121                                 }
122
123                                 bandsB = new BandsBuilder();
124                                 bandsB.setMeterBand(meterBandItem).build();
125
126                                 bands.add(bandsB.build()); //Bands list
127
128
129                         }
130
131         }
132
133          //get it from plugin(SAL) layer
134          private static void getFlagsFromSAL(Flags flags2) {
135                                 boolean meterBurst_SAL = false;
136                                 boolean meterKbps_SAL = false;
137                                 boolean meterPktps_SAL = false;
138                                 boolean meterStats_SAL = false;
139
140
141                                 if(flags2.isMeterBurst())       meterBurst_SAL = true;
142                                 if(flags2.isMeterKbps())  meterKbps_SAL = true;
143                                 if(flags2.isMeterPktps()) meterPktps_SAL = true;
144                                 if(flags2.isMeterStats()) meterStats_SAL = true;
145
146                                 flags = new MeterFlags(meterBurst_SAL,meterKbps_SAL,meterPktps_SAL,meterStats_SAL);
147
148
149                 }
150
151
152 }