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