blob: 098a276cb68842a19b6ffe9e21faccf76ebda7c7 [file] [log] [blame]
Frank Wangcfffbaa2017-05-06 16:11:08 +08001/*
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
17package org.onosproject.incubator.protobuf.models.net;
18
19import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass.ConnectPointProto;
20import org.onosproject.grpc.net.models.HostLocationProtoOuterClass.HostLocationProto;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.HostLocation;
23import org.onosproject.net.PortNumber;
24
25/**
26 * gRPC HostLocationProto message to equivalent ONOS Host location conversion related utilities.
27 */
28public final class HostLocationProtoTranslator {
29
30 /**
31 * Translates gRPC HostLocation to {@link HostLocation}.
32 *
33 * @param hostLocation gRPC message
34 * @return {@link HostLocation}
35 */
36 public static HostLocation translate(HostLocationProto hostLocation) {
37
38 if (hostLocation.equals(HostLocationProto.getDefaultInstance())) {
39 return null;
40 }
41
42 return new HostLocation(DeviceId.deviceId(hostLocation.getConnectPoint().getDeviceId()),
43 PortNumber.portNumber(hostLocation.getConnectPoint().getPortNumber()), 0L);
44 }
45
46 /**
47 * Translates {@link HostLocation} to gRPC HostLocation message.
48 *
49 * @param hostLocation {@link HostLocation}
50 * @return gRPC HostLocation message
51 */
52 public static HostLocationProto translate(HostLocation hostLocation) {
53
54 if (hostLocation != null) {
55 return HostLocationProto.newBuilder()
56 .setConnectPoint(ConnectPointProto.newBuilder()
57 .setDeviceId(hostLocation.deviceId().toString())
58 .setPortNumber(hostLocation.port().toString()))
59 .setTime(hostLocation.time())
60 .build();
61 }
62
63 return HostLocationProto.getDefaultInstance();
64 }
65
66 // Utility class not intended for instantiation.
67 private HostLocationProtoTranslator() {}
68}