blob: 0f2e3bee1e91004140384edd84687bbc7f33d310 [file] [log] [blame]
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-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());
108 return new DefaultDeviceDescription(uri, type, manufacturer,
109 hwVersion, swVersion, serialNumber,
110 chassis,
111 asAnnotations(deviceDescription.getAnnotations()));
112 }
113
114 /**
115 * Translates {@link DeviceDescription} to gRPC DeviceDescription message.
116 *
117 * @param deviceDescription {@link DeviceDescription}
118 * @return gRPC DeviceDescription message
119 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700120 public static org.onosproject.grpc.net.Device.DeviceDescription translate(DeviceDescription deviceDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800121
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700122 return org.onosproject.grpc.net.Device.DeviceDescription.newBuilder()
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800123 .setDeviceUri(deviceDescription.deviceUri().toString())
124 .setType(translate(deviceDescription.type()))
125 .setManufacturer(deviceDescription.manufacturer())
126 .setHwVersion(deviceDescription.hwVersion())
127 .setSwVersion(deviceDescription.swVersion())
128 .setSerialNumber(deviceDescription.serialNumber())
129 .setChassisId(deviceDescription.chassisId().toString())
130 .putAllAnnotations(asMap(deviceDescription.annotations()))
131 .build();
132 }
133
134
135 /**
136 * Translates gRPC DeviceType to {@link Device.Type}.
137 *
138 * @param type gRPC message
139 * @return {@link Device.Type}
140 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700141 public static Device.Type translate(org.onosproject.grpc.net.Device.DeviceType type) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800142 switch (type) {
143 case BALANCER:
144 return Device.Type.BALANCER;
145 case CONTROLLER:
146 return Device.Type.CONTROLLER;
147 case FIBER_SWITCH:
148 return Device.Type.FIBER_SWITCH;
149 case FIREWALL:
150 return Device.Type.FIREWALL;
151 case IDS:
152 return Device.Type.IDS;
153 case IPS:
154 return Device.Type.IPS;
155 case MICROWAVE:
156 return Device.Type.MICROWAVE;
157 case OTHER:
158 return Device.Type.OTHER;
159 case OTN:
160 return Device.Type.OTN;
161 case ROADM:
162 return Device.Type.ROADM;
163 case ROADM_OTN:
164 return Device.Type.ROADM_OTN;
165 case ROUTER:
166 return Device.Type.ROUTER;
167 case SWITCH:
168 return Device.Type.SWITCH;
169 case VIRTUAL:
170 return Device.Type.VIRTUAL;
171
172 case UNRECOGNIZED:
173 default:
174 log.warn("Unexpected DeviceType: {}", type);
175 return Device.Type.OTHER;
176 }
177 }
178
179 /**
180 * Translates {@link Type} to gRPC DeviceType.
181 *
182 * @param type {@link Type}
183 * @return gRPC message
184 */
185 public static DeviceType translate(Device.Type type) {
186 switch (type) {
187 case BALANCER:
188 return DeviceType.BALANCER;
189 case CONTROLLER:
190 return DeviceType.CONTROLLER;
191 case FIBER_SWITCH:
192 return DeviceType.FIBER_SWITCH;
193 case FIREWALL:
194 return DeviceType.FIREWALL;
195 case IDS:
196 return DeviceType.IDS;
197 case IPS:
198 return DeviceType.IPS;
199 case MICROWAVE:
200 return DeviceType.MICROWAVE;
201 case OTHER:
202 return DeviceType.OTHER;
203 case OTN:
204 return DeviceType.OTN;
205 case ROADM:
206 return DeviceType.ROADM;
207 case ROADM_OTN:
208 return DeviceType.ROADM_OTN;
209 case ROUTER:
210 return DeviceType.ROUTER;
211 case SWITCH:
212 return DeviceType.SWITCH;
213 case VIRTUAL:
214 return DeviceType.VIRTUAL;
215
216 default:
217 log.warn("Unexpected Device.Type: {}", type);
218 return DeviceType.OTHER;
219 }
220 }
221
222 /**
223 * Translates gRPC PortDescription message to {@link PortDescription}.
224 *
225 * @param portDescription gRPC message
226 * @return {@link PortDescription}
227 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700228 public static PortDescription translate(org.onosproject.grpc.net.Port.PortDescription portDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800229 PortNumber number = PortNumber.fromString(portDescription.getPortNumber());
230 boolean isEnabled = portDescription.getIsEnabled();
231 Port.Type type = translate(portDescription.getType());
232 long portSpeed = portDescription.getPortSpeed();
233 SparseAnnotations annotations = asAnnotations(portDescription.getAnnotations());
234 // TODO How to deal with more specific Port...
235 return new DefaultPortDescription(number, isEnabled, type, portSpeed, annotations);
236 }
237
238 /**
239 * Translates {@link PortDescription} to gRPC PortDescription message.
240 *
241 * @param portDescription {@link PortDescription}
242 * @return gRPC PortDescription message
243 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700244 public static org.onosproject.grpc.net.Port.PortDescription translate(PortDescription portDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800245 // TODO How to deal with more specific Port...
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700246 return org.onosproject.grpc.net.Port.PortDescription.newBuilder()
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800247 .setPortNumber(portDescription.portNumber().toString())
248 .setIsEnabled(portDescription.isEnabled())
249 .setType(translate(portDescription.type()))
250 .setPortSpeed(portDescription.portSpeed())
251 .putAllAnnotations(asMap(portDescription.annotations()))
252 .build();
253 }
254
255 /**
256 * Translates gRPC PortType to {@link Port.Type}.
257 *
258 * @param type gRPC message
259 * @return {@link Port.Type}
260 */
261 public static Port.Type translate(PortType type) {
262 switch (type) {
263 case COPPER:
264 return Type.COPPER;
265 case FIBER:
266 return Type.FIBER;
267 case OCH:
268 return Type.OCH;
269 case ODUCLT:
270 return Type.ODUCLT;
271 case OMS:
272 return Type.OMS;
273 case PACKET:
274 return Type.PACKET;
275 case VIRTUAL:
276 return Type.VIRTUAL;
277
278 case UNRECOGNIZED:
279 default:
280 log.warn("Unexpected PortType: {}", type);
281 return Type.COPPER;
282 }
283 }
284
285 /**
286 * Translates {@link Port.Type} to gRPC PortType.
287 *
288 * @param type {@link Port.Type}
289 * @return gRPC message
290 */
291 public static PortType translate(Port.Type type) {
292 switch (type) {
293 case COPPER:
294 return PortType.COPPER;
295 case FIBER:
296 return PortType.FIBER;
297 case OCH:
298 return PortType.OCH;
299 case ODUCLT:
300 return PortType.ODUCLT;
301 case OMS:
302 return PortType.OMS;
303 case PACKET:
304 return PortType.PACKET;
305 case VIRTUAL:
306 return PortType.VIRTUAL;
307
308 default:
309 log.warn("Unexpected Port.Type: {}", type);
310 return PortType.COPPER;
311 }
312 }
313
314 /**
315 * Translates gRPC PortStatistics message to {@link PortStatistics}.
316 *
317 * @param portStatistics gRPC PortStatistics message
318 * @return {@link PortStatistics}
319 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700320 public static PortStatistics translate(org.onosproject.grpc.net.Port.PortStatistics portStatistics) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800321 // TODO implement adding missing fields
322 return DefaultPortStatistics.builder()
323 .setPort(portStatistics.getPort())
324 .setPacketsReceived(portStatistics.getPacketsReceived())
325 .setPacketsSent(portStatistics.getPacketsSent())
326 .build();
327 }
328
329 /**
330 * Translates {@link PortStatistics} to gRPC PortStatistics message.
331 *
332 * @param portStatistics {@link PortStatistics}
333 * @return gRPC PortStatistics message
334 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700335 public static org.onosproject.grpc.net.Port.PortStatistics translate(PortStatistics portStatistics) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800336 // TODO implement adding missing fields
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700337 return org.onosproject.grpc.net.Port.PortStatistics.newBuilder()
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800338 .setPort(portStatistics.port())
339 .setPacketsReceived(portStatistics.packetsReceived())
340 .setPacketsSent(portStatistics.packetsSent())
341 .build();
342 }
343
344 // may be this can be moved to Annotation itself or AnnotationsUtils
345 /**
346 * Converts Annotations to Map of Strings.
347 *
348 * @param annotations {@link Annotations}
349 * @return Map of annotation key and values
350 */
351 public static Map<String, String> asMap(Annotations annotations) {
352 if (annotations instanceof DefaultAnnotations) {
353 return ((DefaultAnnotations) annotations).asMap();
354 }
355 Map<String, String> map = new HashMap<>();
356 annotations.keys()
357 .forEach(k -> map.put(k, annotations.value(k)));
358
359 return map;
360 }
361
362 // may be this can be moved to Annotation itself or AnnotationsUtils
363 /**
364 * Converts Map of Strings to {@link SparseAnnotations}.
365 *
366 * @param annotations Map of annotation key and values
367 * @return {@link SparseAnnotations}
368 */
369 public static SparseAnnotations asAnnotations(Map<String, String> annotations) {
370 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
371 annotations.entrySet().forEach(e -> {
372 if (e.getValue() != null) {
373 builder.set(e.getKey(), e.getValue());
374 } else {
375 builder.remove(e.getKey());
376 }
377 });
378 return builder.build();
379 }
380
381 // Utility class not intended for instantiation.
HIGUCHI Yuta06c1a3f2016-05-23 12:54:55 -0700382 private ProtobufUtils() {}
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800383}