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