blob: 7045b0c2d92eabe8c2dfac4f3018fe28a9cd4771 [file] [log] [blame]
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -08001/*
2 * Copyright 2015 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.rpc.grpc;
17
18import java.net.URI;
19import java.util.HashMap;
20import java.util.Map;
21
22import org.onlab.packet.ChassisId;
23import org.onosproject.grpc.Device.DeviceType;
24import org.onosproject.grpc.Port.PortType;
25import 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 */
58 public static MastershipRole translate(org.onosproject.grpc.Device.MastershipRole role) {
59 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);
68 default:
69 return MastershipRole.NONE;
70 }
71 }
72
73 /**
74 * Translates ONOS enum MastershipRole to gRPC enum.
75 *
76 * @param newRole ONOS' mastership role
77 * @return equivalent in gRPC message enum
78 */
79 public static org.onosproject.grpc.Device.MastershipRole translate(MastershipRole newRole) {
80 switch (newRole) {
81 case MASTER:
82 return org.onosproject.grpc.Device.MastershipRole.MASTER;
83 case STANDBY:
84 return org.onosproject.grpc.Device.MastershipRole.STANDBY;
85 case NONE:
86 default:
87 return org.onosproject.grpc.Device.MastershipRole.NONE;
88 }
89 }
90
91
92 /**
93 * Translates gRPC DeviceDescription to {@link DeviceDescription}.
94 *
95 * @param deviceDescription gRPC message
96 * @return {@link DeviceDescription}
97 */
98 public static DeviceDescription translate(org.onosproject.grpc.Device.DeviceDescription deviceDescription) {
99 URI uri = URI.create(deviceDescription.getDeviceUri());
100 Device.Type type = translate(deviceDescription.getType());
101 String manufacturer = deviceDescription.getManufacturer();
102 String hwVersion = deviceDescription.getHwVersion();
103 String swVersion = deviceDescription.getSwVersion();
104 String serialNumber = deviceDescription.getSerialNumber();
105 ChassisId chassis = new ChassisId(deviceDescription.getChassisId());
106 return new DefaultDeviceDescription(uri, type, manufacturer,
107 hwVersion, swVersion, serialNumber,
108 chassis,
109 asAnnotations(deviceDescription.getAnnotations()));
110 }
111
112 /**
113 * Translates {@link DeviceDescription} to gRPC DeviceDescription message.
114 *
115 * @param deviceDescription {@link DeviceDescription}
116 * @return gRPC DeviceDescription message
117 */
118 public static org.onosproject.grpc.Device.DeviceDescription translate(DeviceDescription deviceDescription) {
119
120 return org.onosproject.grpc.Device.DeviceDescription.newBuilder()
121 .setDeviceUri(deviceDescription.deviceUri().toString())
122 .setType(translate(deviceDescription.type()))
123 .setManufacturer(deviceDescription.manufacturer())
124 .setHwVersion(deviceDescription.hwVersion())
125 .setSwVersion(deviceDescription.swVersion())
126 .setSerialNumber(deviceDescription.serialNumber())
127 .setChassisId(deviceDescription.chassisId().toString())
128 .putAllAnnotations(asMap(deviceDescription.annotations()))
129 .build();
130 }
131
132
133 /**
134 * Translates gRPC DeviceType to {@link Device.Type}.
135 *
136 * @param type gRPC message
137 * @return {@link Device.Type}
138 */
139 public static Device.Type translate(org.onosproject.grpc.Device.DeviceType type) {
140 switch (type) {
141 case BALANCER:
142 return Device.Type.BALANCER;
143 case CONTROLLER:
144 return Device.Type.CONTROLLER;
145 case FIBER_SWITCH:
146 return Device.Type.FIBER_SWITCH;
147 case FIREWALL:
148 return Device.Type.FIREWALL;
149 case IDS:
150 return Device.Type.IDS;
151 case IPS:
152 return Device.Type.IPS;
153 case MICROWAVE:
154 return Device.Type.MICROWAVE;
155 case OTHER:
156 return Device.Type.OTHER;
157 case OTN:
158 return Device.Type.OTN;
159 case ROADM:
160 return Device.Type.ROADM;
161 case ROADM_OTN:
162 return Device.Type.ROADM_OTN;
163 case ROUTER:
164 return Device.Type.ROUTER;
165 case SWITCH:
166 return Device.Type.SWITCH;
167 case VIRTUAL:
168 return Device.Type.VIRTUAL;
169
170 case UNRECOGNIZED:
171 default:
172 log.warn("Unexpected DeviceType: {}", type);
173 return Device.Type.OTHER;
174 }
175 }
176
177 /**
178 * Translates {@link Type} to gRPC DeviceType.
179 *
180 * @param type {@link Type}
181 * @return gRPC message
182 */
183 public static DeviceType translate(Device.Type type) {
184 switch (type) {
185 case BALANCER:
186 return DeviceType.BALANCER;
187 case CONTROLLER:
188 return DeviceType.CONTROLLER;
189 case FIBER_SWITCH:
190 return DeviceType.FIBER_SWITCH;
191 case FIREWALL:
192 return DeviceType.FIREWALL;
193 case IDS:
194 return DeviceType.IDS;
195 case IPS:
196 return DeviceType.IPS;
197 case MICROWAVE:
198 return DeviceType.MICROWAVE;
199 case OTHER:
200 return DeviceType.OTHER;
201 case OTN:
202 return DeviceType.OTN;
203 case ROADM:
204 return DeviceType.ROADM;
205 case ROADM_OTN:
206 return DeviceType.ROADM_OTN;
207 case ROUTER:
208 return DeviceType.ROUTER;
209 case SWITCH:
210 return DeviceType.SWITCH;
211 case VIRTUAL:
212 return DeviceType.VIRTUAL;
213
214 default:
215 log.warn("Unexpected Device.Type: {}", type);
216 return DeviceType.OTHER;
217 }
218 }
219
220 /**
221 * Translates gRPC PortDescription message to {@link PortDescription}.
222 *
223 * @param portDescription gRPC message
224 * @return {@link PortDescription}
225 */
226 public static PortDescription translate(org.onosproject.grpc.Port.PortDescription portDescription) {
227 PortNumber number = PortNumber.fromString(portDescription.getPortNumber());
228 boolean isEnabled = portDescription.getIsEnabled();
229 Port.Type type = translate(portDescription.getType());
230 long portSpeed = portDescription.getPortSpeed();
231 SparseAnnotations annotations = asAnnotations(portDescription.getAnnotations());
232 // TODO How to deal with more specific Port...
233 return new DefaultPortDescription(number, isEnabled, type, portSpeed, annotations);
234 }
235
236 /**
237 * Translates {@link PortDescription} to gRPC PortDescription message.
238 *
239 * @param portDescription {@link PortDescription}
240 * @return gRPC PortDescription message
241 */
242 public static org.onosproject.grpc.Port.PortDescription translate(PortDescription portDescription) {
243 // TODO How to deal with more specific Port...
244 return org.onosproject.grpc.Port.PortDescription.newBuilder()
245 .setPortNumber(portDescription.portNumber().toString())
246 .setIsEnabled(portDescription.isEnabled())
247 .setType(translate(portDescription.type()))
248 .setPortSpeed(portDescription.portSpeed())
249 .putAllAnnotations(asMap(portDescription.annotations()))
250 .build();
251 }
252
253 /**
254 * Translates gRPC PortType to {@link Port.Type}.
255 *
256 * @param type gRPC message
257 * @return {@link Port.Type}
258 */
259 public static Port.Type translate(PortType type) {
260 switch (type) {
261 case COPPER:
262 return Type.COPPER;
263 case FIBER:
264 return Type.FIBER;
265 case OCH:
266 return Type.OCH;
267 case ODUCLT:
268 return Type.ODUCLT;
269 case OMS:
270 return Type.OMS;
271 case PACKET:
272 return Type.PACKET;
273 case VIRTUAL:
274 return Type.VIRTUAL;
275
276 case UNRECOGNIZED:
277 default:
278 log.warn("Unexpected PortType: {}", type);
279 return Type.COPPER;
280 }
281 }
282
283 /**
284 * Translates {@link Port.Type} to gRPC PortType.
285 *
286 * @param type {@link Port.Type}
287 * @return gRPC message
288 */
289 public static PortType translate(Port.Type type) {
290 switch (type) {
291 case COPPER:
292 return PortType.COPPER;
293 case FIBER:
294 return PortType.FIBER;
295 case OCH:
296 return PortType.OCH;
297 case ODUCLT:
298 return PortType.ODUCLT;
299 case OMS:
300 return PortType.OMS;
301 case PACKET:
302 return PortType.PACKET;
303 case VIRTUAL:
304 return PortType.VIRTUAL;
305
306 default:
307 log.warn("Unexpected Port.Type: {}", type);
308 return PortType.COPPER;
309 }
310 }
311
312 /**
313 * Translates gRPC PortStatistics message to {@link PortStatistics}.
314 *
315 * @param portStatistics gRPC PortStatistics message
316 * @return {@link PortStatistics}
317 */
318 public static PortStatistics translate(org.onosproject.grpc.Port.PortStatistics portStatistics) {
319 // TODO implement adding missing fields
320 return DefaultPortStatistics.builder()
321 .setPort(portStatistics.getPort())
322 .setPacketsReceived(portStatistics.getPacketsReceived())
323 .setPacketsSent(portStatistics.getPacketsSent())
324 .build();
325 }
326
327 /**
328 * Translates {@link PortStatistics} to gRPC PortStatistics message.
329 *
330 * @param portStatistics {@link PortStatistics}
331 * @return gRPC PortStatistics message
332 */
333 public static org.onosproject.grpc.Port.PortStatistics translate(PortStatistics portStatistics) {
334 // TODO implement adding missing fields
335 return org.onosproject.grpc.Port.PortStatistics.newBuilder()
336 .setPort(portStatistics.port())
337 .setPacketsReceived(portStatistics.packetsReceived())
338 .setPacketsSent(portStatistics.packetsSent())
339 .build();
340 }
341
342 // may be this can be moved to Annotation itself or AnnotationsUtils
343 /**
344 * Converts Annotations to Map of Strings.
345 *
346 * @param annotations {@link Annotations}
347 * @return Map of annotation key and values
348 */
349 public static Map<String, String> asMap(Annotations annotations) {
350 if (annotations instanceof DefaultAnnotations) {
351 return ((DefaultAnnotations) annotations).asMap();
352 }
353 Map<String, String> map = new HashMap<>();
354 annotations.keys()
355 .forEach(k -> map.put(k, annotations.value(k)));
356
357 return map;
358 }
359
360 // may be this can be moved to Annotation itself or AnnotationsUtils
361 /**
362 * Converts Map of Strings to {@link SparseAnnotations}.
363 *
364 * @param annotations Map of annotation key and values
365 * @return {@link SparseAnnotations}
366 */
367 public static SparseAnnotations asAnnotations(Map<String, String> annotations) {
368 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
369 annotations.entrySet().forEach(e -> {
370 if (e.getValue() != null) {
371 builder.set(e.getKey(), e.getValue());
372 } else {
373 builder.remove(e.getKey());
374 }
375 });
376 return builder.build();
377 }
378
379 // Utility class not intended for instantiation.
380 private GrpcDeviceUtils() {}
381}