blob: ff29b2788c30b11a105ba4f2005beaff5ef3379c [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;
23import org.onosproject.grpc.net.models.DeviceDescriptionProto;
24import org.onosproject.grpc.net.models.DeviceEnums;
25import org.onosproject.grpc.net.models.PortDescriptionProto;
26import org.onosproject.grpc.net.models.PortEnums.PortType;
27import org.onosproject.grpc.net.models.DeviceEnums.DeviceType;
28import org.onosproject.grpc.net.models.PortStatisticsProto;
29import 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 /**
57 * Translates gRPC enum MastershipRole to ONOS enum.
58 *
59 * @param role mastership role in gRPC enum
60 * @return equivalent in ONOS enum
61 */
62 public static MastershipRole translate(DeviceEnums.MastershipRole role) {
63 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 */
84 public static DeviceEnums.MastershipRole translate(MastershipRole newRole) {
85 switch (newRole) {
86 case MASTER:
87 return DeviceEnums.MastershipRole.MASTER;
88 case STANDBY:
89 return DeviceEnums.MastershipRole.STANDBY;
90 case NONE:
91 default:
92 return DeviceEnums.MastershipRole.NONE;
93 }
94 }
95
96
97 /**
98 * Translates gRPC DeviceDescription to {@link DeviceDescription}.
99 *
100 * @param deviceDescription gRPC message
101 * @return {@link DeviceDescription}
102 */
103 public static DeviceDescription translate(
104 DeviceDescriptionProto.DeviceDescription deviceDescription) {
105 URI uri = URI.create(deviceDescription.getDeviceUri());
106 Device.Type type = translate(deviceDescription.getType());
107 String manufacturer = deviceDescription.getManufacturer();
108 String hwVersion = deviceDescription.getHwVersion();
109 String swVersion = deviceDescription.getSwVersion();
110 String serialNumber = deviceDescription.getSerialNumber();
111 ChassisId chassis = new ChassisId(deviceDescription.getChassisId());
112 boolean defaultAvailable = deviceDescription.getIsDefaultAvailable();
113 return new DefaultDeviceDescription(uri, type, manufacturer,
114 hwVersion, swVersion, serialNumber,
115 chassis,
116 defaultAvailable,
117 asAnnotations(deviceDescription.getAnnotationsMap()));
118 }
119
120 /**
121 * Translates {@link DeviceDescription} to gRPC DeviceDescription message.
122 *
123 * @param deviceDescription {@link DeviceDescription}
124 * @return gRPC DeviceDescription message
125 */
126 public static DeviceDescriptionProto.DeviceDescription translate(
127 DeviceDescription deviceDescription) {
128
129 return DeviceDescriptionProto.DeviceDescription.newBuilder()
130 .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 /**
144 * Translates gRPC DeviceType to {@link Device.Type}.
145 *
146 * @param type gRPC message
147 * @return {@link Device.Type}
148 */
149 public static Device.Type translate(DeviceType type) {
150 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;
177 case VIRTUAL:
178 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 /**
188 * Translates {@link Type} to gRPC DeviceType.
189 *
190 * @param type {@link Type}
191 * @return gRPC message
192 */
193 public static DeviceType translate(Device.Type type) {
194 switch (type) {
195 case BALANCER:
196 return DeviceType.BALANCER;
197 case CONTROLLER:
198 return DeviceType.CONTROLLER;
199 case FIBER_SWITCH:
200 return DeviceType.FIBER_SWITCH;
201 case FIREWALL:
202 return DeviceType.FIREWALL;
203 case IDS:
204 return DeviceType.IDS;
205 case IPS:
206 return DeviceType.IPS;
207 case MICROWAVE:
208 return DeviceType.MICROWAVE;
209 case OTHER:
210 return DeviceType.OTHER;
211 case OTN:
212 return DeviceType.OTN;
213 case ROADM:
214 return DeviceType.ROADM;
215 case ROADM_OTN:
216 return DeviceType.ROADM_OTN;
217 case ROUTER:
218 return DeviceType.ROUTER;
219 case SWITCH:
220 return DeviceType.SWITCH;
221 case VIRTUAL:
222 return DeviceType.VIRTUAL;
223
224 default:
225 log.warn("Unexpected Device.Type: {}", type);
226 return DeviceType.OTHER;
227 }
228 }
229
230 /**
231 * Translates gRPC PortDescription message to {@link PortDescription}.
232 *
233 * @param portDescription gRPC message
234 * @return {@link PortDescription}
235 */
236 public static PortDescription translate(PortDescriptionProto.PortDescription portDescription) {
237 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 /**
246 * Translates {@link PortDescription} to gRPC PortDescription message.
247 *
248 * @param portDescription {@link PortDescription}
249 * @return gRPC PortDescription message
250 */
251 public static PortDescriptionProto.PortDescription translate(PortDescription portDescription) {
252 return PortDescriptionProto.PortDescription.newBuilder()
253 .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 /**
262 * Translates gRPC PortType to {@link Port.Type}.
263 *
264 * @param type gRPC message
265 * @return {@link Port.Type}
266 */
267 public static Port.Type translate(org.onosproject.grpc.net.models.PortEnums.PortType type) {
268 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;
281 case VIRTUAL:
282 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 */
297 public static PortType translate(Port.Type type) {
298 switch (type) {
299 case COPPER:
300 return PortType.COPPER;
301 case FIBER:
302 return PortType.FIBER;
303 case OCH:
304 return PortType.OCH;
305 case ODUCLT:
306 return PortType.ODUCLT;
307 case OMS:
308 return PortType.OMS;
309 case PACKET:
310 return PortType.PACKET;
311 case VIRTUAL:
312 return PortType.VIRTUAL;
313
314 default:
315 log.warn("Unexpected Port.Type: {}", type);
316 return PortType.COPPER;
317 }
318 }
319
320 /**
321 * Translates gRPC PortStatistics message to {@link PortStatistics}.
322 *
323 * @param portStatistics gRPC PortStatistics message
324 * @return {@link PortStatistics}
325 */
326 public static PortStatistics translate(PortStatisticsProto.PortStatistics portStatistics) {
327 // 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 */
341 public static PortStatisticsProto.PortStatistics translate(PortStatistics portStatistics) {
342 // TODO implement adding missing fields
343 return PortStatisticsProto.PortStatistics.newBuilder()
344 .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}