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