blob: 90d10f52efb4cb83912e5553299ba954699ab7e7 [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;
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 Lid8e72072017-11-21 10:50:02 +0900171 // Utility class not intended for instantiation.
172 private DeviceProtoTranslator() {
173 }
174}