blob: 495da8295b37fa312abfb7def8b8af09abad4d7d [file] [log] [blame]
Jian Lid8e72072017-11-21 10:50:02 +09001/*
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 */
16package org.onosproject.incubator.protobuf.models.net.device;
17
18import org.onlab.packet.ChassisId;
19import org.onosproject.grpc.net.device.models.DeviceDescriptionProtoOuterClass;
20import org.onosproject.grpc.net.device.models.DeviceDescriptionProtoOuterClass.DeviceDescriptionProto;
21import org.onosproject.grpc.net.device.models.DeviceEnumsProto.DeviceTypeProto;
22import org.onosproject.net.Annotations;
23import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.Device.Type;
25import org.onosproject.net.SparseAnnotations;
26import org.onosproject.net.device.DefaultDeviceDescription;
27import org.onosproject.net.device.DeviceDescription;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import java.net.URI;
32import java.util.HashMap;
33import java.util.Map;
34
35/**
36 * gRPC message conversion related utilities for device service.
37 */
38public final class DeviceProtoTranslator {
39
40 private static final Logger log = LoggerFactory.getLogger(DeviceProtoTranslator.class);
41
42 /**
43 * Translates gRPC DeviceDescription to {@link DeviceDescriptionProtoOuterClass}.
44 *
45 * @param deviceDescription gRPC message
46 * @return {@link DeviceDescriptionProtoOuterClass}
47 */
48 public static DeviceDescription translate(
49 DeviceDescriptionProto deviceDescription) {
50 URI uri = URI.create(deviceDescription.getDeviceUri());
51 Type type = translate(deviceDescription.getType());
52 String manufacturer = deviceDescription.getManufacturer();
53 String hwVersion = deviceDescription.getHwVersion();
54 String swVersion = deviceDescription.getSwVersion();
55 String serialNumber = deviceDescription.getSerialNumber();
56 ChassisId chassis = new ChassisId(deviceDescription.getChassisId());
57 boolean defaultAvailable = deviceDescription.getIsDefaultAvailable();
58 return new DefaultDeviceDescription(uri, type, manufacturer,
59 hwVersion, swVersion, serialNumber,
60 chassis,
61 defaultAvailable,
62 asAnnotations(deviceDescription.getAnnotationsMap()));
63 }
64
65 /**
66 * Translates {@link DeviceDescription} to gRPC DeviceDescription message.
67 *
68 * @param deviceDescription {@link DeviceDescription}
69 * @return gRPC DeviceDescription message
70 */
71 public static DeviceDescriptionProto translate(
72 DeviceDescription deviceDescription) {
73
74 return DeviceDescriptionProto.newBuilder()
75 .setDeviceUri(deviceDescription.deviceUri().toString())
76 .setType(translate(deviceDescription.type()))
77 .setManufacturer(deviceDescription.manufacturer())
78 .setHwVersion(deviceDescription.hwVersion())
79 .setSwVersion(deviceDescription.swVersion())
80 .setSerialNumber(deviceDescription.serialNumber())
81 .setChassisId(deviceDescription.chassisId().toString())
82 .setIsDefaultAvailable(deviceDescription.isDefaultAvailable())
83 .putAllAnnotations(asMap(deviceDescription.annotations()))
84 .build();
85 }
86
87
88 /**
89 * Translates gRPC DeviceType to {@link Type}.
90 *
91 * @param type gRPC message
92 * @return {@link Type}
93 */
94 public static Type translate(DeviceTypeProto type) {
95 switch (type) {
96 case BALANCER:
97 return Type.BALANCER;
98 case CONTROLLER:
99 return Type.CONTROLLER;
100 case FIBER_SWITCH:
101 return Type.FIBER_SWITCH;
102 case FIREWALL:
103 return Type.FIREWALL;
104 case IDS:
105 return Type.IDS;
106 case IPS:
107 return Type.IPS;
108 case MICROWAVE:
109 return Type.MICROWAVE;
110 case OTHER:
111 return Type.OTHER;
112 case OTN:
113 return Type.OTN;
114 case ROADM:
115 return Type.ROADM;
116 case ROADM_OTN:
117 return Type.ROADM_OTN;
118 case ROUTER:
119 return Type.ROUTER;
120 case SWITCH:
121 return Type.SWITCH;
122 case VIRTUAL_DEVICE:
123 return Type.VIRTUAL;
124
125 case UNRECOGNIZED:
126 default:
127 log.warn("Unexpected DeviceType: {}", type);
128 return Type.OTHER;
129 }
130 }
131
132 /**
133 * Translates {@link Type} to gRPC DeviceType.
134 *
135 * @param type {@link Type}
136 * @return gRPC message
137 */
138 public static DeviceTypeProto translate(Type type) {
139 switch (type) {
140 case BALANCER:
141 return DeviceTypeProto.BALANCER;
142 case CONTROLLER:
143 return DeviceTypeProto.CONTROLLER;
144 case FIBER_SWITCH:
145 return DeviceTypeProto.FIBER_SWITCH;
146 case FIREWALL:
147 return DeviceTypeProto.FIREWALL;
148 case IDS:
149 return DeviceTypeProto.IDS;
150 case IPS:
151 return DeviceTypeProto.IPS;
152 case MICROWAVE:
153 return DeviceTypeProto.MICROWAVE;
154 case OTHER:
155 return DeviceTypeProto.OTHER;
156 case OTN:
157 return DeviceTypeProto.OTN;
158 case ROADM:
159 return DeviceTypeProto.ROADM;
160 case ROADM_OTN:
161 return DeviceTypeProto.ROADM_OTN;
162 case ROUTER:
163 return DeviceTypeProto.ROUTER;
164 case SWITCH:
165 return DeviceTypeProto.SWITCH;
166 case VIRTUAL:
167 return DeviceTypeProto.VIRTUAL_DEVICE;
168
169 default:
170 log.warn("Unexpected Device.Type: {}", type);
171 return DeviceTypeProto.OTHER;
172 }
173 }
174
175
176 // may be this can be moved to Annotation itself or AnnotationsUtils
177 /**
178 * Converts Annotations to Map of Strings.
179 *
180 * @param annotations {@link Annotations}
181 * @return Map of annotation key and values
182 */
183 public static Map<String, String> asMap(Annotations annotations) {
184 if (annotations instanceof DefaultAnnotations) {
185 return ((DefaultAnnotations) annotations).asMap();
186 }
187 Map<String, String> map = new HashMap<>();
188 annotations.keys()
189 .forEach(k -> map.put(k, annotations.value(k)));
190
191 return map;
192 }
193
194 // may be this can be moved to Annotation itself or AnnotationsUtils
195 /**
196 * Converts Map of Strings to {@link SparseAnnotations}.
197 *
198 * @param annotations Map of annotation key and values
199 * @return {@link SparseAnnotations}
200 */
201 public static SparseAnnotations asAnnotations(Map<String, String> annotations) {
202 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
203 annotations.entrySet().forEach(e -> {
204 if (e.getValue() != null) {
205 builder.set(e.getKey(), e.getValue());
206 } else {
207 builder.remove(e.getKey());
208 }
209 });
210 return builder.build();
211 }
212
213 // Utility class not intended for instantiation.
214 private DeviceProtoTranslator() {
215 }
216}