Jian Li | 40f4d6c | 2017-12-14 00:07:21 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.incubator.protobuf.models.net.meter; |
| 17 | |
| 18 | import org.onosproject.grpc.net.meter.models.BandEnumsProto; |
| 19 | import org.onosproject.grpc.net.meter.models.BandProtoOuterClass.BandProto; |
| 20 | import org.onosproject.net.meter.Band; |
| 21 | import org.onosproject.net.meter.DefaultBand; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Collection; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * gRPC message conversion related utilities for band service. |
| 31 | */ |
| 32 | public 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 Li | 1aaa64d | 2017-12-14 11:25:39 +0900 | [diff] [blame] | 43 | Band.Type type = BandEnumsProtoTranslator.translate(gBand.getType()).get(); |
Jian Li | 40f4d6c | 2017-12-14 00:07:21 +0900 | [diff] [blame] | 44 | 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 | } |