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