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