Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [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 | |
| 17 | package org.onosproject.drivers.gnmi; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 21 | import com.google.common.util.concurrent.Futures; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 22 | import gnmi.Gnmi; |
| 23 | import gnmi.Gnmi.GetRequest; |
| 24 | import gnmi.Gnmi.GetResponse; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 25 | import org.onlab.packet.ChassisId; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 26 | import org.onosproject.gnmi.api.GnmiClient; |
| 27 | import org.onosproject.gnmi.api.GnmiController; |
| 28 | import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DefaultAnnotations; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 31 | import org.onosproject.net.Device; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 32 | import org.onosproject.net.PortNumber; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 33 | import org.onosproject.net.device.DefaultDeviceDescription; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 34 | import org.onosproject.net.device.DefaultPortDescription; |
| 35 | import org.onosproject.net.device.DeviceDescription; |
| 36 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 37 | import org.onosproject.net.device.PortDescription; |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 41 | import java.util.Collections; |
| 42 | import java.util.List; |
| 43 | import java.util.Map; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 44 | |
| 45 | import static gnmi.Gnmi.Path; |
| 46 | import static gnmi.Gnmi.PathElem; |
| 47 | import static gnmi.Gnmi.Update; |
| 48 | |
| 49 | /** |
| 50 | * Class that discovers the device description and ports of a device that |
| 51 | * supports the gNMI protocol and Openconfig models. |
| 52 | */ |
| 53 | public class OpenConfigGnmiDeviceDescriptionDiscovery |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 54 | extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController> |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 55 | implements DeviceDescriptionDiscovery { |
| 56 | |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 57 | private static final Logger log = LoggerFactory |
| 58 | .getLogger(OpenConfigGnmiDeviceDescriptionDiscovery.class); |
| 59 | |
pierventre | d03459c | 2021-06-01 12:26:36 +0200 | [diff] [blame] | 60 | private static final String LAST_CHANGE = "last-change"; |
Yi Tseng | ca5cfe7 | 2022-02-07 11:42:12 -0800 | [diff] [blame^] | 61 | private static final String SDK_PORT = "sdk-port"; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 62 | |
| 63 | private static final String UNKNOWN = "unknown"; |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 64 | |
pierventre | c19294d | 2022-02-02 22:33:41 +0100 | [diff] [blame] | 65 | // FIXME temporary solution will be removed when the |
| 66 | // transition to p4rt translation is completed |
| 67 | public static boolean readPortId = false; |
| 68 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 69 | public OpenConfigGnmiDeviceDescriptionDiscovery() { |
| 70 | super(GnmiController.class); |
| 71 | } |
| 72 | |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 73 | @Override |
| 74 | public DeviceDescription discoverDeviceDetails() { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 75 | return new DefaultDeviceDescription( |
| 76 | data().deviceId().uri(), |
| 77 | Device.Type.SWITCH, |
| 78 | data().driver().manufacturer(), |
| 79 | data().driver().hwVersion(), |
| 80 | data().driver().swVersion(), |
| 81 | UNKNOWN, |
| 82 | new ChassisId(), |
| 83 | true, |
| 84 | DefaultAnnotations.builder() |
| 85 | .set(AnnotationKeys.PROTOCOL, "gNMI") |
| 86 | .build()); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public List<PortDescription> discoverPortDetails() { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 91 | if (!setupBehaviour("discoverPortDetails()")) { |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 92 | return Collections.emptyList(); |
| 93 | } |
| 94 | log.debug("Discovering port details on device {}", handler().data().deviceId()); |
| 95 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 96 | final GetResponse response = Futures.getUnchecked(client.get(buildPortStateRequest())); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 97 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 98 | final Map<String, DefaultPortDescription.Builder> ports = Maps.newHashMap(); |
| 99 | final Map<String, DefaultAnnotations.Builder> annotations = Maps.newHashMap(); |
pierventre | 374e261 | 2022-01-31 13:14:46 +0100 | [diff] [blame] | 100 | final Map<String, PortNumber> portIds = Maps.newHashMap(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 101 | |
| 102 | // Creates port descriptions with port name and port number |
| 103 | response.getNotificationList() |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 104 | .forEach(notification -> { |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 105 | notification.getUpdateList().forEach(update -> { |
| 106 | // /interfaces/interface[name=ifName]/state/... |
| 107 | final String ifName = update.getPath().getElem(1) |
| 108 | .getKeyMap().get("name"); |
| 109 | if (!ports.containsKey(ifName)) { |
| 110 | ports.put(ifName, DefaultPortDescription.builder()); |
| 111 | annotations.put(ifName, DefaultAnnotations.builder()); |
| 112 | } |
| 113 | final DefaultPortDescription.Builder builder = ports.get(ifName); |
| 114 | final DefaultAnnotations.Builder annotationsBuilder = annotations.get(ifName); |
pierventre | 374e261 | 2022-01-31 13:14:46 +0100 | [diff] [blame] | 115 | parseInterfaceInfo(update, ifName, builder, annotationsBuilder, portIds); |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 116 | }); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 117 | }); |
| 118 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 119 | final List<PortDescription> portDescriptionList = Lists.newArrayList(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 120 | ports.forEach((key, value) -> { |
pierventre | d03459c | 2021-06-01 12:26:36 +0200 | [diff] [blame] | 121 | // For devices not providing last-change, we set it to 0 |
| 122 | final DefaultAnnotations.Builder annotationsBuilder = annotations.get(key); |
| 123 | if (!annotationsBuilder.build().keys().contains(LAST_CHANGE)) { |
| 124 | annotationsBuilder.set(LAST_CHANGE, String.valueOf(0)); |
| 125 | } |
pierventre | c19294d | 2022-02-02 22:33:41 +0100 | [diff] [blame] | 126 | /* Override port number if read port-id is enabled |
| 127 | and /interfaces/interface/state/id is available */ |
| 128 | if (readPortId && portIds.containsKey(key)) { |
pierventre | 374e261 | 2022-01-31 13:14:46 +0100 | [diff] [blame] | 129 | value.withPortNumber(portIds.get(key)); |
| 130 | } |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 131 | DefaultAnnotations annotation = annotations.get(key).build(); |
| 132 | portDescriptionList.add(value.annotations(annotation).build()); |
| 133 | }); |
pierventre | 374e261 | 2022-01-31 13:14:46 +0100 | [diff] [blame] | 134 | |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 135 | return portDescriptionList; |
| 136 | } |
| 137 | |
| 138 | private GetRequest buildPortStateRequest() { |
| 139 | Path path = Path.newBuilder() |
| 140 | .addElem(PathElem.newBuilder().setName("interfaces").build()) |
| 141 | .addElem(PathElem.newBuilder().setName("interface").putKey("name", "...").build()) |
| 142 | .addElem(PathElem.newBuilder().setName("state").build()) |
| 143 | .build(); |
| 144 | return GetRequest.newBuilder() |
| 145 | .addPath(path) |
| 146 | .setType(GetRequest.DataType.ALL) |
| 147 | .setEncoding(Gnmi.Encoding.PROTO) |
| 148 | .build(); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Parses the interface information. |
| 153 | * |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 154 | * @param update the update received |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 155 | */ |
| 156 | private void parseInterfaceInfo(Update update, |
| 157 | String ifName, |
| 158 | DefaultPortDescription.Builder builder, |
pierventre | 374e261 | 2022-01-31 13:14:46 +0100 | [diff] [blame] | 159 | DefaultAnnotations.Builder annotationsBuilder, |
| 160 | Map<String, PortNumber> portIds) { |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 161 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 162 | final Path path = update.getPath(); |
| 163 | final List<PathElem> elems = path.getElemList(); |
| 164 | final Gnmi.TypedValue val = update.getVal(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 165 | if (elems.size() == 4) { |
pierventre | 374e261 | 2022-01-31 13:14:46 +0100 | [diff] [blame] | 166 | /* /interfaces/interface/state/ifindex |
| 167 | /interfaces/interface/state/oper-status |
| 168 | /interfaces/interface/state/last-change |
| 169 | /interfaces/interface/state/id */ |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 170 | final String pathElemName = elems.get(3).getName(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 171 | switch (pathElemName) { |
| 172 | case "ifindex": // port number |
| 173 | builder.withPortNumber(PortNumber.portNumber(val.getUintVal(), ifName)); |
Yi Tseng | ca5cfe7 | 2022-02-07 11:42:12 -0800 | [diff] [blame^] | 174 | annotationsBuilder.set(SDK_PORT, String.valueOf(val.getUintVal())); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 175 | return; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 176 | case "oper-status": |
| 177 | builder.isEnabled(parseOperStatus(val.getStringVal())); |
pierventre | d03459c | 2021-06-01 12:26:36 +0200 | [diff] [blame] | 178 | return; |
| 179 | case "last-change": |
| 180 | annotationsBuilder.set(LAST_CHANGE, String.valueOf(val.getUintVal())); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 181 | return; |
pierventre | 374e261 | 2022-01-31 13:14:46 +0100 | [diff] [blame] | 182 | case "id": |
| 183 | /* Temporary stored in portIds and eventually substituted |
| 184 | when all updates have been processed. This is done because |
| 185 | there is no guarantee about the order of the updates delivery */ |
| 186 | portIds.put(ifName, PortNumber.portNumber(val.getUintVal(), ifName)); |
| 187 | return; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 188 | default: |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 189 | break; |
| 190 | } |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 191 | } else if (elems.size() == 5) { |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 192 | // /interfaces/interface/ethernet/config/port-speed |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 193 | final String pathElemName = elems.get(4).getName(); |
| 194 | if (pathElemName.equals("port-speed")) { |
| 195 | builder.portSpeed(parsePortSpeed(val.getStringVal())); |
| 196 | return; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 199 | log.debug("Unknown path when parsing interface info: {}", path); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | private boolean parseOperStatus(String operStatus) { |
| 203 | switch (operStatus) { |
| 204 | case "UP": |
| 205 | return true; |
| 206 | case "DOWN": |
| 207 | default: |
| 208 | return false; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | private long parsePortSpeed(String speed) { |
| 213 | log.debug("Speed from config {}", speed); |
| 214 | switch (speed) { |
| 215 | case "SPEED_10MB": |
| 216 | return 10; |
| 217 | case "SPEED_100MB": |
| 218 | return 100; |
| 219 | case "SPEED_1GB": |
| 220 | return 1000; |
| 221 | case "SPEED_10GB": |
| 222 | return 10000; |
| 223 | case "SPEED_25GB": |
| 224 | return 25000; |
| 225 | case "SPEED_40GB": |
| 226 | return 40000; |
| 227 | case "SPEED_50GB": |
| 228 | return 50000; |
| 229 | case "SPEED_100GB": |
| 230 | return 100000; |
| 231 | default: |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 232 | log.warn("Unrecognized port speed string '{}'", speed); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 233 | return 1000; |
| 234 | } |
| 235 | } |
| 236 | } |