blob: 481839ad05c6b1e9105da2f66b605d304cc722ec [file] [log] [blame]
Aaron Kruglikov9f95f992017-06-23 14:15:25 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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;
17
18import java.net.URI;
19import java.util.HashMap;
20import java.util.Map;
21
22import org.onlab.packet.ChassisId;
Jian Lic9b4bf12017-06-26 23:50:32 +090023import org.onosproject.grpc.net.device.models.DeviceDescriptionProtoOuterClass.DeviceDescriptionProto;
24import org.onosproject.grpc.net.device.models.DeviceEnumsProto.MastershipRoleProto;
25import org.onosproject.grpc.net.device.models.PortDescriptionProtoOuterClass.PortDescriptionProto;
26import org.onosproject.grpc.net.device.models.PortEnumsProto.PortTypeProto;
27import org.onosproject.grpc.net.device.models.DeviceEnumsProto.DeviceTypeProto;
28import org.onosproject.grpc.net.device.models.PortStatisticsProtoOuterClass.PortStatisticsProto;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090029import org.onosproject.net.Annotations;
30import org.onosproject.net.DefaultAnnotations;
31import org.onosproject.net.Device;
32import org.onosproject.net.MastershipRole;
33import org.onosproject.net.Port;
34import org.onosproject.net.Port.Type;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.SparseAnnotations;
37import org.onosproject.net.device.DefaultDeviceDescription;
38import org.onosproject.net.device.DefaultPortDescription;
39import org.onosproject.net.device.DefaultPortStatistics;
40import org.onosproject.net.device.DeviceDescription;
41import org.onosproject.net.device.PortDescription;
42import org.onosproject.net.device.PortStatistics;
43import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
46import com.google.common.annotations.Beta;
47
48/**
49 * gRPC message conversion related utilities.
50 */
51@Beta
52public final class ProtobufUtils {
53
54 private static final Logger log = LoggerFactory.getLogger(ProtobufUtils.class);
55
56 /**
Jian Lic9b4bf12017-06-26 23:50:32 +090057 * Translates gRPC enum MastershipRoleProto to ONOS enum.
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090058 *
59 * @param role mastership role in gRPC enum
60 * @return equivalent in ONOS enum
61 */
Jian Lic9b4bf12017-06-26 23:50:32 +090062 public static MastershipRole translate(MastershipRoleProto role) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090063 switch (role) {
64 case NONE:
65 return MastershipRole.NONE;
66 case MASTER:
67 return MastershipRole.MASTER;
68 case STANDBY:
69 return MastershipRole.STANDBY;
70 case UNRECOGNIZED:
71 log.warn("Unrecognized MastershipRole gRPC message: {}", role);
72 return MastershipRole.NONE;
73 default:
74 return MastershipRole.NONE;
75 }
76 }
77
78 /**
79 * Translates ONOS enum MastershipRole to gRPC enum.
80 *
81 * @param newRole ONOS' mastership role
82 * @return equivalent in gRPC message enum
83 */
Jian Lic9b4bf12017-06-26 23:50:32 +090084 public static MastershipRoleProto translate(MastershipRole newRole) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090085 switch (newRole) {
86 case MASTER:
Jian Lic9b4bf12017-06-26 23:50:32 +090087 return MastershipRoleProto.MASTER;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090088 case STANDBY:
Jian Lic9b4bf12017-06-26 23:50:32 +090089 return MastershipRoleProto.STANDBY;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090090 case NONE:
91 default:
Jian Lic9b4bf12017-06-26 23:50:32 +090092 return MastershipRoleProto.NONE;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090093 }
94 }
95
96
97 /**
Jian Lic9b4bf12017-06-26 23:50:32 +090098 * Translates gRPC DeviceDescriptionProto to {@link DeviceDescription}.
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090099 *
Jian Lic9b4bf12017-06-26 23:50:32 +0900100 * @param devDescProto device description protobuf message
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900101 * @return {@link DeviceDescription}
102 */
103 public static DeviceDescription translate(
Jian Lic9b4bf12017-06-26 23:50:32 +0900104 DeviceDescriptionProto devDescProto) {
105 URI uri = URI.create(devDescProto.getDeviceUri());
106 Device.Type type = translate(devDescProto.getType());
107 String manufacturer = devDescProto.getManufacturer();
108 String hwVersion = devDescProto.getHwVersion();
109 String swVersion = devDescProto.getSwVersion();
110 String serialNumber = devDescProto.getSerialNumber();
111 ChassisId chassis = new ChassisId(devDescProto.getChassisId());
112 boolean defaultAvailable = devDescProto.getIsDefaultAvailable();
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900113 return new DefaultDeviceDescription(uri, type, manufacturer,
114 hwVersion, swVersion, serialNumber,
115 chassis,
116 defaultAvailable,
Jian Lic9b4bf12017-06-26 23:50:32 +0900117 asAnnotations(devDescProto.getAnnotationsMap()));
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900118 }
119
120 /**
Jian Lic9b4bf12017-06-26 23:50:32 +0900121 * Translates {@link DeviceDescription} to gRPC DeviceDescriptionProto message.
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900122 *
123 * @param deviceDescription {@link DeviceDescription}
Jian Lic9b4bf12017-06-26 23:50:32 +0900124 * @return gRPC DeviceDescriptionProto message
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900125 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900126 public static DeviceDescriptionProto translate(
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900127 DeviceDescription deviceDescription) {
128
Jian Lic9b4bf12017-06-26 23:50:32 +0900129 return DeviceDescriptionProto.newBuilder()
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900130 .setDeviceUri(deviceDescription.deviceUri().toString())
131 .setType(translate(deviceDescription.type()))
132 .setManufacturer(deviceDescription.manufacturer())
133 .setHwVersion(deviceDescription.hwVersion())
134 .setSwVersion(deviceDescription.swVersion())
135 .setSerialNumber(deviceDescription.serialNumber())
136 .setChassisId(deviceDescription.chassisId().toString())
137 .setIsDefaultAvailable(deviceDescription.isDefaultAvailable())
138 .putAllAnnotations(asMap(deviceDescription.annotations()))
139 .build();
140 }
141
142
143 /**
Jian Lic9b4bf12017-06-26 23:50:32 +0900144 * Translates gRPC DeviceTypeProto to {@link Device.Type}.
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900145 *
146 * @param type gRPC message
147 * @return {@link Device.Type}
148 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900149 public static Device.Type translate(DeviceTypeProto type) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900150 switch (type) {
151 case BALANCER:
152 return Device.Type.BALANCER;
153 case CONTROLLER:
154 return Device.Type.CONTROLLER;
155 case FIBER_SWITCH:
156 return Device.Type.FIBER_SWITCH;
157 case FIREWALL:
158 return Device.Type.FIREWALL;
159 case IDS:
160 return Device.Type.IDS;
161 case IPS:
162 return Device.Type.IPS;
163 case MICROWAVE:
164 return Device.Type.MICROWAVE;
165 case OTHER:
166 return Device.Type.OTHER;
167 case OTN:
168 return Device.Type.OTN;
169 case ROADM:
170 return Device.Type.ROADM;
171 case ROADM_OTN:
172 return Device.Type.ROADM_OTN;
173 case ROUTER:
174 return Device.Type.ROUTER;
175 case SWITCH:
176 return Device.Type.SWITCH;
Jian Lic9b4bf12017-06-26 23:50:32 +0900177 case VIRTUAL_DEVICE:
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900178 return Device.Type.VIRTUAL;
179
180 case UNRECOGNIZED:
181 default:
182 log.warn("Unexpected DeviceType: {}", type);
183 return Device.Type.OTHER;
184 }
185 }
186
187 /**
Jian Lic9b4bf12017-06-26 23:50:32 +0900188 * Translates {@link Type} to gRPC DeviceTypeProto.
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900189 *
190 * @param type {@link Type}
191 * @return gRPC message
192 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900193 public static DeviceTypeProto translate(Device.Type type) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900194 switch (type) {
195 case BALANCER:
Jian Lic9b4bf12017-06-26 23:50:32 +0900196 return DeviceTypeProto.BALANCER;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900197 case CONTROLLER:
Jian Lic9b4bf12017-06-26 23:50:32 +0900198 return DeviceTypeProto.CONTROLLER;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900199 case FIBER_SWITCH:
Jian Lic9b4bf12017-06-26 23:50:32 +0900200 return DeviceTypeProto.FIBER_SWITCH;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900201 case FIREWALL:
Jian Lic9b4bf12017-06-26 23:50:32 +0900202 return DeviceTypeProto.FIREWALL;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900203 case IDS:
Jian Lic9b4bf12017-06-26 23:50:32 +0900204 return DeviceTypeProto.IDS;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900205 case IPS:
Jian Lic9b4bf12017-06-26 23:50:32 +0900206 return DeviceTypeProto.IPS;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900207 case MICROWAVE:
Jian Lic9b4bf12017-06-26 23:50:32 +0900208 return DeviceTypeProto.MICROWAVE;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900209 case OTHER:
Jian Lic9b4bf12017-06-26 23:50:32 +0900210 return DeviceTypeProto.OTHER;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900211 case OTN:
Jian Lic9b4bf12017-06-26 23:50:32 +0900212 return DeviceTypeProto.OTN;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900213 case ROADM:
Jian Lic9b4bf12017-06-26 23:50:32 +0900214 return DeviceTypeProto.ROADM;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900215 case ROADM_OTN:
Jian Lic9b4bf12017-06-26 23:50:32 +0900216 return DeviceTypeProto.ROADM_OTN;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900217 case ROUTER:
Jian Lic9b4bf12017-06-26 23:50:32 +0900218 return DeviceTypeProto.ROUTER;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900219 case SWITCH:
Jian Lic9b4bf12017-06-26 23:50:32 +0900220 return DeviceTypeProto.SWITCH;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900221 case VIRTUAL:
Jian Lic9b4bf12017-06-26 23:50:32 +0900222 return DeviceTypeProto.VIRTUAL_DEVICE;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900223
224 default:
225 log.warn("Unexpected Device.Type: {}", type);
Jian Lic9b4bf12017-06-26 23:50:32 +0900226 return DeviceTypeProto.OTHER;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900227 }
228 }
229
230 /**
Jian Lic9b4bf12017-06-26 23:50:32 +0900231 * Translates gRPC PortDescriptionProto message to {@link PortDescription}.
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900232 *
233 * @param portDescription gRPC message
234 * @return {@link PortDescription}
235 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900236 public static PortDescription translate(PortDescriptionProto portDescription) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900237 PortNumber number = PortNumber.fromString(portDescription.getPortNumber());
238 boolean isEnabled = portDescription.getIsEnabled();
239 Port.Type type = translate(portDescription.getType());
240 long portSpeed = portDescription.getPortSpeed();
241 SparseAnnotations annotations = asAnnotations(portDescription.getAnnotationsMap());
242 return new DefaultPortDescription(number, isEnabled, type, portSpeed, annotations);
243 }
244
245 /**
Jian Lic9b4bf12017-06-26 23:50:32 +0900246 * Translates {@link PortDescription} to gRPC PortDescriptionProto message.
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900247 *
248 * @param portDescription {@link PortDescription}
Jian Lic9b4bf12017-06-26 23:50:32 +0900249 * @return gRPC PortDescriptionProto message
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900250 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900251 public static PortDescriptionProto translate(PortDescription portDescription) {
252 return PortDescriptionProto.newBuilder()
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900253 .setPortNumber(portDescription.portNumber().toString())
254 .setIsEnabled(portDescription.isEnabled())
255 .setType(translate(portDescription.type()))
256 .setPortSpeed(portDescription.portSpeed())
257 .putAllAnnotations(asMap(portDescription.annotations()))
258 .build();
259 }
260
261 /**
Jian Lic9b4bf12017-06-26 23:50:32 +0900262 * Translates gRPC PortTypeProto to {@link Port.Type}.
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900263 *
264 * @param type gRPC message
265 * @return {@link Port.Type}
266 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900267 public static Port.Type translate(PortTypeProto type) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900268 switch (type) {
269 case COPPER:
270 return Type.COPPER;
271 case FIBER:
272 return Type.FIBER;
273 case OCH:
274 return Type.OCH;
275 case ODUCLT:
276 return Type.ODUCLT;
277 case OMS:
278 return Type.OMS;
279 case PACKET:
280 return Type.PACKET;
Jian Lic9b4bf12017-06-26 23:50:32 +0900281 case VIRTUAL_PORT:
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900282 return Type.VIRTUAL;
283
284 case UNRECOGNIZED:
285 default:
286 log.warn("Unexpected PortType: {}", type);
287 return Type.COPPER;
288 }
289 }
290
291 /**
292 * Translates {@link Port.Type} to gRPC PortType.
293 *
294 * @param type {@link org.onosproject.net.Port.Type}
295 * @return gRPC message
296 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900297 public static PortTypeProto translate(Port.Type type) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900298 switch (type) {
299 case COPPER:
Jian Lic9b4bf12017-06-26 23:50:32 +0900300 return PortTypeProto.COPPER;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900301 case FIBER:
Jian Lic9b4bf12017-06-26 23:50:32 +0900302 return PortTypeProto.FIBER;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900303 case OCH:
Jian Lic9b4bf12017-06-26 23:50:32 +0900304 return PortTypeProto.OCH;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900305 case ODUCLT:
Jian Lic9b4bf12017-06-26 23:50:32 +0900306 return PortTypeProto.ODUCLT;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900307 case OMS:
Jian Lic9b4bf12017-06-26 23:50:32 +0900308 return PortTypeProto.OMS;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900309 case PACKET:
Jian Lic9b4bf12017-06-26 23:50:32 +0900310 return PortTypeProto.PACKET;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900311 case VIRTUAL:
Jian Lic9b4bf12017-06-26 23:50:32 +0900312 return PortTypeProto.VIRTUAL_PORT;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900313
314 default:
315 log.warn("Unexpected Port.Type: {}", type);
Jian Lic9b4bf12017-06-26 23:50:32 +0900316 return PortTypeProto.COPPER;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900317 }
318 }
319
320 /**
321 * Translates gRPC PortStatistics message to {@link PortStatistics}.
322 *
323 * @param portStatistics gRPC PortStatistics message
324 * @return {@link PortStatistics}
325 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900326 public static PortStatistics translate(PortStatisticsProto portStatistics) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900327 // TODO implement adding missing fields
328 return DefaultPortStatistics.builder()
329 .setPort(portStatistics.getPort())
330 .setPacketsReceived(portStatistics.getPacketsReceived())
331 .setPacketsSent(portStatistics.getPacketsSent())
332 .build();
333 }
334
335 /**
336 * Translates {@link PortStatistics} to gRPC PortStatistics message.
337 *
338 * @param portStatistics {@link PortStatistics}
339 * @return gRPC PortStatistics message
340 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900341 public static PortStatisticsProto translate(PortStatistics portStatistics) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900342 // TODO implement adding missing fields
Jian Lic9b4bf12017-06-26 23:50:32 +0900343 return PortStatisticsProto.newBuilder()
Aaron Kruglikov9f95f992017-06-23 14:15:25 +0900344 .setPort(portStatistics.port())
345 .setPacketsReceived(portStatistics.packetsReceived())
346 .setPacketsSent(portStatistics.packetsSent())
347 .build();
348 }
349
350 // may be this can be moved to Annotation itself or AnnotationsUtils
351 /**
352 * Converts Annotations to Map of Strings.
353 *
354 * @param annotations {@link Annotations}
355 * @return Map of annotation key and values
356 */
357 public static Map<String, String> asMap(Annotations annotations) {
358 if (annotations instanceof DefaultAnnotations) {
359 return ((DefaultAnnotations) annotations).asMap();
360 }
361 Map<String, String> map = new HashMap<>();
362 annotations.keys()
363 .forEach(k -> map.put(k, annotations.value(k)));
364
365 return map;
366 }
367
368 // may be this can be moved to Annotation itself or AnnotationsUtils
369 /**
370 * Converts Map of Strings to {@link SparseAnnotations}.
371 *
372 * @param annotations Map of annotation key and values
373 * @return {@link SparseAnnotations}
374 */
375 public static SparseAnnotations asAnnotations(Map<String, String> annotations) {
376 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
377 annotations.entrySet().forEach(e -> {
378 if (e.getValue() != null) {
379 builder.set(e.getKey(), e.getValue());
380 } else {
381 builder.remove(e.getKey());
382 }
383 });
384 return builder.build();
385 }
386
387 // Utility class not intended for instantiation.
388 private ProtobufUtils() {}
389}