blob: 0d0e0facf8c7621807d717b0a6223d63a2baaa3b [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.onosproject.grpc.net.device.models.PortDescriptionProtoOuterClass.PortDescriptionProto;
19import org.onosproject.grpc.net.device.models.PortEnumsProto;
20import org.onosproject.grpc.net.device.models.PortStatisticsProtoOuterClass;
21import org.onosproject.grpc.net.device.models.PortStatisticsProtoOuterClass.PortStatisticsProto;
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.Port;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.SparseAnnotations;
26import org.onosproject.net.device.DefaultPortDescription;
27import org.onosproject.net.device.DefaultPortStatistics;
28import org.onosproject.net.device.PortDescription;
29import org.onosproject.net.device.PortStatistics;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Jian Lid8e72072017-11-21 10:50:02 +090033/**
34 * gRPC message conversion related utilities for port service.
35 */
36public final class PortProtoTranslator {
37
38 private static final Logger log = LoggerFactory.getLogger(PortProtoTranslator.class);
39
40 /**
41 * Translates gRPC PortDescription message to {@link PortDescription}.
42 *
43 * @param portDescription gRPC message
44 * @return {@link PortDescription}
45 */
46 public static PortDescription translate(PortDescriptionProto portDescription) {
47 PortNumber number = PortNumber.fromString(portDescription.getPortNumber());
48 boolean isEnabled = portDescription.getIsEnabled();
49 Port.Type type = translate(portDescription.getType());
50 long portSpeed = portDescription.getPortSpeed();
Jian Li21fa4fe2017-11-21 11:23:19 +090051 SparseAnnotations annotations = AnnotationsTranslator.asAnnotations(portDescription.getAnnotationsMap());
Jian Lid8e72072017-11-21 10:50:02 +090052 // TODO How to deal with more specific Port...
53 return new DefaultPortDescription(number, isEnabled, type, portSpeed, annotations);
54 }
55
56 /**
57 * Translates {@link PortDescription} to gRPC PortDescription message.
58 *
59 * @param portDescription {@link PortDescription}
60 * @return gRPC PortDescription message
61 */
62 public static PortDescriptionProto translate(PortDescription portDescription) {
63 return PortDescriptionProto.newBuilder()
64 .setPortNumber(portDescription.portNumber().toString())
65 .setIsEnabled(portDescription.isEnabled())
66 .setType(translate(portDescription.type()))
67 .setPortSpeed(portDescription.portSpeed())
Jian Li21fa4fe2017-11-21 11:23:19 +090068 .putAllAnnotations(AnnotationsTranslator.asMap(portDescription.annotations()))
Jian Lid8e72072017-11-21 10:50:02 +090069 .build();
70 }
71
72 /**
73 * Translates gRPC PortType to {@link Port.Type}.
74 *
75 * @param type gRPC message
76 * @return {@link Port.Type}
77 */
78 public static Port.Type translate(PortEnumsProto.PortTypeProto type) {
79 switch (type) {
80 case COPPER:
81 return Port.Type.COPPER;
82 case FIBER:
83 return Port.Type.FIBER;
84 case OCH:
85 return Port.Type.OCH;
86 case ODUCLT:
87 return Port.Type.ODUCLT;
88 case OMS:
89 return Port.Type.OMS;
90 case PACKET:
91 return Port.Type.PACKET;
92 case VIRTUAL_PORT:
93 return Port.Type.VIRTUAL;
94
95 case UNRECOGNIZED:
96 default:
97 log.warn("Unexpected PortType: {}", type);
98 return Port.Type.COPPER;
99 }
100 }
101
102 /**
103 * Translates {@link Port.Type} to gRPC PortType.
104 *
105 * @param type {@link Port.Type}
106 * @return gRPC message
107 */
108 public static PortEnumsProto.PortTypeProto translate(Port.Type type) {
109 switch (type) {
110 case COPPER:
111 return PortEnumsProto.PortTypeProto.COPPER;
112 case FIBER:
113 return PortEnumsProto.PortTypeProto.FIBER;
114 case OCH:
115 return PortEnumsProto.PortTypeProto.OCH;
116 case ODUCLT:
117 return PortEnumsProto.PortTypeProto.ODUCLT;
118 case OMS:
119 return PortEnumsProto.PortTypeProto.OMS;
120 case PACKET:
121 return PortEnumsProto.PortTypeProto.PACKET;
122 case VIRTUAL:
123 return PortEnumsProto.PortTypeProto.VIRTUAL_PORT;
124
125 default:
126 log.warn("Unexpected Port.Type: {}", type);
127 return PortEnumsProto.PortTypeProto.COPPER;
128 }
129 }
130
131 /**
132 * Translates gRPC PortStatistics message to {@link PortStatisticsProtoOuterClass}.
133 *
134 * @param portStatistics gRPC PortStatistics message
135 * @return {@link PortStatisticsProtoOuterClass}
136 */
137 public static PortStatistics translate(PortStatisticsProto portStatistics) {
138 // TODO implement adding missing fields
139 return DefaultPortStatistics.builder()
140 .setPort(portStatistics.getPort())
141 .setPacketsReceived(portStatistics.getPacketsReceived())
142 .setPacketsSent(portStatistics.getPacketsSent())
143 .build();
144 }
145
146 /**
147 * Translates {@link PortStatistics} to gRPC PortStatistics message.
148 *
149 * @param portStatistics {@link PortStatistics}
150 * @return gRPC PortStatistics message
151 */
152 public static PortStatisticsProto translate(PortStatistics portStatistics) {
153 // TODO implement adding missing fields
154 return PortStatisticsProto.newBuilder()
155 .setPort(portStatistics.port())
156 .setPacketsReceived(portStatistics.packetsReceived())
157 .setPacketsSent(portStatistics.packetsSent())
158 .build();
159 }
160
Jian Lid8e72072017-11-21 10:50:02 +0900161 // Utility class not intended for instantiation.
162 private PortProtoTranslator() {}
163}