blob: 647acf7b24f6c63eb6834176fd26eb369cc27b67 [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 */
16package org.onosproject.incubator.rpc.grpc;
17
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
42import com.google.api.client.repackaged.com.google.common.annotations.Beta;
43
44/**
45 * gRPC message conversion related utilities.
46 */
47@Beta
48public final class GrpcDeviceUtils {
49
50 private static final Logger log = LoggerFactory.getLogger(GrpcDeviceUtils.class);
51
52 /**
53 * Translates gRPC enum MastershipRole to ONOS enum.
54 *
55 * @param role mastership role in gRPC enum
56 * @return equivalent in ONOS enum
57 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070058 public static MastershipRole translate(org.onosproject.grpc.net.Device.MastershipRole role) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080059 switch (role) {
60 case NONE:
61 return MastershipRole.NONE;
62 case MASTER:
63 return MastershipRole.MASTER;
64 case STANDBY:
65 return MastershipRole.STANDBY;
66 case UNRECOGNIZED:
67 log.warn("Unrecognized MastershipRole gRPC message: {}", role);
Ray Milkey676249c2015-12-18 09:27:03 -080068 return MastershipRole.NONE;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080069 default:
70 return MastershipRole.NONE;
71 }
72 }
73
74 /**
75 * Translates ONOS enum MastershipRole to gRPC enum.
76 *
77 * @param newRole ONOS' mastership role
78 * @return equivalent in gRPC message enum
79 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070080 public static org.onosproject.grpc.net.Device.MastershipRole translate(MastershipRole newRole) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080081 switch (newRole) {
82 case MASTER:
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070083 return org.onosproject.grpc.net.Device.MastershipRole.MASTER;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080084 case STANDBY:
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070085 return org.onosproject.grpc.net.Device.MastershipRole.STANDBY;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080086 case NONE:
87 default:
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070088 return org.onosproject.grpc.net.Device.MastershipRole.NONE;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080089 }
90 }
91
92
93 /**
94 * Translates gRPC DeviceDescription to {@link DeviceDescription}.
95 *
96 * @param deviceDescription gRPC message
97 * @return {@link DeviceDescription}
98 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070099 public static DeviceDescription translate(org.onosproject.grpc.net.Device.DeviceDescription deviceDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800100 URI uri = URI.create(deviceDescription.getDeviceUri());
101 Device.Type type = translate(deviceDescription.getType());
102 String manufacturer = deviceDescription.getManufacturer();
103 String hwVersion = deviceDescription.getHwVersion();
104 String swVersion = deviceDescription.getSwVersion();
105 String serialNumber = deviceDescription.getSerialNumber();
106 ChassisId chassis = new ChassisId(deviceDescription.getChassisId());
107 return new DefaultDeviceDescription(uri, type, manufacturer,
108 hwVersion, swVersion, serialNumber,
109 chassis,
110 asAnnotations(deviceDescription.getAnnotations()));
111 }
112
113 /**
114 * Translates {@link DeviceDescription} to gRPC DeviceDescription message.
115 *
116 * @param deviceDescription {@link DeviceDescription}
117 * @return gRPC DeviceDescription message
118 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700119 public static org.onosproject.grpc.net.Device.DeviceDescription translate(DeviceDescription deviceDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800120
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700121 return org.onosproject.grpc.net.Device.DeviceDescription.newBuilder()
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800122 .setDeviceUri(deviceDescription.deviceUri().toString())
123 .setType(translate(deviceDescription.type()))
124 .setManufacturer(deviceDescription.manufacturer())
125 .setHwVersion(deviceDescription.hwVersion())
126 .setSwVersion(deviceDescription.swVersion())
127 .setSerialNumber(deviceDescription.serialNumber())
128 .setChassisId(deviceDescription.chassisId().toString())
129 .putAllAnnotations(asMap(deviceDescription.annotations()))
130 .build();
131 }
132
133
134 /**
135 * Translates gRPC DeviceType to {@link Device.Type}.
136 *
137 * @param type gRPC message
138 * @return {@link Device.Type}
139 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700140 public static Device.Type translate(org.onosproject.grpc.net.Device.DeviceType type) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800141 switch (type) {
142 case BALANCER:
143 return Device.Type.BALANCER;
144 case CONTROLLER:
145 return Device.Type.CONTROLLER;
146 case FIBER_SWITCH:
147 return Device.Type.FIBER_SWITCH;
148 case FIREWALL:
149 return Device.Type.FIREWALL;
150 case IDS:
151 return Device.Type.IDS;
152 case IPS:
153 return Device.Type.IPS;
154 case MICROWAVE:
155 return Device.Type.MICROWAVE;
156 case OTHER:
157 return Device.Type.OTHER;
158 case OTN:
159 return Device.Type.OTN;
160 case ROADM:
161 return Device.Type.ROADM;
162 case ROADM_OTN:
163 return Device.Type.ROADM_OTN;
164 case ROUTER:
165 return Device.Type.ROUTER;
166 case SWITCH:
167 return Device.Type.SWITCH;
168 case VIRTUAL:
169 return Device.Type.VIRTUAL;
170
171 case UNRECOGNIZED:
172 default:
173 log.warn("Unexpected DeviceType: {}", type);
174 return Device.Type.OTHER;
175 }
176 }
177
178 /**
179 * Translates {@link Type} to gRPC DeviceType.
180 *
181 * @param type {@link Type}
182 * @return gRPC message
183 */
184 public static DeviceType translate(Device.Type type) {
185 switch (type) {
186 case BALANCER:
187 return DeviceType.BALANCER;
188 case CONTROLLER:
189 return DeviceType.CONTROLLER;
190 case FIBER_SWITCH:
191 return DeviceType.FIBER_SWITCH;
192 case FIREWALL:
193 return DeviceType.FIREWALL;
194 case IDS:
195 return DeviceType.IDS;
196 case IPS:
197 return DeviceType.IPS;
198 case MICROWAVE:
199 return DeviceType.MICROWAVE;
200 case OTHER:
201 return DeviceType.OTHER;
202 case OTN:
203 return DeviceType.OTN;
204 case ROADM:
205 return DeviceType.ROADM;
206 case ROADM_OTN:
207 return DeviceType.ROADM_OTN;
208 case ROUTER:
209 return DeviceType.ROUTER;
210 case SWITCH:
211 return DeviceType.SWITCH;
212 case VIRTUAL:
213 return DeviceType.VIRTUAL;
214
215 default:
216 log.warn("Unexpected Device.Type: {}", type);
217 return DeviceType.OTHER;
218 }
219 }
220
221 /**
222 * Translates gRPC PortDescription message to {@link PortDescription}.
223 *
224 * @param portDescription gRPC message
225 * @return {@link PortDescription}
226 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700227 public static PortDescription translate(org.onosproject.grpc.net.Port.PortDescription portDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800228 PortNumber number = PortNumber.fromString(portDescription.getPortNumber());
229 boolean isEnabled = portDescription.getIsEnabled();
230 Port.Type type = translate(portDescription.getType());
231 long portSpeed = portDescription.getPortSpeed();
232 SparseAnnotations annotations = asAnnotations(portDescription.getAnnotations());
233 // TODO How to deal with more specific Port...
234 return new DefaultPortDescription(number, isEnabled, type, portSpeed, annotations);
235 }
236
237 /**
238 * Translates {@link PortDescription} to gRPC PortDescription message.
239 *
240 * @param portDescription {@link PortDescription}
241 * @return gRPC PortDescription message
242 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700243 public static org.onosproject.grpc.net.Port.PortDescription translate(PortDescription portDescription) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800244 // TODO How to deal with more specific Port...
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700245 return org.onosproject.grpc.net.Port.PortDescription.newBuilder()
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800246 .setPortNumber(portDescription.portNumber().toString())
247 .setIsEnabled(portDescription.isEnabled())
248 .setType(translate(portDescription.type()))
249 .setPortSpeed(portDescription.portSpeed())
250 .putAllAnnotations(asMap(portDescription.annotations()))
251 .build();
252 }
253
254 /**
255 * Translates gRPC PortType to {@link Port.Type}.
256 *
257 * @param type gRPC message
258 * @return {@link Port.Type}
259 */
260 public static Port.Type translate(PortType type) {
261 switch (type) {
262 case COPPER:
263 return Type.COPPER;
264 case FIBER:
265 return Type.FIBER;
266 case OCH:
267 return Type.OCH;
268 case ODUCLT:
269 return Type.ODUCLT;
270 case OMS:
271 return Type.OMS;
272 case PACKET:
273 return Type.PACKET;
274 case VIRTUAL:
275 return Type.VIRTUAL;
276
277 case UNRECOGNIZED:
278 default:
279 log.warn("Unexpected PortType: {}", type);
280 return Type.COPPER;
281 }
282 }
283
284 /**
285 * Translates {@link Port.Type} to gRPC PortType.
286 *
287 * @param type {@link Port.Type}
288 * @return gRPC message
289 */
290 public static PortType translate(Port.Type type) {
291 switch (type) {
292 case COPPER:
293 return PortType.COPPER;
294 case FIBER:
295 return PortType.FIBER;
296 case OCH:
297 return PortType.OCH;
298 case ODUCLT:
299 return PortType.ODUCLT;
300 case OMS:
301 return PortType.OMS;
302 case PACKET:
303 return PortType.PACKET;
304 case VIRTUAL:
305 return PortType.VIRTUAL;
306
307 default:
308 log.warn("Unexpected Port.Type: {}", type);
309 return PortType.COPPER;
310 }
311 }
312
313 /**
314 * Translates gRPC PortStatistics message to {@link PortStatistics}.
315 *
316 * @param portStatistics gRPC PortStatistics message
317 * @return {@link PortStatistics}
318 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700319 public static PortStatistics translate(org.onosproject.grpc.net.Port.PortStatistics portStatistics) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800320 // TODO implement adding missing fields
321 return DefaultPortStatistics.builder()
322 .setPort(portStatistics.getPort())
323 .setPacketsReceived(portStatistics.getPacketsReceived())
324 .setPacketsSent(portStatistics.getPacketsSent())
325 .build();
326 }
327
328 /**
329 * Translates {@link PortStatistics} to gRPC PortStatistics message.
330 *
331 * @param portStatistics {@link PortStatistics}
332 * @return gRPC PortStatistics message
333 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700334 public static org.onosproject.grpc.net.Port.PortStatistics translate(PortStatistics portStatistics) {
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800335 // TODO implement adding missing fields
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700336 return org.onosproject.grpc.net.Port.PortStatistics.newBuilder()
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800337 .setPort(portStatistics.port())
338 .setPacketsReceived(portStatistics.packetsReceived())
339 .setPacketsSent(portStatistics.packetsSent())
340 .build();
341 }
342
343 // may be this can be moved to Annotation itself or AnnotationsUtils
344 /**
345 * Converts Annotations to Map of Strings.
346 *
347 * @param annotations {@link Annotations}
348 * @return Map of annotation key and values
349 */
350 public static Map<String, String> asMap(Annotations annotations) {
351 if (annotations instanceof DefaultAnnotations) {
352 return ((DefaultAnnotations) annotations).asMap();
353 }
354 Map<String, String> map = new HashMap<>();
355 annotations.keys()
356 .forEach(k -> map.put(k, annotations.value(k)));
357
358 return map;
359 }
360
361 // may be this can be moved to Annotation itself or AnnotationsUtils
362 /**
363 * Converts Map of Strings to {@link SparseAnnotations}.
364 *
365 * @param annotations Map of annotation key and values
366 * @return {@link SparseAnnotations}
367 */
368 public static SparseAnnotations asAnnotations(Map<String, String> annotations) {
369 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
370 annotations.entrySet().forEach(e -> {
371 if (e.getValue() != null) {
372 builder.set(e.getKey(), e.getValue());
373 } else {
374 builder.remove(e.getKey());
375 }
376 });
377 return builder.build();
378 }
379
380 // Utility class not intended for instantiation.
381 private GrpcDeviceUtils() {}
382}