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 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 60 | private static final String LAST_CHANGE = "last-changed"; |
| 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 -> { |
| 99 | long timestamp = notification.getTimestamp(); |
| 100 | notification.getUpdateList().forEach(update -> { |
| 101 | // /interfaces/interface[name=ifName]/state/... |
| 102 | final String ifName = update.getPath().getElem(1) |
| 103 | .getKeyMap().get("name"); |
| 104 | if (!ports.containsKey(ifName)) { |
| 105 | ports.put(ifName, DefaultPortDescription.builder()); |
| 106 | annotations.put(ifName, DefaultAnnotations.builder()); |
| 107 | } |
| 108 | final DefaultPortDescription.Builder builder = ports.get(ifName); |
| 109 | final DefaultAnnotations.Builder annotationsBuilder = annotations.get(ifName); |
| 110 | parseInterfaceInfo(update, ifName, builder, annotationsBuilder, timestamp); |
| 111 | }); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 112 | }); |
| 113 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 114 | final List<PortDescription> portDescriptionList = Lists.newArrayList(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 115 | ports.forEach((key, value) -> { |
| 116 | DefaultAnnotations annotation = annotations.get(key).build(); |
| 117 | portDescriptionList.add(value.annotations(annotation).build()); |
| 118 | }); |
| 119 | return portDescriptionList; |
| 120 | } |
| 121 | |
| 122 | private GetRequest buildPortStateRequest() { |
| 123 | Path path = Path.newBuilder() |
| 124 | .addElem(PathElem.newBuilder().setName("interfaces").build()) |
| 125 | .addElem(PathElem.newBuilder().setName("interface").putKey("name", "...").build()) |
| 126 | .addElem(PathElem.newBuilder().setName("state").build()) |
| 127 | .build(); |
| 128 | return GetRequest.newBuilder() |
| 129 | .addPath(path) |
| 130 | .setType(GetRequest.DataType.ALL) |
| 131 | .setEncoding(Gnmi.Encoding.PROTO) |
| 132 | .build(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Parses the interface information. |
| 137 | * |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 138 | * @param update the update received |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 139 | */ |
| 140 | private void parseInterfaceInfo(Update update, |
| 141 | String ifName, |
| 142 | DefaultPortDescription.Builder builder, |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 143 | DefaultAnnotations.Builder annotationsBuilder, |
| 144 | long timestamp) { |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 145 | |
| 146 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 147 | final Path path = update.getPath(); |
| 148 | final List<PathElem> elems = path.getElemList(); |
| 149 | final Gnmi.TypedValue val = update.getVal(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 150 | if (elems.size() == 4) { |
| 151 | // /interfaces/interface/state/ifindex |
| 152 | // /interfaces/interface/state/oper-status |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 153 | final String pathElemName = elems.get(3).getName(); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 154 | switch (pathElemName) { |
| 155 | case "ifindex": // port number |
| 156 | builder.withPortNumber(PortNumber.portNumber(val.getUintVal(), ifName)); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 157 | return; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 158 | case "oper-status": |
| 159 | builder.isEnabled(parseOperStatus(val.getStringVal())); |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 160 | annotationsBuilder.set(LAST_CHANGE, String.valueOf(timestamp)); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 161 | return; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 162 | default: |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 163 | break; |
| 164 | } |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 165 | } else if (elems.size() == 5) { |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 166 | // /interfaces/interface/ethernet/config/port-speed |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 167 | final String pathElemName = elems.get(4).getName(); |
| 168 | if (pathElemName.equals("port-speed")) { |
| 169 | builder.portSpeed(parsePortSpeed(val.getStringVal())); |
| 170 | return; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 173 | log.debug("Unknown path when parsing interface info: {}", path); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private boolean parseOperStatus(String operStatus) { |
| 177 | switch (operStatus) { |
| 178 | case "UP": |
| 179 | return true; |
| 180 | case "DOWN": |
| 181 | default: |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private long parsePortSpeed(String speed) { |
| 187 | log.debug("Speed from config {}", speed); |
| 188 | switch (speed) { |
| 189 | case "SPEED_10MB": |
| 190 | return 10; |
| 191 | case "SPEED_100MB": |
| 192 | return 100; |
| 193 | case "SPEED_1GB": |
| 194 | return 1000; |
| 195 | case "SPEED_10GB": |
| 196 | return 10000; |
| 197 | case "SPEED_25GB": |
| 198 | return 25000; |
| 199 | case "SPEED_40GB": |
| 200 | return 40000; |
| 201 | case "SPEED_50GB": |
| 202 | return 50000; |
| 203 | case "SPEED_100GB": |
| 204 | return 100000; |
| 205 | default: |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 206 | log.warn("Unrecognized port speed string '{}'", speed); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 207 | return 1000; |
| 208 | } |
| 209 | } |
| 210 | } |