blob: eb41f4dea1ea949c7308958c09d886dd67287688 [file] [log] [blame]
Yi Tseng27851e32018-11-01 18:30:04 -07001/*
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
17package org.onosproject.drivers.gnmi;
18
19import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080021import com.google.common.util.concurrent.Futures;
Yi Tseng27851e32018-11-01 18:30:04 -070022import gnmi.Gnmi;
23import gnmi.Gnmi.GetRequest;
24import gnmi.Gnmi.GetResponse;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080025import org.onlab.packet.ChassisId;
Carmelo Casconec2be50a2019-04-10 00:15:39 -070026import org.onosproject.gnmi.api.GnmiClient;
27import org.onosproject.gnmi.api.GnmiController;
28import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080029import org.onosproject.net.AnnotationKeys;
Yi Tseng27851e32018-11-01 18:30:04 -070030import org.onosproject.net.DefaultAnnotations;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080031import org.onosproject.net.Device;
Yi Tseng27851e32018-11-01 18:30:04 -070032import org.onosproject.net.PortNumber;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080033import org.onosproject.net.device.DefaultDeviceDescription;
Yi Tseng27851e32018-11-01 18:30:04 -070034import org.onosproject.net.device.DefaultPortDescription;
35import org.onosproject.net.device.DeviceDescription;
36import org.onosproject.net.device.DeviceDescriptionDiscovery;
37import org.onosproject.net.device.PortDescription;
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
Yi Tseng27851e32018-11-01 18:30:04 -070041import java.util.Collections;
42import java.util.List;
43import java.util.Map;
Yi Tseng27851e32018-11-01 18:30:04 -070044
45import static gnmi.Gnmi.Path;
46import static gnmi.Gnmi.PathElem;
47import 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 */
53public class OpenConfigGnmiDeviceDescriptionDiscovery
Carmelo Casconec2be50a2019-04-10 00:15:39 -070054 extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController>
Yi Tseng27851e32018-11-01 18:30:04 -070055 implements DeviceDescriptionDiscovery {
56
Yi Tseng27851e32018-11-01 18:30:04 -070057 private static final Logger log = LoggerFactory
58 .getLogger(OpenConfigGnmiDeviceDescriptionDiscovery.class);
59
pierventre50696a72021-06-01 12:26:36 +020060 private static final String LAST_CHANGE = "last-change";
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080061
62 private static final String UNKNOWN = "unknown";
Yi Tseng59d5f3e2018-11-27 23:09:41 -080063
Carmelo Casconec2be50a2019-04-10 00:15:39 -070064 public OpenConfigGnmiDeviceDescriptionDiscovery() {
65 super(GnmiController.class);
66 }
67
Yi Tseng27851e32018-11-01 18:30:04 -070068 @Override
69 public DeviceDescription discoverDeviceDetails() {
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080070 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 Tseng27851e32018-11-01 18:30:04 -070082 }
83
84 @Override
85 public List<PortDescription> discoverPortDetails() {
Carmelo Casconec32976e2019-04-08 14:50:52 -070086 if (!setupBehaviour("discoverPortDetails()")) {
Yi Tseng27851e32018-11-01 18:30:04 -070087 return Collections.emptyList();
88 }
89 log.debug("Discovering port details on device {}", handler().data().deviceId());
90
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080091 final GetResponse response = Futures.getUnchecked(client.get(buildPortStateRequest()));
Yi Tseng27851e32018-11-01 18:30:04 -070092
Yi Tsengd7716482018-10-31 15:34:30 -070093 final Map<String, DefaultPortDescription.Builder> ports = Maps.newHashMap();
94 final Map<String, DefaultAnnotations.Builder> annotations = Maps.newHashMap();
Yi Tseng27851e32018-11-01 18:30:04 -070095
96 // Creates port descriptions with port name and port number
97 response.getNotificationList()
Yi Tseng59d5f3e2018-11-27 23:09:41 -080098 .forEach(notification -> {
Yi Tseng59d5f3e2018-11-27 23:09:41 -080099 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);
pierventre50696a72021-06-01 12:26:36 +0200109 parseInterfaceInfo(update, ifName, builder, annotationsBuilder);
Yi Tseng59d5f3e2018-11-27 23:09:41 -0800110 });
Yi Tseng27851e32018-11-01 18:30:04 -0700111 });
112
Yi Tsengd7716482018-10-31 15:34:30 -0700113 final List<PortDescription> portDescriptionList = Lists.newArrayList();
Yi Tseng27851e32018-11-01 18:30:04 -0700114 ports.forEach((key, value) -> {
pierventre50696a72021-06-01 12:26:36 +0200115 // 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 Tseng27851e32018-11-01 18:30:04 -0700120 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 Tsengd7716482018-10-31 15:34:30 -0700142 * @param update the update received
Yi Tseng27851e32018-11-01 18:30:04 -0700143 */
144 private void parseInterfaceInfo(Update update,
145 String ifName,
146 DefaultPortDescription.Builder builder,
pierventre50696a72021-06-01 12:26:36 +0200147 DefaultAnnotations.Builder annotationsBuilder) {
Yi Tseng27851e32018-11-01 18:30:04 -0700148
Yi Tsengd7716482018-10-31 15:34:30 -0700149 final Path path = update.getPath();
150 final List<PathElem> elems = path.getElemList();
151 final Gnmi.TypedValue val = update.getVal();
Yi Tseng27851e32018-11-01 18:30:04 -0700152 if (elems.size() == 4) {
153 // /interfaces/interface/state/ifindex
154 // /interfaces/interface/state/oper-status
pierventre50696a72021-06-01 12:26:36 +0200155 // /interfaces/interface/state/last-change
Yi Tsengd7716482018-10-31 15:34:30 -0700156 final String pathElemName = elems.get(3).getName();
Yi Tseng27851e32018-11-01 18:30:04 -0700157 switch (pathElemName) {
158 case "ifindex": // port number
159 builder.withPortNumber(PortNumber.portNumber(val.getUintVal(), ifName));
Yi Tsengd7716482018-10-31 15:34:30 -0700160 return;
Yi Tseng27851e32018-11-01 18:30:04 -0700161 case "oper-status":
162 builder.isEnabled(parseOperStatus(val.getStringVal()));
pierventre50696a72021-06-01 12:26:36 +0200163 return;
164 case "last-change":
165 annotationsBuilder.set(LAST_CHANGE, String.valueOf(val.getUintVal()));
Yi Tsengd7716482018-10-31 15:34:30 -0700166 return;
Yi Tseng27851e32018-11-01 18:30:04 -0700167 default:
Yi Tseng27851e32018-11-01 18:30:04 -0700168 break;
169 }
Yi Tsengd7716482018-10-31 15:34:30 -0700170 } else if (elems.size() == 5) {
Yi Tseng27851e32018-11-01 18:30:04 -0700171 // /interfaces/interface/ethernet/config/port-speed
Yi Tsengd7716482018-10-31 15:34:30 -0700172 final String pathElemName = elems.get(4).getName();
173 if (pathElemName.equals("port-speed")) {
174 builder.portSpeed(parsePortSpeed(val.getStringVal()));
175 return;
Yi Tseng27851e32018-11-01 18:30:04 -0700176 }
177 }
Yi Tsengd7716482018-10-31 15:34:30 -0700178 log.debug("Unknown path when parsing interface info: {}", path);
Yi Tseng27851e32018-11-01 18:30:04 -0700179 }
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 Tsengd7716482018-10-31 15:34:30 -0700211 log.warn("Unrecognized port speed string '{}'", speed);
Yi Tseng27851e32018-11-01 18:30:04 -0700212 return 1000;
213 }
214 }
215}