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.core.ApplicationId; |
| 19 | import org.onosproject.grpc.net.meter.models.MeterEnumsProto; |
| 20 | import org.onosproject.grpc.net.meter.models.MeterRequestProtoOuterClass; |
| 21 | import org.onosproject.incubator.protobuf.models.core.ApplicationIdProtoTranslator; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.meter.Band; |
| 24 | import org.onosproject.net.meter.DefaultMeterRequest; |
| 25 | import org.onosproject.net.meter.Meter; |
| 26 | import org.onosproject.net.meter.MeterRequest; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.util.Collection; |
| 31 | import java.util.Optional; |
| 32 | |
| 33 | /** |
| 34 | * gRPC message conversion related utilities for meter request service. |
| 35 | */ |
| 36 | public final class MeterRequestProtoTranslator { |
| 37 | |
| 38 | private static final Logger log = LoggerFactory.getLogger(MeterRequestProtoTranslator.class); |
| 39 | |
| 40 | /** |
| 41 | * Translates gRPC MeterRequestType to {@link MeterRequest.Type}. |
| 42 | * |
| 43 | * @param type gRPC message |
| 44 | * @return {@link MeterRequest.Type} |
| 45 | */ |
| 46 | public static Optional<Object> translate(MeterEnumsProto.MeterRequestTypeProto type) { |
| 47 | switch (type) { |
| 48 | case ADD: |
| 49 | return Optional.of(MeterRequest.Type.ADD); |
| 50 | case MODIFY: |
| 51 | return Optional.of(MeterRequest.Type.MODIFY); |
| 52 | case REMOVE: |
| 53 | return Optional.of(MeterRequest.Type.REMOVE); |
| 54 | default: |
| 55 | log.warn("Unrecognized MeterRequest type gRPC message: {}", type); |
| 56 | return Optional.empty(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Translates gRPC MeterRequest to {@link MeterRequest}. |
| 62 | * |
| 63 | * @param meterRequest gRPC message |
| 64 | * @return {@link MeterRequest} |
| 65 | */ |
| 66 | public static MeterRequest translate(MeterRequestProtoOuterClass.MeterRequestProto meterRequest) { |
| 67 | |
| 68 | DeviceId deviceid = DeviceId.deviceId(meterRequest.getDeviceId()); |
| 69 | ApplicationId appId = ApplicationIdProtoTranslator.translate(meterRequest.getApplicationId()); |
Jian Li | 1aaa64d | 2017-12-14 11:25:39 +0900 | [diff] [blame] | 70 | Meter.Unit unit = MeterEnumsProtoTranslator.translate(meterRequest.getUnit()).get(); |
Jian Li | 40f4d6c | 2017-12-14 00:07:21 +0900 | [diff] [blame] | 71 | boolean burst = meterRequest.getIsBurst(); |
| 72 | Collection<Band> bands = BandProtoTranslator.translate(meterRequest.getBandsList()); |
| 73 | MeterRequest.Type type = (MeterRequest.Type) translate(meterRequest.getType()).get(); |
| 74 | if (type == MeterRequest.Type.ADD) { |
| 75 | return DefaultMeterRequest.builder() |
| 76 | .forDevice(deviceid) |
| 77 | .fromApp(appId) |
| 78 | .withUnit(unit) |
| 79 | .withBands(bands) |
| 80 | .add(); |
| 81 | } else { |
| 82 | return DefaultMeterRequest.builder() |
| 83 | .forDevice(deviceid) |
| 84 | .fromApp(appId) |
| 85 | .withUnit(unit) |
| 86 | .withBands(bands) |
| 87 | .remove(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Utility class not intended for instantiation. |
| 92 | private MeterRequestProtoTranslator() {} |
| 93 | } |