blob: 6d233346502723f559eb6f9912018f0fcb6e2a4d [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;
Jian Li21fa4fe2017-11-21 11:23:19 +090022import org.onosproject.incubator.protobuf.models.net.AnnotationsTranslator;
Jian Lid8e72072017-11-21 10:50:02 +090023import org.onosproject.net.Device.Type;
Jian Lid8e72072017-11-21 10:50:02 +090024import org.onosproject.net.device.DefaultDeviceDescription;
25import org.onosproject.net.device.DeviceDescription;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.net.URI;
Jian Lid8e72072017-11-21 10:50:02 +090030
31/**
32 * gRPC message conversion related utilities for device service.
33 */
34public 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 Li21fa4fe2017-11-21 11:23:19 +090058 AnnotationsTranslator.asAnnotations(deviceDescription.getAnnotationsMap()));
Jian Lid8e72072017-11-21 10:50:02 +090059 }
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 Li21fa4fe2017-11-21 11:23:19 +090079 .putAllAnnotations(AnnotationsTranslator.asMap(deviceDescription.annotations()))
Jian Lid8e72072017-11-21 10:50:02 +090080 .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;
Andrea Campanellafa0f6cc2018-12-18 15:17:53 +0100118 case OLS:
119 return Type.OLS;
Jian Lid8e72072017-11-21 10:50:02 +0900120 case VIRTUAL_DEVICE:
121 return Type.VIRTUAL;
122
123 case UNRECOGNIZED:
124 default:
125 log.warn("Unexpected DeviceType: {}", type);
126 return Type.OTHER;
127 }
128 }
129
130 /**
131 * Translates {@link Type} to gRPC DeviceType.
132 *
133 * @param type {@link Type}
134 * @return gRPC message
135 */
136 public static DeviceTypeProto translate(Type type) {
137 switch (type) {
138 case BALANCER:
139 return DeviceTypeProto.BALANCER;
140 case CONTROLLER:
141 return DeviceTypeProto.CONTROLLER;
142 case FIBER_SWITCH:
143 return DeviceTypeProto.FIBER_SWITCH;
144 case FIREWALL:
145 return DeviceTypeProto.FIREWALL;
146 case IDS:
147 return DeviceTypeProto.IDS;
148 case IPS:
149 return DeviceTypeProto.IPS;
150 case MICROWAVE:
151 return DeviceTypeProto.MICROWAVE;
152 case OTHER:
153 return DeviceTypeProto.OTHER;
154 case OTN:
155 return DeviceTypeProto.OTN;
156 case ROADM:
157 return DeviceTypeProto.ROADM;
158 case ROADM_OTN:
159 return DeviceTypeProto.ROADM_OTN;
160 case ROUTER:
161 return DeviceTypeProto.ROUTER;
162 case SWITCH:
163 return DeviceTypeProto.SWITCH;
164 case VIRTUAL:
165 return DeviceTypeProto.VIRTUAL_DEVICE;
166
167 default:
168 log.warn("Unexpected Device.Type: {}", type);
169 return DeviceTypeProto.OTHER;
170 }
171 }
172
Jian Lid8e72072017-11-21 10:50:02 +0900173 // Utility class not intended for instantiation.
174 private DeviceProtoTranslator() {
175 }
176}