Jian Li | ec80a33 | 2017-11-17 10:54:23 +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.cluster; |
| 17 | |
| 18 | import org.onosproject.cluster.NodeId; |
| 19 | import org.onosproject.grpc.net.cluster.models.NodeIdProtoOuterClass; |
| 20 | |
| 21 | /** |
| 22 | * gRPC NodeIdProto message to equivalent ONOS NodeId conversion related utilities. |
| 23 | */ |
| 24 | public final class NodeIdProtoTranslator { |
| 25 | |
| 26 | /** |
| 27 | * Translates gRPC NodeId to {@link NodeId}. |
| 28 | * |
| 29 | * @param nodeId gRPC message |
| 30 | * @return {@link NodeId} |
| 31 | */ |
| 32 | public static NodeId translate(NodeIdProtoOuterClass.NodeIdProto nodeId) { |
| 33 | if (nodeId.equals(NodeIdProtoOuterClass.NodeIdProto.getDefaultInstance())) { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | return NodeId.nodeId(nodeId.getNodeId()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Translates {@link NodeId} to gRPC NodeId message. |
| 42 | * |
| 43 | * @param nodeId {@link NodeId} |
| 44 | * @return gRPC NodeId message |
| 45 | */ |
| 46 | public static NodeIdProtoOuterClass.NodeIdProto translate(NodeId nodeId) { |
| 47 | |
| 48 | if (nodeId != null) { |
| 49 | return NodeIdProtoOuterClass.NodeIdProto.newBuilder() |
| 50 | .setNodeId(nodeId.id()) |
| 51 | .build(); |
| 52 | } |
| 53 | |
| 54 | return NodeIdProtoOuterClass.NodeIdProto.getDefaultInstance(); |
| 55 | } |
| 56 | |
| 57 | // Utility class not intended for instantiation. |
| 58 | private NodeIdProtoTranslator() {} |
| 59 | } |