Frank Wang | caef314 | 2017-08-09 15:24:45 +0800 | [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 | |
| 17 | package org.onosproject.incubator.protobuf.models.cfg; |
| 18 | |
| 19 | import org.onosproject.cfg.ConfigProperty; |
| 20 | import org.onosproject.grpc.cfg.models.ConfigPropertyProtoOuterClass.ConfigPropertyProto; |
| 21 | |
| 22 | /** |
| 23 | * gRPC ConfigPropertyProto message to equivalent ONOS ConfigProperty conversion related utilities. |
| 24 | */ |
| 25 | public final class ConfigPropertyProtoTranslator { |
| 26 | |
| 27 | /** |
| 28 | * Translates gRPC ConfigProperty message to {@link ConfigProperty}. |
| 29 | * |
| 30 | * @param configPropertyProto gRPC message |
| 31 | * @return {@link ConfigProperty} |
| 32 | */ |
| 33 | public static ConfigProperty translate(ConfigPropertyProto configPropertyProto) { |
| 34 | |
| 35 | ConfigProperty configProperty = ConfigProperty.defineProperty(configPropertyProto.getName(), |
| 36 | ConfigPropertyEnumsProtoTranslator |
| 37 | .translate(configPropertyProto |
| 38 | .getType()), |
| 39 | configPropertyProto.getDefaultValue(), |
| 40 | configPropertyProto.getDescriptionBytes() |
| 41 | .toString()); |
| 42 | return ConfigProperty.setProperty(configProperty, configPropertyProto.getValue()); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Translates {@link ConfigProperty} to gRPC ConfigProperty message. |
| 47 | * |
| 48 | * @param configProperty config property |
| 49 | * @return gRPC ConfigProperty message |
| 50 | */ |
| 51 | public static ConfigPropertyProto translate(ConfigProperty configProperty) { |
| 52 | |
| 53 | if (configProperty != null) { |
| 54 | return ConfigPropertyProto.newBuilder() |
| 55 | .setName(configProperty.name()) |
| 56 | .setType(ConfigPropertyEnumsProtoTranslator.translate(configProperty.type())) |
| 57 | .setDefaultValue(configProperty.defaultValue()) |
| 58 | .setDescription(configProperty.description()) |
| 59 | .setValue(configProperty.value()) |
| 60 | .build(); |
| 61 | } |
| 62 | |
| 63 | return ConfigPropertyProto.getDefaultInstance(); |
| 64 | } |
| 65 | |
| 66 | // Utility class not intended for instantiation. |
| 67 | private ConfigPropertyProtoTranslator() {} |
| 68 | } |