Jian Li | d8e7207 | 2017-11-21 10:50:02 +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 | */ |
| 16 | package org.onosproject.incubator.protobuf.models.net.device; |
| 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; |
Jian Li | 21fa4fe | 2017-11-21 11:23:19 +0900 | [diff] [blame] | 22 | import org.onosproject.incubator.protobuf.models.net.AnnotationsTranslator; |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 23 | import org.onosproject.net.Device.Type; |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 24 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 25 | import org.onosproject.net.device.DeviceDescription; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.net.URI; |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * gRPC message conversion related utilities for device service. |
| 33 | */ |
| 34 | public final class DeviceProtoTranslator { |
| 35 | |
| 36 | private static final Logger log = LoggerFactory.getLogger(DeviceProtoTranslator.class); |
| 37 | |
| 38 | /** |
| 39 | * Translates gRPC DeviceDescription to {@link DeviceDescriptionProtoOuterClass}. |
| 40 | * |
| 41 | * @param deviceDescription gRPC message |
| 42 | * @return {@link DeviceDescriptionProtoOuterClass} |
| 43 | */ |
| 44 | public static DeviceDescription translate( |
| 45 | DeviceDescriptionProto deviceDescription) { |
| 46 | URI uri = URI.create(deviceDescription.getDeviceUri()); |
| 47 | Type type = translate(deviceDescription.getType()); |
| 48 | String manufacturer = deviceDescription.getManufacturer(); |
| 49 | String hwVersion = deviceDescription.getHwVersion(); |
| 50 | String swVersion = deviceDescription.getSwVersion(); |
| 51 | String serialNumber = deviceDescription.getSerialNumber(); |
| 52 | ChassisId chassis = new ChassisId(deviceDescription.getChassisId()); |
| 53 | boolean defaultAvailable = deviceDescription.getIsDefaultAvailable(); |
| 54 | return new DefaultDeviceDescription(uri, type, manufacturer, |
| 55 | hwVersion, swVersion, serialNumber, |
| 56 | chassis, |
| 57 | defaultAvailable, |
Jian Li | 21fa4fe | 2017-11-21 11:23:19 +0900 | [diff] [blame] | 58 | AnnotationsTranslator.asAnnotations(deviceDescription.getAnnotationsMap())); |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Translates {@link DeviceDescription} to gRPC DeviceDescription message. |
| 63 | * |
| 64 | * @param deviceDescription {@link DeviceDescription} |
| 65 | * @return gRPC DeviceDescription message |
| 66 | */ |
| 67 | public static DeviceDescriptionProto translate( |
| 68 | DeviceDescription deviceDescription) { |
| 69 | |
| 70 | return DeviceDescriptionProto.newBuilder() |
| 71 | .setDeviceUri(deviceDescription.deviceUri().toString()) |
| 72 | .setType(translate(deviceDescription.type())) |
| 73 | .setManufacturer(deviceDescription.manufacturer()) |
| 74 | .setHwVersion(deviceDescription.hwVersion()) |
| 75 | .setSwVersion(deviceDescription.swVersion()) |
| 76 | .setSerialNumber(deviceDescription.serialNumber()) |
| 77 | .setChassisId(deviceDescription.chassisId().toString()) |
| 78 | .setIsDefaultAvailable(deviceDescription.isDefaultAvailable()) |
Jian Li | 21fa4fe | 2017-11-21 11:23:19 +0900 | [diff] [blame] | 79 | .putAllAnnotations(AnnotationsTranslator.asMap(deviceDescription.annotations())) |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 80 | .build(); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Translates gRPC DeviceType to {@link Type}. |
| 86 | * |
| 87 | * @param type gRPC message |
| 88 | * @return {@link Type} |
| 89 | */ |
| 90 | public static Type translate(DeviceTypeProto type) { |
| 91 | switch (type) { |
| 92 | case BALANCER: |
| 93 | return Type.BALANCER; |
| 94 | case CONTROLLER: |
| 95 | return Type.CONTROLLER; |
| 96 | case FIBER_SWITCH: |
| 97 | return Type.FIBER_SWITCH; |
| 98 | case FIREWALL: |
| 99 | return Type.FIREWALL; |
| 100 | case IDS: |
| 101 | return Type.IDS; |
| 102 | case IPS: |
| 103 | return Type.IPS; |
| 104 | case MICROWAVE: |
| 105 | return Type.MICROWAVE; |
| 106 | case OTHER: |
| 107 | return Type.OTHER; |
| 108 | case OTN: |
| 109 | return Type.OTN; |
| 110 | case ROADM: |
| 111 | return Type.ROADM; |
| 112 | case ROADM_OTN: |
| 113 | return Type.ROADM_OTN; |
| 114 | case ROUTER: |
| 115 | return Type.ROUTER; |
| 116 | case SWITCH: |
| 117 | return Type.SWITCH; |
| 118 | case VIRTUAL_DEVICE: |
| 119 | return Type.VIRTUAL; |
| 120 | |
| 121 | case UNRECOGNIZED: |
| 122 | default: |
| 123 | log.warn("Unexpected DeviceType: {}", type); |
| 124 | return Type.OTHER; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Translates {@link Type} to gRPC DeviceType. |
| 130 | * |
| 131 | * @param type {@link Type} |
| 132 | * @return gRPC message |
| 133 | */ |
| 134 | public static DeviceTypeProto translate(Type type) { |
| 135 | switch (type) { |
| 136 | case BALANCER: |
| 137 | return DeviceTypeProto.BALANCER; |
| 138 | case CONTROLLER: |
| 139 | return DeviceTypeProto.CONTROLLER; |
| 140 | case FIBER_SWITCH: |
| 141 | return DeviceTypeProto.FIBER_SWITCH; |
| 142 | case FIREWALL: |
| 143 | return DeviceTypeProto.FIREWALL; |
| 144 | case IDS: |
| 145 | return DeviceTypeProto.IDS; |
| 146 | case IPS: |
| 147 | return DeviceTypeProto.IPS; |
| 148 | case MICROWAVE: |
| 149 | return DeviceTypeProto.MICROWAVE; |
| 150 | case OTHER: |
| 151 | return DeviceTypeProto.OTHER; |
| 152 | case OTN: |
| 153 | return DeviceTypeProto.OTN; |
| 154 | case ROADM: |
| 155 | return DeviceTypeProto.ROADM; |
| 156 | case ROADM_OTN: |
| 157 | return DeviceTypeProto.ROADM_OTN; |
| 158 | case ROUTER: |
| 159 | return DeviceTypeProto.ROUTER; |
| 160 | case SWITCH: |
| 161 | return DeviceTypeProto.SWITCH; |
| 162 | case VIRTUAL: |
| 163 | return DeviceTypeProto.VIRTUAL_DEVICE; |
| 164 | |
| 165 | default: |
| 166 | log.warn("Unexpected Device.Type: {}", type); |
| 167 | return DeviceTypeProto.OTHER; |
| 168 | } |
| 169 | } |
| 170 | |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 171 | // Utility class not intended for instantiation. |
| 172 | private DeviceProtoTranslator() { |
| 173 | } |
| 174 | } |