shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [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.net; |
| 17 | |
| 18 | import org.onlab.packet.IpAddress; |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 19 | import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass.ConnectPointProto; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.HostId; |
| 23 | import org.onosproject.net.IpElementId; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.util.Optional; |
| 29 | |
| 30 | /** |
| 31 | * gRPC ConnectPoint message to org.onosproject.net.ConnectPoint conversion related utilities. |
| 32 | */ |
| 33 | public final class ConnectPointProtoTranslator { |
| 34 | |
| 35 | private static final Logger log = LoggerFactory.getLogger(ConnectPointProtoTranslator.class); |
| 36 | |
| 37 | /** |
| 38 | * Translates gRPC ConnectPoint message to Optional of {@link org.onosproject.net.ConnectPoint}. |
| 39 | * |
| 40 | * @param connectPoint gRPC message |
| 41 | * @return Optional of equivalent {@link org.onosproject.net.ConnectPoint} or empty if ElementId is not recognized |
| 42 | */ |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 43 | public static Optional<ConnectPoint> translate(ConnectPointProto connectPoint) { |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 44 | switch (connectPoint.getElementIdCase()) { |
| 45 | case DEVICE_ID: |
| 46 | return Optional.of(new ConnectPoint(DeviceId.deviceId(connectPoint.getDeviceId()), |
| 47 | PortNumber.portNumber(connectPoint.getPortNumber()))); |
| 48 | case HOST_ID: |
| 49 | return Optional.of(new ConnectPoint(HostId.hostId(connectPoint.getHostId()), |
| 50 | PortNumber.portNumber(connectPoint.getPortNumber()))); |
| 51 | case IP_ELEMENT_ID: |
| 52 | return Optional.of(new ConnectPoint(IpElementId.ipElement(IpAddress |
| 53 | .valueOf(connectPoint |
| 54 | .getIpElementId() |
| 55 | )), |
| 56 | PortNumber.portNumber(connectPoint.getPortNumber()))); |
| 57 | default: |
| 58 | return Optional.empty(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Translates {@link org.onosproject.net.ConnectPoint} to gRPC ConnectPoint message. |
| 64 | * |
| 65 | * @param connectPoint {@link org.onosproject.net.ConnectPoint} |
| 66 | * @return gRPC ConnectPoint message |
| 67 | */ |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 68 | public static ConnectPointProto translate(ConnectPoint connectPoint) { |
| 69 | |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 70 | if (connectPoint.elementId() instanceof DeviceId) { |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 71 | return ConnectPointProto.newBuilder().setDeviceId(connectPoint.deviceId().toString()) |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 72 | .setPortNumber(connectPoint.port().toString()) |
| 73 | .build(); |
| 74 | } else if (connectPoint.elementId() instanceof HostId) { |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 75 | return ConnectPointProto.newBuilder().setHostId(connectPoint.hostId().toString()) |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 76 | .setPortNumber(connectPoint.port().toString()) |
| 77 | .build(); |
Ray Milkey | ef31005 | 2018-02-06 08:58:51 -0800 | [diff] [blame] | 78 | } else if (connectPoint.ipElementId() != null) { |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 79 | return ConnectPointProto.newBuilder().setIpElementId(connectPoint.ipElementId().toString()) |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 80 | .setPortNumber(connectPoint.port().toString()) |
| 81 | .build(); |
| 82 | } else { |
| 83 | log.warn("Unrecognized ElementId", connectPoint); |
| 84 | throw new IllegalArgumentException("Unrecognized ElementId"); |
| 85 | } |
| 86 | } |
| 87 | |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 88 | // Utility class not intended for instantiation. |
| 89 | private ConnectPointProtoTranslator() {} |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 90 | } |