Frank Wang | cfffbaa | 2017-05-06 16:11:08 +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.net; |
| 18 | |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onosproject.grpc.net.models.HostIdProtoOuterClass.HostIdProto; |
| 22 | import org.onosproject.net.HostId; |
| 23 | |
| 24 | /** |
| 25 | * gRPC HostIdProto message to equivalent ONOS HostId conversion related utilities. |
| 26 | */ |
| 27 | public final class HostIdProtoTranslator { |
| 28 | |
| 29 | /** |
| 30 | * Translates gRPC HostId to {@link HostId}. |
| 31 | * |
| 32 | * @param hostId gRPC message |
| 33 | * @return {@link HostId} |
| 34 | */ |
| 35 | public static HostId translate(HostIdProto hostId) { |
| 36 | |
| 37 | if (hostId.equals(HostIdProto.getDefaultInstance())) { |
| 38 | return null; |
| 39 | } |
| 40 | |
| 41 | return HostId.hostId(MacAddress.valueOf(hostId.getMac()), VlanId.vlanId((short) hostId.getVlanId())); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Translates {@link HostId} to gRPC HostId message. |
| 46 | * |
| 47 | * @param hostId {@link HostId} |
| 48 | * @return gRPC HostId message |
| 49 | */ |
| 50 | public static HostIdProto translate(HostId hostId) { |
| 51 | |
| 52 | if (hostId != null) { |
| 53 | return HostIdProto.newBuilder() |
| 54 | .setMac(hostId.mac().toString()) |
| 55 | .setVlanId(hostId.vlanId().toShort()) |
| 56 | .build(); |
| 57 | } |
| 58 | |
| 59 | return HostIdProto.getDefaultInstance(); |
| 60 | } |
| 61 | |
| 62 | // Utility class not intended for instantiation. |
| 63 | private HostIdProtoTranslator() {} |
| 64 | } |