blob: ff3998714835e68b775da7b60c61388e523b8b30 [file] [log] [blame]
Jian Lid8e72072017-11-21 10:50:02 +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.net.device;
17
18import org.onosproject.grpc.net.device.models.PortDescriptionProtoOuterClass.PortDescriptionProto;
19import org.onosproject.grpc.net.device.models.PortEnumsProto;
20import org.onosproject.grpc.net.device.models.PortStatisticsProtoOuterClass;
21import org.onosproject.grpc.net.device.models.PortStatisticsProtoOuterClass.PortStatisticsProto;
Jian Li21fa4fe2017-11-21 11:23:19 +090022import org.onosproject.incubator.protobuf.models.net.AnnotationsTranslator;
Jian Lid8e72072017-11-21 10:50:02 +090023import org.onosproject.net.Port;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.SparseAnnotations;
26import org.onosproject.net.device.DefaultPortDescription;
27import org.onosproject.net.device.DefaultPortStatistics;
28import org.onosproject.net.device.PortDescription;
29import org.onosproject.net.device.PortStatistics;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Jian Li1aaa64d2017-12-14 11:25:39 +090033import java.util.Optional;
34
Jian Lid8e72072017-11-21 10:50:02 +090035/**
36 * gRPC message conversion related utilities for port service.
37 */
38public final class PortProtoTranslator {
39
40 private static final Logger log = LoggerFactory.getLogger(PortProtoTranslator.class);
41
42 /**
43 * Translates gRPC PortDescription message to {@link PortDescription}.
44 *
45 * @param portDescription gRPC message
46 * @return {@link PortDescription}
47 */
48 public static PortDescription translate(PortDescriptionProto portDescription) {
49 PortNumber number = PortNumber.fromString(portDescription.getPortNumber());
50 boolean isEnabled = portDescription.getIsEnabled();
Jian Li1aaa64d2017-12-14 11:25:39 +090051 Port.Type type = translate(portDescription.getType()).get();
Jian Lid8e72072017-11-21 10:50:02 +090052 long portSpeed = portDescription.getPortSpeed();
Jian Li21fa4fe2017-11-21 11:23:19 +090053 SparseAnnotations annotations = AnnotationsTranslator.asAnnotations(portDescription.getAnnotationsMap());
Jian Lid8e72072017-11-21 10:50:02 +090054 // TODO How to deal with more specific Port...
Yuta HIGUCHI53e47962018-03-01 23:50:48 -080055 return DefaultPortDescription.builder().withPortNumber(number).isEnabled(isEnabled)
56 .type(type).portSpeed(portSpeed).annotations(annotations)
57 .build();
Jian Lid8e72072017-11-21 10:50:02 +090058 }
59
60 /**
61 * Translates {@link PortDescription} to gRPC PortDescription message.
62 *
63 * @param portDescription {@link PortDescription}
64 * @return gRPC PortDescription message
65 */
66 public static PortDescriptionProto translate(PortDescription portDescription) {
67 return PortDescriptionProto.newBuilder()
68 .setPortNumber(portDescription.portNumber().toString())
69 .setIsEnabled(portDescription.isEnabled())
70 .setType(translate(portDescription.type()))
71 .setPortSpeed(portDescription.portSpeed())
Jian Li21fa4fe2017-11-21 11:23:19 +090072 .putAllAnnotations(AnnotationsTranslator.asMap(portDescription.annotations()))
Jian Lid8e72072017-11-21 10:50:02 +090073 .build();
74 }
75
76 /**
77 * Translates gRPC PortType to {@link Port.Type}.
78 *
79 * @param type gRPC message
80 * @return {@link Port.Type}
81 */
Jian Li1aaa64d2017-12-14 11:25:39 +090082 public static Optional<Port.Type> translate(PortEnumsProto.PortTypeProto type) {
Jian Lid8e72072017-11-21 10:50:02 +090083 switch (type) {
84 case COPPER:
Jian Li1aaa64d2017-12-14 11:25:39 +090085 return Optional.of(Port.Type.COPPER);
Jian Lid8e72072017-11-21 10:50:02 +090086 case FIBER:
Jian Li1aaa64d2017-12-14 11:25:39 +090087 return Optional.of(Port.Type.FIBER);
Jian Lid8e72072017-11-21 10:50:02 +090088 case OCH:
Jian Li1aaa64d2017-12-14 11:25:39 +090089 return Optional.of(Port.Type.OCH);
Jian Lid8e72072017-11-21 10:50:02 +090090 case ODUCLT:
Jian Li1aaa64d2017-12-14 11:25:39 +090091 return Optional.of(Port.Type.ODUCLT);
Jian Lid8e72072017-11-21 10:50:02 +090092 case OMS:
Jian Li1aaa64d2017-12-14 11:25:39 +090093 return Optional.of(Port.Type.OMS);
Jian Lid8e72072017-11-21 10:50:02 +090094 case PACKET:
Jian Li1aaa64d2017-12-14 11:25:39 +090095 return Optional.of(Port.Type.PACKET);
Jian Lid8e72072017-11-21 10:50:02 +090096 case VIRTUAL_PORT:
Jian Li1aaa64d2017-12-14 11:25:39 +090097 return Optional.of(Port.Type.VIRTUAL);
Jian Lid8e72072017-11-21 10:50:02 +090098
Jian Lid8e72072017-11-21 10:50:02 +090099 default:
100 log.warn("Unexpected PortType: {}", type);
Jian Li1aaa64d2017-12-14 11:25:39 +0900101 return Optional.empty();
Jian Lid8e72072017-11-21 10:50:02 +0900102 }
103 }
104
105 /**
106 * Translates {@link Port.Type} to gRPC PortType.
107 *
108 * @param type {@link Port.Type}
109 * @return gRPC message
110 */
111 public static PortEnumsProto.PortTypeProto translate(Port.Type type) {
112 switch (type) {
113 case COPPER:
114 return PortEnumsProto.PortTypeProto.COPPER;
115 case FIBER:
116 return PortEnumsProto.PortTypeProto.FIBER;
117 case OCH:
118 return PortEnumsProto.PortTypeProto.OCH;
119 case ODUCLT:
120 return PortEnumsProto.PortTypeProto.ODUCLT;
121 case OMS:
122 return PortEnumsProto.PortTypeProto.OMS;
123 case PACKET:
124 return PortEnumsProto.PortTypeProto.PACKET;
125 case VIRTUAL:
126 return PortEnumsProto.PortTypeProto.VIRTUAL_PORT;
127
128 default:
129 log.warn("Unexpected Port.Type: {}", type);
Jian Li1aaa64d2017-12-14 11:25:39 +0900130 return PortEnumsProto.PortTypeProto.UNRECOGNIZED;
Jian Lid8e72072017-11-21 10:50:02 +0900131 }
132 }
133
134 /**
135 * Translates gRPC PortStatistics message to {@link PortStatisticsProtoOuterClass}.
136 *
137 * @param portStatistics gRPC PortStatistics message
138 * @return {@link PortStatisticsProtoOuterClass}
139 */
140 public static PortStatistics translate(PortStatisticsProto portStatistics) {
141 // TODO implement adding missing fields
142 return DefaultPortStatistics.builder()
143 .setPort(portStatistics.getPort())
144 .setPacketsReceived(portStatistics.getPacketsReceived())
145 .setPacketsSent(portStatistics.getPacketsSent())
146 .build();
147 }
148
149 /**
150 * Translates {@link PortStatistics} to gRPC PortStatistics message.
151 *
152 * @param portStatistics {@link PortStatistics}
153 * @return gRPC PortStatistics message
154 */
155 public static PortStatisticsProto translate(PortStatistics portStatistics) {
156 // TODO implement adding missing fields
157 return PortStatisticsProto.newBuilder()
158 .setPort(portStatistics.port())
159 .setPacketsReceived(portStatistics.packetsReceived())
160 .setPacketsSent(portStatistics.packetsSent())
161 .build();
162 }
163
Jian Lid8e72072017-11-21 10:50:02 +0900164 // Utility class not intended for instantiation.
165 private PortProtoTranslator() {}
166}