blob: ba4460db03370e80e4ee00fcc04dfa61bbed8a5f [file] [log] [blame]
Jian Li40f4d6c2017-12-14 00:07:21 +09001/*
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 */
16package org.onosproject.incubator.protobuf.models.net.meter;
17
18import org.onosproject.grpc.net.meter.models.BandEnumsProto;
19import org.onosproject.grpc.net.meter.models.BandProtoOuterClass.BandProto;
20import org.onosproject.net.meter.Band;
21import org.onosproject.net.meter.DefaultBand;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import java.util.ArrayList;
26import java.util.Collection;
27import java.util.List;
28
29/**
30 * gRPC message conversion related utilities for band service.
31 */
32public final class BandProtoTranslator {
33
34 private static final Logger log = LoggerFactory.getLogger(BandProtoTranslator.class);
35
36 /**
37 * Translates gRPC Band to {@link Band}.
38 *
39 * @param gBand gRPC message
40 * @return {@link Band}
41 */
42 public static Band translate(BandProto gBand) {
Jian Li1aaa64d2017-12-14 11:25:39 +090043 Band.Type type = BandEnumsProtoTranslator.translate(gBand.getType()).get();
Jian Li40f4d6c2017-12-14 00:07:21 +090044 long rate = gBand.getRate();
45 long burstSize = gBand.getBurst();
46 short prec = (short) gBand.getDropPrecedence();
47 Band band = new DefaultBand(type, rate, burstSize, prec);
48 return band;
49 }
50
51 /**
52 * Translates gRPC List Bands to Collection Band.
53 *
54 * @param listBands gRPC message
55 * @return Collection Band
56 */
57 public static Collection<Band> translate(List<BandProto> listBands) {
58 Collection<Band> bands = new ArrayList<>();
59 listBands.forEach(d -> bands.add(translate(d)));
60 return bands;
61 }
62
63 /**
64 * Translates ONOS enum Band Type to gRPC enum.
65 *
66 * @param bandType BandType in ONOS enum
67 * @return equivalent in gRPC enum
68 */
69 public static BandEnumsProto.BandTypeProto translate(Band.Type bandType) {
70 switch (bandType) {
71 case DROP:
72 return BandEnumsProto.BandTypeProto.DROP;
73 case REMARK:
74 return BandEnumsProto.BandTypeProto.REMARK;
75 case EXPERIMENTAL:
76 return BandEnumsProto.BandTypeProto.EXPERIMENTAL;
77 default:
78 log.warn("Unrecognized BandType ONOS message: {}", bandType);
79 return BandEnumsProto.BandTypeProto.UNRECOGNIZED;
80 }
81 }
82
83 // Utility class not intended for instantiation.
84 private BandProtoTranslator() {}
85}