blob: fd149b104cedb05536f50654a5a26b7f399cff1c [file] [log] [blame]
shivani vaidya9632b5f2017-06-27 11:00:04 -07001/*
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.net;
17
18import org.onlab.packet.IpAddress;
Frank Wangcfffbaa2017-05-06 16:11:08 +080019import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass.ConnectPointProto;
shivani vaidya9632b5f2017-06-27 11:00:04 -070020import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.HostId;
23import org.onosproject.net.IpElementId;
24import org.onosproject.net.PortNumber;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.util.Optional;
29
30/**
31 * gRPC ConnectPoint message to org.onosproject.net.ConnectPoint conversion related utilities.
32 */
33public 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 Wangcfffbaa2017-05-06 16:11:08 +080043 public static Optional<ConnectPoint> translate(ConnectPointProto connectPoint) {
shivani vaidya9632b5f2017-06-27 11:00:04 -070044 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 Wangcfffbaa2017-05-06 16:11:08 +080068 public static ConnectPointProto translate(ConnectPoint connectPoint) {
69
shivani vaidya9632b5f2017-06-27 11:00:04 -070070 if (connectPoint.elementId() instanceof DeviceId) {
Frank Wangcfffbaa2017-05-06 16:11:08 +080071 return ConnectPointProto.newBuilder().setDeviceId(connectPoint.deviceId().toString())
shivani vaidya9632b5f2017-06-27 11:00:04 -070072 .setPortNumber(connectPoint.port().toString())
73 .build();
74 } else if (connectPoint.elementId() instanceof HostId) {
Frank Wangcfffbaa2017-05-06 16:11:08 +080075 return ConnectPointProto.newBuilder().setHostId(connectPoint.hostId().toString())
shivani vaidya9632b5f2017-06-27 11:00:04 -070076 .setPortNumber(connectPoint.port().toString())
77 .build();
Ray Milkeyef310052018-02-06 08:58:51 -080078 } else if (connectPoint.ipElementId() != null) {
Frank Wangcfffbaa2017-05-06 16:11:08 +080079 return ConnectPointProto.newBuilder().setIpElementId(connectPoint.ipElementId().toString())
shivani vaidya9632b5f2017-06-27 11:00:04 -070080 .setPortNumber(connectPoint.port().toString())
81 .build();
82 } else {
83 log.warn("Unrecognized ElementId", connectPoint);
84 throw new IllegalArgumentException("Unrecognized ElementId");
85 }
86 }
87
shivani vaidya9632b5f2017-06-27 11:00:04 -070088 // Utility class not intended for instantiation.
89 private ConnectPointProtoTranslator() {}
Frank Wangcfffbaa2017-05-06 16:11:08 +080090}