blob: 85f0a97d8d6ae8b1a3a395bd8584074ba44bf109 [file] [log] [blame]
zhiyong ke0c26c282017-07-07 08:58:19 +08001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jian Li4c703b92017-10-17 00:35:53 +090016package org.onosproject.incubator.protobuf.models.net.meter;
zhiyong ke0c26c282017-07-07 08:58:19 +080017
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21import org.onosproject.net.meter.Band;
22import org.onosproject.net.meter.MeterRequest;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.meter.DefaultBand;
25import org.onosproject.net.meter.DefaultMeterRequest;
26import org.onosproject.net.meter.Meter;
27import org.onosproject.net.meter.MeterState;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.DefaultApplicationId;
30import org.onosproject.grpc.core.models.ApplicationIdProtoOuterClass.ApplicationIdProto;
31import org.onosproject.grpc.net.meter.models.BandProtoOuterClass.BandProto;
32import org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterRequestTypeProto;
33import org.onosproject.grpc.net.meter.models.MeterRequestProtoOuterClass.MeterRequestProto;
34import org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterUnitProto;
35import org.onosproject.grpc.net.meter.models.BandEnumsProto.BandTypeProto;
36import org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterStateProto;
37import org.onosproject.grpc.net.meter.models.MeterProtoOuterClass.MeterProto;
38
39import java.util.ArrayList;
40import java.util.Collection;
41import java.util.stream.Collectors;
42import com.google.common.annotations.Beta;
43
44/**
45 * gRPC message conversion related utilities for meter service.
46 */
47@Beta
Jian Li4c703b92017-10-17 00:35:53 +090048public final class MeterProtoTranslator {
49 private static final Logger log = LoggerFactory.getLogger(MeterProtoTranslator.class);
zhiyong ke0c26c282017-07-07 08:58:19 +080050
51 /**
52 * Translates gRPC ApplicationId to {@link ApplicationId}.
53 *
54 * @param gAppId gRPC message
55 * @return {@link ApplicationId}
56 */
57 public static ApplicationId translate(ApplicationIdProto gAppId) {
58 int id = gAppId.getId();
59 String name = gAppId.getName();
60 ApplicationId appId = new DefaultApplicationId(id, name);
61 return appId;
62 }
63
64 /**
65 * Translates gRPC Band to {@link Band}.
66 *
67 * @param gBand gRPC message
68 * @return {@link Band}
69 */
70 public static Band translate(BandProto gBand) {
71 Band.Type type = translate(gBand.getType());
72 long rate = gBand.getRate();
73 long burstSize = gBand.getBurst();
74 short prec = (short) gBand.getDropPrecedence();
75 Band band = new DefaultBand(type, rate, burstSize, prec);
76 return band;
77 }
78
79 /**
80 * Translates gRPC List Bands to Collection Band.
81 *
82 * @param listBands gRPC message
83 * @return Collection Band
84 */
85 public static Collection<Band> translate(java.util.List<BandProto> listBands) {
86 Collection<Band> bands = new ArrayList<>();
87 listBands.forEach(d -> {
88 bands.add(translate(d));
89 });
90 return bands;
91 }
92
93 /**
94 * Translates gRPC MeterRequestType to {@link MeterRequest.Type}.
95 *
96 * @param type gRPC message
97 * @return {@link MeterRequest.Type}
98 */
99 public static MeterRequest.Type translate(MeterRequestTypeProto type) {
100 switch (type) {
101 case ADD:
102 return MeterRequest.Type.ADD;
103 case MODIFY:
104 return MeterRequest.Type.MODIFY;
105 case REMOVE:
106 return MeterRequest.Type.REMOVE;
107 case UNRECOGNIZED:
108 log.warn("Unrecognized MeterRequest type gRPC message: {}", type);
109 return null;
110 default:
111 log.warn("Unrecognized MeterRequest type gRPC message: {}", type);
112 return null;
113 }
114 }
115
116 /**
117 * Translates gRPC MeterRequest to {@link MeterRequest}.
118 *
119 * @param meterRequest gRPC message
120 * @return {@link MeterRequest}
121 */
122 public static MeterRequest translate(MeterRequestProto meterRequest) {
123
124 DeviceId deviceid = DeviceId.deviceId(meterRequest.getDeviceId());
125 ApplicationId appId = translate(meterRequest.getApplicationId());
126 Meter.Unit unit = translate(meterRequest.getUnit());
127 boolean burst = meterRequest.getIsBurst();
128 Collection<Band> bands = translate(meterRequest.getBandsList());
129 MeterRequest.Type type = translate(meterRequest.getType());
130 if (type == MeterRequest.Type.ADD) {
131 return DefaultMeterRequest.builder()
132 .forDevice(deviceid)
133 .fromApp(appId)
134 .withUnit(unit)
135 .withBands(bands)
136 .add();
137 } else {
138 return DefaultMeterRequest.builder()
139 .forDevice(deviceid)
140 .fromApp(appId)
141 .withUnit(unit)
142 .withBands(bands)
143 .remove();
144 }
145 }
146
147 /**
148 * Translates {@link ApplicationId} to gRPC ApplicationId message.
149 *
150 * @param appId {@link ApplicationId}
151 * @return gRPC ApplicationId message
152 */
153 public static ApplicationIdProto translate(ApplicationId appId) {
154 return ApplicationIdProto.newBuilder()
155 .setId(appId.id())
156 .setName(appId.name())
157 .build();
158 }
159
160 /**
161 * Translates gRPC enum MeterUnit to ONOS enum.
162 *
163 * @param unit meterUnit in gRPC enum
164 * @return equivalent in ONOS enum
165 */
166 public static Meter.Unit translate(MeterUnitProto unit) {
167 switch (unit) {
168 case PKTS_PER_SEC:
169 return Meter.Unit.PKTS_PER_SEC;
170 case KB_PER_SEC:
171 return Meter.Unit.KB_PER_SEC;
172 case UNRECOGNIZED:
173 log.warn("Unrecognized MeterUnit gRPC message: {}", unit);
174 return null;
175 default:
176 log.warn("Unrecognized MeterUnit gRPC message: {}", unit);
177 return null;
178 }
179 }
180
181 /**
182 * Translates ONOS enum Meter.Unit Type to gRPC enum.
183 *
184 * @param unit Meter.Unit in ONOS enum
185 * @return equivalent in gRPC enum
186 */
187 public static MeterUnitProto translate(Meter.Unit unit) {
188 switch (unit) {
189 case PKTS_PER_SEC:
190 return org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterUnitProto.PKTS_PER_SEC;
191 case KB_PER_SEC:
192 return org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterUnitProto.KB_PER_SEC;
193 default:
194 log.warn("Unrecognized MeterUnit ONOS message: {}", unit);
195 return org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterUnitProto.UNRECOGNIZED;
196 }
197 }
198
199 /**
200 * Translates gRPC enum Band Type to ONOS enum.
201 *
202 * @param bandType BandType in gRPC enum
203 * @return equivalent in ONOS enum
204 */
205 public static Band.Type translate(BandTypeProto bandType) {
206 switch (bandType) {
207 case DROP:
208 return Band.Type.DROP;
209 case REMARK:
210 return Band.Type.REMARK;
211 case EXPERIMENTAL:
212 return Band.Type.EXPERIMENTAL;
213 case UNRECOGNIZED:
214 log.warn("Unrecognized BandType gRPC message: {}", bandType);
215 return null;
216 default:
217 log.warn("Unrecognized BandType gRPC message: {}", bandType);
218 return null;
219 }
220 }
221
222 /**
223 * Translates ONOS enum Band Type to gRPC enum.
224 *
225 * @param bandType BandType in ONOS enum
226 * @return equivalent in gRPC enum
227 */
228 public static BandTypeProto translate(Band.Type bandType) {
229 switch (bandType) {
230 case DROP:
231 return org.onosproject.grpc.net.meter.models.BandEnumsProto.BandTypeProto.DROP;
232 case REMARK:
233 return org.onosproject.grpc.net.meter.models.BandEnumsProto.BandTypeProto.REMARK;
234 case EXPERIMENTAL:
235 return org.onosproject.grpc.net.meter.models.BandEnumsProto.BandTypeProto.EXPERIMENTAL;
236 default:
237 log.warn("Unrecognized BandType ONOS message: {}", bandType);
238 return null;
239 }
240 }
241
242 /**
243 * Translates ONOS enum MeterState Type to gRPC enum.
244 *
245 * @param meterState MeterState in ONOS enum
246 * @return equivalent in gRPC enum
247 */
248 public static MeterStateProto translate(MeterState meterState) {
249 switch (meterState) {
250 case PENDING_ADD:
251 return org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterStateProto.PENDING_ADD;
252 case ADDED:
253 return org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterStateProto.ADDED;
254 case PENDING_REMOVE:
255 return org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterStateProto.PENDING_REMOVE;
256 case REMOVED:
257 return org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterStateProto.REMOVED;
258 default:
259 log.warn("Unrecognized MeterState ONOS message: {}", meterState);
260 return org.onosproject.grpc.net.meter.models.MeterEnumsProto.MeterStateProto.UNRECOGNIZED;
261 }
262 }
263
264 /**
265 * Translates {@link Meter} to gRPC MeterCore message.
266 *
267 * @param meter {@link Meter}
268 * @return gRPC MeterCore message
269 */
270 public static MeterProto translate(Meter meter) {
271 return MeterProto.newBuilder()
272 .setDeviceId(meter.deviceId().toString())
273 .setApplicationId(translate(meter.appId()))
274 .setUnit(translate(meter.unit()))
275 .setIsBurst(meter.isBurst())
276 .addAllBands(meter.bands().stream()
277 .map(b -> BandProto.newBuilder()
278 .setRate(b.rate())
279 .setBurst(b.burst())
280 .setDropPrecedence(b.dropPrecedence())
281 .setType(translate(b.type()))
282 .setPackets(b.packets())
283 .setBytes(b.bytes())
284 .build())
285 .collect(Collectors.toList()))
286 .setState(translate(meter.state()))
287 .setLife(meter.life())
288 .setReferenceCount(meter.referenceCount())
289 .setPacketsSeen(meter.packetsSeen())
290 .setBytesSeen(meter.bytesSeen())
291 .build();
292 }
293
294 // Utility class not intended for instantiation.
Jian Li4c703b92017-10-17 00:35:53 +0900295 private MeterProtoTranslator() {
zhiyong ke0c26c282017-07-07 08:58:19 +0800296 }
297}