blob: f41586db0de3a479bb5d721db86d53b1f74310e1 [file] [log] [blame]
Jian Liec80a332017-11-17 10:54:23 +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.cluster;
17
18import org.onosproject.cluster.NodeId;
19import org.onosproject.grpc.net.cluster.models.NodeIdProtoOuterClass;
20
21/**
22 * gRPC NodeIdProto message to equivalent ONOS NodeId conversion related utilities.
23 */
24public 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}