HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.incubator.rpc.grpc; |
| 17 | |
| 18 | import java.net.URI; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | import org.onlab.packet.ChassisId; |
| 23 | import org.onosproject.grpc.Device.DeviceType; |
| 24 | import org.onosproject.grpc.Port.PortType; |
| 25 | import org.onosproject.net.Annotations; |
| 26 | import org.onosproject.net.DefaultAnnotations; |
| 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.MastershipRole; |
| 29 | import org.onosproject.net.Port; |
| 30 | import org.onosproject.net.Port.Type; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.SparseAnnotations; |
| 33 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 34 | import org.onosproject.net.device.DefaultPortDescription; |
| 35 | import org.onosproject.net.device.DefaultPortStatistics; |
| 36 | import org.onosproject.net.device.DeviceDescription; |
| 37 | import org.onosproject.net.device.PortDescription; |
| 38 | import org.onosproject.net.device.PortStatistics; |
| 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
| 41 | |
| 42 | import com.google.api.client.repackaged.com.google.common.annotations.Beta; |
| 43 | |
| 44 | /** |
| 45 | * gRPC message conversion related utilities. |
| 46 | */ |
| 47 | @Beta |
| 48 | public 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); |
Ray Milkey | 676249c | 2015-12-18 09:27:03 -0800 | [diff] [blame] | 68 | return MastershipRole.NONE; |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 69 | 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 | */ |
| 80 | public static org.onosproject.grpc.Device.MastershipRole translate(MastershipRole newRole) { |
| 81 | switch (newRole) { |
| 82 | case MASTER: |
| 83 | return org.onosproject.grpc.Device.MastershipRole.MASTER; |
| 84 | case STANDBY: |
| 85 | return org.onosproject.grpc.Device.MastershipRole.STANDBY; |
| 86 | case NONE: |
| 87 | default: |
| 88 | return org.onosproject.grpc.Device.MastershipRole.NONE; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * Translates gRPC DeviceDescription to {@link DeviceDescription}. |
| 95 | * |
| 96 | * @param deviceDescription gRPC message |
| 97 | * @return {@link DeviceDescription} |
| 98 | */ |
| 99 | public static DeviceDescription translate(org.onosproject.grpc.Device.DeviceDescription deviceDescription) { |
| 100 | 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 | */ |
| 119 | public static org.onosproject.grpc.Device.DeviceDescription translate(DeviceDescription deviceDescription) { |
| 120 | |
| 121 | return org.onosproject.grpc.Device.DeviceDescription.newBuilder() |
| 122 | .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 | */ |
| 140 | public static Device.Type translate(org.onosproject.grpc.Device.DeviceType type) { |
| 141 | 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 | */ |
| 227 | public static PortDescription translate(org.onosproject.grpc.Port.PortDescription portDescription) { |
| 228 | 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 | */ |
| 243 | public static org.onosproject.grpc.Port.PortDescription translate(PortDescription portDescription) { |
| 244 | // TODO How to deal with more specific Port... |
| 245 | return org.onosproject.grpc.Port.PortDescription.newBuilder() |
| 246 | .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 | */ |
| 319 | public static PortStatistics translate(org.onosproject.grpc.Port.PortStatistics portStatistics) { |
| 320 | // 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 | */ |
| 334 | public static org.onosproject.grpc.Port.PortStatistics translate(PortStatistics portStatistics) { |
| 335 | // TODO implement adding missing fields |
| 336 | return org.onosproject.grpc.Port.PortStatistics.newBuilder() |
| 337 | .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 | } |