blob: 30410d6a048bc036d768bac092d048acc022a4b9 [file] [log] [blame]
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -08001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -08003 *
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 */
HIGUCHI Yuta06c1a3f2016-05-23 12:54:55 -070016package org.onosproject.incubator.protobuf.net;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080017
18import java.net.URI;
19import java.util.HashMap;
20import java.util.Map;
21
22import org.onlab.packet.ChassisId;
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070023import org.onosproject.grpc.net.Device.DeviceType;
24import org.onosproject.grpc.net.Port.PortType;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080025import org.onosproject.net.Annotations;
26import org.onosproject.net.DefaultAnnotations;
27import org.onosproject.net.Device;
28import org.onosproject.net.MastershipRole;
29import org.onosproject.net.Port;
30import org.onosproject.net.Port.Type;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.SparseAnnotations;
33import org.onosproject.net.device.DefaultDeviceDescription;
34import org.onosproject.net.device.DefaultPortDescription;
35import org.onosproject.net.device.DefaultPortStatistics;
36import org.onosproject.net.device.DeviceDescription;
37import org.onosproject.net.device.PortDescription;
38import org.onosproject.net.device.PortStatistics;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
HIGUCHI Yuta06c1a3f2016-05-23 12:54:55 -070042import com.google.common.annotations.Beta;
43
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080044
45/**
46 * gRPC message conversion related utilities.
47 */
48@Beta
HIGUCHI Yuta06c1a3f2016-05-23 12:54:55 -070049public final class ProtobufUtils {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080050
HIGUCHI Yuta06c1a3f2016-05-23 12:54:55 -070051 private static final Logger log = LoggerFactory.getLogger(ProtobufUtils.class);
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080052
53 /**
54 * Translates gRPC enum MastershipRole to ONOS enum.
55 *
56 * @param role mastership role in gRPC enum
57 * @return equivalent in ONOS enum
58 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070059 public static MastershipRole translate(org.onosproject.grpc.net.Device.MastershipRole role) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080060 switch (role) {
61 case NONE:
62 return MastershipRole.NONE;
63 case MASTER:
64 return MastershipRole.MASTER;
65 case STANDBY:
66 return MastershipRole.STANDBY;
67 case UNRECOGNIZED:
68 log.warn("Unrecognized MastershipRole gRPC message: {}", role);
Ray Milkey676249c2015-12-18 09:27:03 -080069 return MastershipRole.NONE;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080070 default:
71 return MastershipRole.NONE;
72 }
73 }
74
75 /**
76 * Translates ONOS enum MastershipRole to gRPC enum.
77 *
78 * @param newRole ONOS' mastership role
79 * @return equivalent in gRPC message enum
80 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070081 public static org.onosproject.grpc.net.Device.MastershipRole translate(MastershipRole newRole) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080082 switch (newRole) {
83 case MASTER:
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070084 return org.onosproject.grpc.net.Device.MastershipRole.MASTER;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080085 case STANDBY:
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070086 return org.onosproject.grpc.net.Device.MastershipRole.STANDBY;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080087 case NONE:
88 default:
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070089 return org.onosproject.grpc.net.Device.MastershipRole.NONE;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080090 }
91 }
92
93
94 /**
95 * Translates gRPC DeviceDescription to {@link DeviceDescription}.
96 *
97 * @param deviceDescription gRPC message
98 * @return {@link DeviceDescription}
99 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700100 public static DeviceDescription translate(org.onosproject.grpc.net.Device.DeviceDescription deviceDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800101 URI uri = URI.create(deviceDescription.getDeviceUri());
102 Device.Type type = translate(deviceDescription.getType());
103 String manufacturer = deviceDescription.getManufacturer();
104 String hwVersion = deviceDescription.getHwVersion();
105 String swVersion = deviceDescription.getSwVersion();
106 String serialNumber = deviceDescription.getSerialNumber();
107 ChassisId chassis = new ChassisId(deviceDescription.getChassisId());
Yuta HIGUCHI3680fab2016-08-22 21:19:55 -0700108 boolean defaultAvailable = deviceDescription.getIsDefaultAvailable();
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800109 return new DefaultDeviceDescription(uri, type, manufacturer,
110 hwVersion, swVersion, serialNumber,
111 chassis,
Yuta HIGUCHI3680fab2016-08-22 21:19:55 -0700112 defaultAvailable,
113 asAnnotations(deviceDescription.getAnnotationsMap()));
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800114 }
115
116 /**
117 * Translates {@link DeviceDescription} to gRPC DeviceDescription message.
118 *
119 * @param deviceDescription {@link DeviceDescription}
120 * @return gRPC DeviceDescription message
121 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700122 public static org.onosproject.grpc.net.Device.DeviceDescription translate(DeviceDescription deviceDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800123
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700124 return org.onosproject.grpc.net.Device.DeviceDescription.newBuilder()
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800125 .setDeviceUri(deviceDescription.deviceUri().toString())
126 .setType(translate(deviceDescription.type()))
127 .setManufacturer(deviceDescription.manufacturer())
128 .setHwVersion(deviceDescription.hwVersion())
129 .setSwVersion(deviceDescription.swVersion())
130 .setSerialNumber(deviceDescription.serialNumber())
131 .setChassisId(deviceDescription.chassisId().toString())
Yuta HIGUCHI3680fab2016-08-22 21:19:55 -0700132 .setIsDefaultAvailable(deviceDescription.isDefaultAvailable())
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800133 .putAllAnnotations(asMap(deviceDescription.annotations()))
134 .build();
135 }
136
137
138 /**
139 * Translates gRPC DeviceType to {@link Device.Type}.
140 *
141 * @param type gRPC message
142 * @return {@link Device.Type}
143 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700144 public static Device.Type translate(org.onosproject.grpc.net.Device.DeviceType type) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800145 switch (type) {
146 case BALANCER:
147 return Device.Type.BALANCER;
148 case CONTROLLER:
149 return Device.Type.CONTROLLER;
150 case FIBER_SWITCH:
151 return Device.Type.FIBER_SWITCH;
152 case FIREWALL:
153 return Device.Type.FIREWALL;
154 case IDS:
155 return Device.Type.IDS;
156 case IPS:
157 return Device.Type.IPS;
158 case MICROWAVE:
159 return Device.Type.MICROWAVE;
160 case OTHER:
161 return Device.Type.OTHER;
162 case OTN:
163 return Device.Type.OTN;
164 case ROADM:
165 return Device.Type.ROADM;
166 case ROADM_OTN:
167 return Device.Type.ROADM_OTN;
168 case ROUTER:
169 return Device.Type.ROUTER;
170 case SWITCH:
171 return Device.Type.SWITCH;
172 case VIRTUAL:
173 return Device.Type.VIRTUAL;
174
175 case UNRECOGNIZED:
176 default:
177 log.warn("Unexpected DeviceType: {}", type);
178 return Device.Type.OTHER;
179 }
180 }
181
182 /**
183 * Translates {@link Type} to gRPC DeviceType.
184 *
185 * @param type {@link Type}
186 * @return gRPC message
187 */
188 public static DeviceType translate(Device.Type type) {
189 switch (type) {
190 case BALANCER:
191 return DeviceType.BALANCER;
192 case CONTROLLER:
193 return DeviceType.CONTROLLER;
194 case FIBER_SWITCH:
195 return DeviceType.FIBER_SWITCH;
196 case FIREWALL:
197 return DeviceType.FIREWALL;
198 case IDS:
199 return DeviceType.IDS;
200 case IPS:
201 return DeviceType.IPS;
202 case MICROWAVE:
203 return DeviceType.MICROWAVE;
204 case OTHER:
205 return DeviceType.OTHER;
206 case OTN:
207 return DeviceType.OTN;
208 case ROADM:
209 return DeviceType.ROADM;
210 case ROADM_OTN:
211 return DeviceType.ROADM_OTN;
212 case ROUTER:
213 return DeviceType.ROUTER;
214 case SWITCH:
215 return DeviceType.SWITCH;
216 case VIRTUAL:
217 return DeviceType.VIRTUAL;
218
219 default:
220 log.warn("Unexpected Device.Type: {}", type);
221 return DeviceType.OTHER;
222 }
223 }
224
225 /**
226 * Translates gRPC PortDescription message to {@link PortDescription}.
227 *
228 * @param portDescription gRPC message
229 * @return {@link PortDescription}
230 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700231 public static PortDescription translate(org.onosproject.grpc.net.Port.PortDescription portDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800232 PortNumber number = PortNumber.fromString(portDescription.getPortNumber());
233 boolean isEnabled = portDescription.getIsEnabled();
234 Port.Type type = translate(portDescription.getType());
235 long portSpeed = portDescription.getPortSpeed();
Yuta HIGUCHI3680fab2016-08-22 21:19:55 -0700236 SparseAnnotations annotations = asAnnotations(portDescription.getAnnotationsMap());
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800237 // TODO How to deal with more specific Port...
238 return new DefaultPortDescription(number, isEnabled, type, portSpeed, annotations);
239 }
240
241 /**
242 * Translates {@link PortDescription} to gRPC PortDescription message.
243 *
244 * @param portDescription {@link PortDescription}
245 * @return gRPC PortDescription message
246 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700247 public static org.onosproject.grpc.net.Port.PortDescription translate(PortDescription portDescription) {
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700248 return org.onosproject.grpc.net.Port.PortDescription.newBuilder()
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800249 .setPortNumber(portDescription.portNumber().toString())
250 .setIsEnabled(portDescription.isEnabled())
251 .setType(translate(portDescription.type()))
252 .setPortSpeed(portDescription.portSpeed())
253 .putAllAnnotations(asMap(portDescription.annotations()))
254 .build();
255 }
256
257 /**
258 * Translates gRPC PortType to {@link Port.Type}.
259 *
260 * @param type gRPC message
261 * @return {@link Port.Type}
262 */
263 public static Port.Type translate(PortType type) {
264 switch (type) {
265 case COPPER:
266 return Type.COPPER;
267 case FIBER:
268 return Type.FIBER;
269 case OCH:
270 return Type.OCH;
271 case ODUCLT:
272 return Type.ODUCLT;
273 case OMS:
274 return Type.OMS;
275 case PACKET:
276 return Type.PACKET;
277 case VIRTUAL:
278 return Type.VIRTUAL;
279
280 case UNRECOGNIZED:
281 default:
282 log.warn("Unexpected PortType: {}", type);
283 return Type.COPPER;
284 }
285 }
286
287 /**
288 * Translates {@link Port.Type} to gRPC PortType.
289 *
290 * @param type {@link Port.Type}
291 * @return gRPC message
292 */
293 public static PortType translate(Port.Type type) {
294 switch (type) {
295 case COPPER:
296 return PortType.COPPER;
297 case FIBER:
298 return PortType.FIBER;
299 case OCH:
300 return PortType.OCH;
301 case ODUCLT:
302 return PortType.ODUCLT;
303 case OMS:
304 return PortType.OMS;
305 case PACKET:
306 return PortType.PACKET;
307 case VIRTUAL:
308 return PortType.VIRTUAL;
309
310 default:
311 log.warn("Unexpected Port.Type: {}", type);
312 return PortType.COPPER;
313 }
314 }
315
316 /**
317 * Translates gRPC PortStatistics message to {@link PortStatistics}.
318 *
319 * @param portStatistics gRPC PortStatistics message
320 * @return {@link PortStatistics}
321 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700322 public static PortStatistics translate(org.onosproject.grpc.net.Port.PortStatistics portStatistics) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800323 // TODO implement adding missing fields
324 return DefaultPortStatistics.builder()
325 .setPort(portStatistics.getPort())
326 .setPacketsReceived(portStatistics.getPacketsReceived())
327 .setPacketsSent(portStatistics.getPacketsSent())
328 .build();
329 }
330
331 /**
332 * Translates {@link PortStatistics} to gRPC PortStatistics message.
333 *
334 * @param portStatistics {@link PortStatistics}
335 * @return gRPC PortStatistics message
336 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700337 public static org.onosproject.grpc.net.Port.PortStatistics translate(PortStatistics portStatistics) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800338 // TODO implement adding missing fields
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700339 return org.onosproject.grpc.net.Port.PortStatistics.newBuilder()
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800340 .setPort(portStatistics.port())
341 .setPacketsReceived(portStatistics.packetsReceived())
342 .setPacketsSent(portStatistics.packetsSent())
343 .build();
344 }
345
346 // may be this can be moved to Annotation itself or AnnotationsUtils
347 /**
348 * Converts Annotations to Map of Strings.
349 *
350 * @param annotations {@link Annotations}
351 * @return Map of annotation key and values
352 */
353 public static Map<String, String> asMap(Annotations annotations) {
354 if (annotations instanceof DefaultAnnotations) {
355 return ((DefaultAnnotations) annotations).asMap();
356 }
357 Map<String, String> map = new HashMap<>();
358 annotations.keys()
359 .forEach(k -> map.put(k, annotations.value(k)));
360
361 return map;
362 }
363
364 // may be this can be moved to Annotation itself or AnnotationsUtils
365 /**
366 * Converts Map of Strings to {@link SparseAnnotations}.
367 *
368 * @param annotations Map of annotation key and values
369 * @return {@link SparseAnnotations}
370 */
371 public static SparseAnnotations asAnnotations(Map<String, String> annotations) {
372 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
373 annotations.entrySet().forEach(e -> {
374 if (e.getValue() != null) {
375 builder.set(e.getKey(), e.getValue());
376 } else {
377 builder.remove(e.getKey());
378 }
379 });
380 return builder.build();
381 }
382
383 // Utility class not intended for instantiation.
HIGUCHI Yuta06c1a3f2016-05-23 12:54:55 -0700384 private ProtobufUtils() {}
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800385}