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"; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 61 | |
| 62 | private static final String UNKNOWN = "unknown"; |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 63 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 64 | public OpenConfigGnmiDeviceDescriptionDiscovery() { |
| 65 | super(GnmiController.class); |
| 66 | } |
| 67 | |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 68 | @Override |
| 69 | public DeviceDescription discoverDeviceDetails() { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 70 | return new DefaultDeviceDescription( |
| 71 | data().deviceId().uri(), |
| 72 | Device.Type.SWITCH, |
| 73 | data().driver().manufacturer(), |
| 74 | data().driver().hwVersion(), |
| 75 | data().driver().swVersion(), |
| 76 | UNKNOWN, |
| 77 | new ChassisId(), |
| 78 | true, |
| 79 | DefaultAnnotations.builder() |
| 80 | .set(AnnotationKeys.PROTOCOL, "gNMI") |
| 81 | .build()); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public List<PortDescription> discoverPortDetails() { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 86 | if (!setupBehaviour("discoverPortDetails()")) { |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 87 | return Collections.emptyList(); |
| 88 | } |
| 89 | log.debug("Discovering port details on device {}", handler().data().deviceId()); |
| 90 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 91 | final GetResponse response = Futures.getUnchecked(client.get(buildPortStateRequest())); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 92 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 93 | final Map<String, DefaultPortDescription.Builder> ports = Maps.newHashMap(); |
| 94 | final Map<String, DefaultAnnotations.Builder> annotations = Maps.newHashMap(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 95 | |
| 96 | // Creates port descriptions with port name and port number |
| 97 | response.getNotificationList() |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 98 | .forEach(notification -> { |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 99 | notification.getUpdateList().forEach(update -> { |
| 100 | // /interfaces/interface[name=ifName]/state/... |
| 101 | final String ifName = update.getPath().getElem(1) |
| 102 | .getKeyMap().get("name"); |
| 103 | if (!ports.containsKey(ifName)) { |
| 104 | ports.put(ifName, DefaultPortDescription.builder()); |
| 105 | annotations.put(ifName, DefaultAnnotations.builder()); |
| 106 | } |
| 107 | final DefaultPortDescription.Builder builder = ports.get(ifName); |
| 108 | final DefaultAnnotations.Builder annotationsBuilder = annotations.get(ifName); |
pierventre | d03459c | 2021-06-01 12:26:36 +0200 | [diff] [blame] | 109 | parseInterfaceInfo(update, ifName, builder, annotationsBuilder); |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 110 | }); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 111 | }); |
| 112 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 113 | final List<PortDescription> portDescriptionList = Lists.newArrayList(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 114 | ports.forEach((key, value) -> { |
pierventre | d03459c | 2021-06-01 12:26:36 +0200 | [diff] [blame] | 115 | // For devices not providing last-change, we set it to 0 |
| 116 | final DefaultAnnotations.Builder annotationsBuilder = annotations.get(key); |
| 117 | if (!annotationsBuilder.build().keys().contains(LAST_CHANGE)) { |
| 118 | annotationsBuilder.set(LAST_CHANGE, String.valueOf(0)); |
| 119 | } |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 120 | DefaultAnnotations annotation = annotations.get(key).build(); |
| 121 | portDescriptionList.add(value.annotations(annotation).build()); |
| 122 | }); |
| 123 | return portDescriptionList; |
| 124 | } |
| 125 | |
| 126 | private GetRequest buildPortStateRequest() { |
| 127 | Path path = Path.newBuilder() |
| 128 | .addElem(PathElem.newBuilder().setName("interfaces").build()) |
| 129 | .addElem(PathElem.newBuilder().setName("interface").putKey("name", "...").build()) |
| 130 | .addElem(PathElem.newBuilder().setName("state").build()) |
| 131 | .build(); |
| 132 | return GetRequest.newBuilder() |
| 133 | .addPath(path) |
| 134 | .setType(GetRequest.DataType.ALL) |
| 135 | .setEncoding(Gnmi.Encoding.PROTO) |
| 136 | .build(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Parses the interface information. |
| 141 | * |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 142 | * @param update the update received |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 143 | */ |
| 144 | private void parseInterfaceInfo(Update update, |
| 145 | String ifName, |
| 146 | DefaultPortDescription.Builder builder, |
pierventre | d03459c | 2021-06-01 12:26:36 +0200 | [diff] [blame] | 147 | DefaultAnnotations.Builder annotationsBuilder) { |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 148 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 149 | final Path path = update.getPath(); |
| 150 | final List<PathElem> elems = path.getElemList(); |
| 151 | final Gnmi.TypedValue val = update.getVal(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 152 | if (elems.size() == 4) { |
| 153 | // /interfaces/interface/state/ifindex |
| 154 | // /interfaces/interface/state/oper-status |
pierventre | d03459c | 2021-06-01 12:26:36 +0200 | [diff] [blame] | 155 | // /interfaces/interface/state/last-change |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 156 | final String pathElemName = elems.get(3).getName(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 157 | switch (pathElemName) { |
| 158 | case "ifindex": // port number |
| 159 | builder.withPortNumber(PortNumber.portNumber(val.getUintVal(), ifName)); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 160 | return; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 161 | case "oper-status": |
| 162 | builder.isEnabled(parseOperStatus(val.getStringVal())); |
pierventre | d03459c | 2021-06-01 12:26:36 +0200 | [diff] [blame] | 163 | return; |
| 164 | case "last-change": |
| 165 | annotationsBuilder.set(LAST_CHANGE, String.valueOf(val.getUintVal())); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 166 | return; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 167 | default: |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 168 | break; |
| 169 | } |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 170 | } else if (elems.size() == 5) { |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 171 | // /interfaces/interface/ethernet/config/port-speed |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 172 | final String pathElemName = elems.get(4).getName(); |
| 173 | if (pathElemName.equals("port-speed")) { |
| 174 | builder.portSpeed(parsePortSpeed(val.getStringVal())); |
| 175 | return; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 178 | log.debug("Unknown path when parsing interface info: {}", path); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | private boolean parseOperStatus(String operStatus) { |
| 182 | switch (operStatus) { |
| 183 | case "UP": |
| 184 | return true; |
| 185 | case "DOWN": |
| 186 | default: |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | private long parsePortSpeed(String speed) { |
| 192 | log.debug("Speed from config {}", speed); |
| 193 | switch (speed) { |
| 194 | case "SPEED_10MB": |
| 195 | return 10; |
| 196 | case "SPEED_100MB": |
| 197 | return 100; |
| 198 | case "SPEED_1GB": |
| 199 | return 1000; |
| 200 | case "SPEED_10GB": |
| 201 | return 10000; |
| 202 | case "SPEED_25GB": |
| 203 | return 25000; |
| 204 | case "SPEED_40GB": |
| 205 | return 40000; |
| 206 | case "SPEED_50GB": |
| 207 | return 50000; |
| 208 | case "SPEED_100GB": |
| 209 | return 100000; |
| 210 | default: |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 211 | log.warn("Unrecognized port speed string '{}'", speed); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 212 | return 1000; |
| 213 | } |
| 214 | } |
| 215 | } |