blob: b73117c0c59ef0a4434401e2f541c69ffb85adb0 [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.MeterEnumsProto;
19import org.onosproject.net.meter.Meter;
20import org.onosproject.net.meter.MeterState;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import java.util.Optional;
25
26/**
27 * gRPC message conversion related utilities for meter enums.
28 */
29public final class MeterEnumsProtoTranslator {
30
31 private static final Logger log = LoggerFactory.getLogger(MeterEnumsProtoTranslator.class);
32
33 /**
34 * Translates gRPC enum MeterUnit to ONOS enum.
35 *
36 * @param unit meterUnit in gRPC enum
37 * @return equivalent in ONOS enum
38 */
Jian Li1aaa64d2017-12-14 11:25:39 +090039 public static Optional<Meter.Unit> translate(MeterEnumsProto.MeterUnitProto unit) {
Jian Li40f4d6c2017-12-14 00:07:21 +090040 switch (unit) {
41 case PKTS_PER_SEC:
42 return Optional.of(Meter.Unit.PKTS_PER_SEC);
43 case KB_PER_SEC:
44 return Optional.of(Meter.Unit.KB_PER_SEC);
45 default:
46 log.warn("Unrecognized MeterUnit gRPC message: {}", unit);
47 return Optional.empty();
48 }
49 }
50
51 /**
52 * Translates ONOS enum Meter.Unit Type to gRPC enum.
53 *
54 * @param unit Meter.Unit in ONOS enum
55 * @return equivalent in gRPC enum
56 */
57 public static MeterEnumsProto.MeterUnitProto translate(Meter.Unit unit) {
58 switch (unit) {
59 case PKTS_PER_SEC:
60 return MeterEnumsProto.MeterUnitProto.PKTS_PER_SEC;
61 case KB_PER_SEC:
62 return MeterEnumsProto.MeterUnitProto.KB_PER_SEC;
63 default:
64 log.warn("Unrecognized MeterUnit ONOS message: {}", unit);
65 return MeterEnumsProto.MeterUnitProto.UNRECOGNIZED;
66 }
67 }
68
69 /**
70 * Translates ONOS enum MeterState Type to gRPC enum.
71 *
72 * @param meterState MeterState in ONOS enum
73 * @return equivalent in gRPC enum
74 */
75 public static MeterEnumsProto.MeterStateProto translate(MeterState meterState) {
76 switch (meterState) {
77 case PENDING_ADD:
78 return MeterEnumsProto.MeterStateProto.PENDING_ADD;
79 case ADDED:
80 return MeterEnumsProto.MeterStateProto.ADDED;
81 case PENDING_REMOVE:
82 return MeterEnumsProto.MeterStateProto.PENDING_REMOVE;
83 case REMOVED:
84 return MeterEnumsProto.MeterStateProto.REMOVED;
85 default:
86 log.warn("Unrecognized MeterState ONOS message: {}", meterState);
87 return MeterEnumsProto.MeterStateProto.UNRECOGNIZED;
88 }
89 }
90
91 // Utility class not intended for instantiation.
92 private MeterEnumsProtoTranslator() {}
93}