blob: 210a237d8561c89108746a84ee1cd2f8c2b5b852 [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
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080060 private static final String LAST_CHANGE = "last-changed";
61
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 -> {
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 Tseng27851e32018-11-01 18:30:04 -0700112 });
113
Yi Tsengd7716482018-10-31 15:34:30 -0700114 final List<PortDescription> portDescriptionList = Lists.newArrayList();
Yi Tseng27851e32018-11-01 18:30:04 -0700115 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 Tsengd7716482018-10-31 15:34:30 -0700138 * @param update the update received
Yi Tseng27851e32018-11-01 18:30:04 -0700139 */
140 private void parseInterfaceInfo(Update update,
141 String ifName,
142 DefaultPortDescription.Builder builder,
Yi Tseng59d5f3e2018-11-27 23:09:41 -0800143 DefaultAnnotations.Builder annotationsBuilder,
144 long timestamp) {
Yi Tseng27851e32018-11-01 18:30:04 -0700145
146
Yi Tsengd7716482018-10-31 15:34:30 -0700147 final Path path = update.getPath();
148 final List<PathElem> elems = path.getElemList();
149 final Gnmi.TypedValue val = update.getVal();
Yi Tseng27851e32018-11-01 18:30:04 -0700150 if (elems.size() == 4) {
151 // /interfaces/interface/state/ifindex
152 // /interfaces/interface/state/oper-status
Yi Tsengd7716482018-10-31 15:34:30 -0700153 final String pathElemName = elems.get(3).getName();
Yi Tseng27851e32018-11-01 18:30:04 -0700154 switch (pathElemName) {
155 case "ifindex": // port number
156 builder.withPortNumber(PortNumber.portNumber(val.getUintVal(), ifName));
Yi Tsengd7716482018-10-31 15:34:30 -0700157 return;
Yi Tseng27851e32018-11-01 18:30:04 -0700158 case "oper-status":
159 builder.isEnabled(parseOperStatus(val.getStringVal()));
Yi Tseng59d5f3e2018-11-27 23:09:41 -0800160 annotationsBuilder.set(LAST_CHANGE, String.valueOf(timestamp));
Yi Tsengd7716482018-10-31 15:34:30 -0700161 return;
Yi Tseng27851e32018-11-01 18:30:04 -0700162 default:
Yi Tseng27851e32018-11-01 18:30:04 -0700163 break;
164 }
Yi Tsengd7716482018-10-31 15:34:30 -0700165 } else if (elems.size() == 5) {
Yi Tseng27851e32018-11-01 18:30:04 -0700166 // /interfaces/interface/ethernet/config/port-speed
Yi Tsengd7716482018-10-31 15:34:30 -0700167 final String pathElemName = elems.get(4).getName();
168 if (pathElemName.equals("port-speed")) {
169 builder.portSpeed(parsePortSpeed(val.getStringVal()));
170 return;
Yi Tseng27851e32018-11-01 18:30:04 -0700171 }
172 }
Yi Tsengd7716482018-10-31 15:34:30 -0700173 log.debug("Unknown path when parsing interface info: {}", path);
Yi Tseng27851e32018-11-01 18:30:04 -0700174 }
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 Tsengd7716482018-10-31 15:34:30 -0700206 log.warn("Unrecognized port speed string '{}'", speed);
Yi Tseng27851e32018-11-01 18:30:04 -0700207 return 1000;
208 }
209 }
210}