blob: d061a43ab4d4e4b65c385eb1d967a8aa444ce28c [file] [log] [blame]
quan PHAM VAN32d70e52018-08-01 17:35:30 -07001/*
2 * Copyright 2018-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 */
16package org.onosproject.drivers.odtn;
17
18import com.google.common.annotations.VisibleForTesting;
19import com.google.common.collect.ImmutableList;
20import com.google.common.io.CharSource;
21import org.apache.commons.configuration.HierarchicalConfiguration;
22import org.apache.commons.configuration.XMLConfiguration;
23import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
24import org.onlab.packet.ChassisId;
25import org.onosproject.drivers.utilities.XmlConfigParser;
Eroshkin Ivan85f21c82019-12-12 15:20:34 +010026import org.onosproject.net.AnnotationKeys;
quan PHAM VAN32d70e52018-08-01 17:35:30 -070027import org.onosproject.net.DefaultAnnotations;
28import org.onosproject.net.Device;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Port.Type;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.device.DeviceDescription;
33import org.onosproject.net.device.DeviceDescriptionDiscovery;
34import org.onosproject.net.device.DefaultPortDescription.Builder;
35import org.onosproject.net.device.DefaultDeviceDescription;
36import org.onosproject.net.device.PortDescription;
37import org.onosproject.net.device.DefaultPortDescription;
38import org.onosproject.net.driver.AbstractHandlerBehaviour;
39import org.onosproject.netconf.NetconfController;
40import org.onosproject.netconf.NetconfDevice;
41import org.onosproject.netconf.NetconfException;
42import org.onosproject.netconf.NetconfSession;
43import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
qphamvan9aedb562019-02-19 14:38:53 +010044import org.onosproject.net.OchSignal;
45import org.onosproject.net.optical.device.OchPortHelper;
46import org.onosproject.net.OduSignalType;
47import org.onosproject.net.ChannelSpacing;
quan PHAM VAN32d70e52018-08-01 17:35:30 -070048import org.slf4j.Logger;
49
50import java.util.HashMap;
51import java.util.List;
52import java.util.Map;
53import java.util.Objects;
54import java.util.stream.Collectors;
55
56import static com.google.common.base.Preconditions.checkNotNull;
57import static org.slf4j.LoggerFactory.getLogger;
58
59/**
60 * Nokia OpenConfig based device and port discovery.
61 */
62public class NokiaOpenConfigDeviceDiscovery
63 extends AbstractHandlerBehaviour
64 implements OdtnDeviceDescriptionDiscovery, DeviceDescriptionDiscovery {
65
66 private static final Logger log = getLogger(NokiaOpenConfigDeviceDiscovery.class);
67 private static final String RPC_TAG_NETCONF_BASE = "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">";
68 private static final String RPC_CLOSE_TAG = "</rpc>";
69 private static final String OPTICAL_CHANNEL = "OCH";
70 private static final String TRANSCEIVER = "TRANSCEIVER";
71
72 //TODO should be loaded from a file config or something else
73 //The user and password are different from the user and password in the netconf-cfg file
74 private static final String USER_NAME = "admin";
75 private static final String PASSWORD = "admin";
76
77 @Override
78 public DeviceDescription discoverDeviceDetails() {
79 DeviceId did = data().deviceId();
80 NetconfSession ns = getNetconfSessionAndLogin(did, USER_NAME, PASSWORD);
81 if (ns == null) {
82 log.error("DiscoverDeviceDetails called with null session for {}", did);
83 return null;
84 }
85 log.info("Discovering device details {}", handler().data().deviceId());
86 String hwVersion = "1830", swVersion = "OpenAgent";
87 try {
88 String reply = ns.requestSync(buildGetSystemSoftwareRpc());
89 XMLConfiguration cfg = (XMLConfiguration) XmlConfigParser.loadXmlString(getDataOfRpcReply(reply));
90 hwVersion = cfg.getString("components.component.state.description");
91 swVersion = cfg.getString("components.component.state.version");
92 } catch (NetconfException e) {
93 log.error("Error discovering device details on {}", data().deviceId(), e);
94 }
95 return new DefaultDeviceDescription(handler().data().deviceId().uri(),
96 Device.Type.ROADM_OTN,
97 "NOKIA",
98 hwVersion,
99 swVersion,
100 "",
101 new ChassisId("1"));
102 }
103
104 @Override
105 public List<PortDescription> discoverPortDetails() {
106 DeviceId did = data().deviceId();
107 XMLConfiguration cfg = new XMLConfiguration();
108 NetconfSession ns = getNetconfSessionAndLogin(did, USER_NAME, PASSWORD);
109 if (ns == null) {
110 log.error("discoverPorts called with null session for {}", did);
111 return ImmutableList.of();
112 }
113 log.info("Discovering ports details {}", handler().data().deviceId());
114 try {
115 String reply = ns.requestSync(buildGetPlatformComponentsRpc());
116 String data = getDataOfRpcReply(reply);
117 if (data == null) {
118 log.error("No valid response found from {}:\n{}", did, reply);
119 return ImmutableList.of();
120 }
121 cfg.load(CharSource.wrap(data).openStream());
Eroshkin Ivan85f21c82019-12-12 15:20:34 +0100122 try {
123 ns.startSubscription();
124 log.info("Started subscription");
125 } catch (NetconfException e) {
126 log.error("NETCONF exception caught on {} when the subscription started \n {}",
127 data().deviceId(), e);
128 }
quan PHAM VAN32d70e52018-08-01 17:35:30 -0700129 return discoverPorts(cfg);
130 } catch (Exception e) {
131 log.error("Error discovering port details on {}", data().deviceId(), e);
132 return ImmutableList.of();
133 }
134 }
135
136 /**
137 * Parses port information from OpenConfig XML configuration.
138 *
139 * @param cfg tree where the root node is {@literal <data>}
140 * @return List of ports
141 */
142 @VisibleForTesting
143 private List<PortDescription> discoverPorts(XMLConfiguration cfg) {
144 // If we want to use XPath
145 cfg.setExpressionEngine(new XPathExpressionEngine());
146 // converting components into PortDescription.
147 List<HierarchicalConfiguration> components = cfg.configurationsAt("components/component");
148 return components.stream()
149 .map(this::toPortDescriptionInternal)
150 .filter(Objects::nonNull)
151 .collect(Collectors.toList());
152 }
153
154 /**
155 * Converts Component subtree to PortDescription.
156 *
157 * @param component subtree to parse
158 * @return PortDescription or null if component is not an ONOS Port
159 */
160 private PortDescription toPortDescriptionInternal(HierarchicalConfiguration component) {
qphamvan9aedb562019-02-19 14:38:53 +0100161 Map<String, String> annotations = new HashMap<>();
quan PHAM VAN32d70e52018-08-01 17:35:30 -0700162 String name = component.getString("name");
163 String type = component.getString("state/type");
164 checkNotNull(name, "name not found");
165 checkNotNull(type, "state/type not found");
qphamvan9aedb562019-02-19 14:38:53 +0100166 annotations.put(OdtnDeviceDescriptionDiscovery.OC_NAME, name);
167 annotations.put(OdtnDeviceDescriptionDiscovery.OC_TYPE, type);
168
169 component.configurationsAt("properties/property")
170 .forEach(property -> {
171 String pn = property.getString("name");
172 String pv = property.getString("state/value");
173 annotations.put(pn, pv);
174 });
175
quan PHAM VAN32d70e52018-08-01 17:35:30 -0700176 if (type.equals("oc-platform-types:PORT")) {
qphamvan9aedb562019-02-19 14:38:53 +0100177
quan PHAM VAN32d70e52018-08-01 17:35:30 -0700178 String subComponentName = component.getString("subcomponents/subcomponent/name");
179 String[] textStr = subComponentName.split("-");
180 String portComponentType = textStr[0];
181 String portComponentIndex = textStr[textStr.length - 1];
Eroshkin Ivan85f21c82019-12-12 15:20:34 +0100182 String portNumber = component.getString("name");
qphamvan9aedb562019-02-19 14:38:53 +0100183
184 if (portComponentType.equals(OPTICAL_CHANNEL)) {
185
186 annotations.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value());
187 annotations.putIfAbsent(ONOS_PORT_INDEX, portComponentIndex.toString());
188 annotations.putIfAbsent(CONNECTION_ID, "connection" + portComponentIndex.toString());
Eroshkin Ivan85f21c82019-12-12 15:20:34 +0100189 annotations.putIfAbsent(AnnotationKeys.PORT_NAME, portNumber);
qphamvan9aedb562019-02-19 14:38:53 +0100190
191 OchSignal signalId = OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1);
192 return OchPortHelper.ochPortDescription(
193 PortNumber.portNumber(Long.parseLong(portComponentIndex)),
194 true,
195 OduSignalType.ODU4, // TODO Client signal to be discovered
196 true,
197 signalId,
198 DefaultAnnotations.builder().putAll(annotations).build());
199
200 } else if (portComponentType.equals(TRANSCEIVER)) {
201
202 Builder builder = DefaultPortDescription.builder();
203 annotations.putIfAbsent(PORT_TYPE, OdtnPortType.CLIENT.value());
204 annotations.putIfAbsent(ONOS_PORT_INDEX, portComponentIndex.toString());
205 annotations.putIfAbsent(CONNECTION_ID, "connection" + portComponentIndex.toString());
Eroshkin Ivan85f21c82019-12-12 15:20:34 +0100206 annotations.putIfAbsent(AnnotationKeys.PORT_NAME, portNumber);
qphamvan9aedb562019-02-19 14:38:53 +0100207
208 builder.withPortNumber(PortNumber.portNumber(Long.parseLong(portComponentIndex), subComponentName));
209 builder.type(Type.PACKET);
210
211 builder.annotations(DefaultAnnotations.builder().putAll(annotations).build());
212 return builder.build();
213
quan PHAM VAN32d70e52018-08-01 17:35:30 -0700214 } else {
215 log.debug("Unknown port component type {}", type);
216 return null;
217 }
218 } else {
219 log.debug("Another component type {}", type);
220 return null;
221 }
quan PHAM VAN32d70e52018-08-01 17:35:30 -0700222 }
223
224 /**
225 * Login to the device by providing the correct user and password in order to configure the device
226 * Returns the NetconfSession with the device for which the method was called.
227 *
228 * @param deviceId device indetifier
229 * @param userName
230 * @param passwd
231 * @return The netconf session or null
232 */
233 private NetconfSession getNetconfSessionAndLogin(DeviceId deviceId, String userName, String passwd) {
234 NetconfController nc = handler().get(NetconfController.class);
235 NetconfDevice ndev = nc.getDevicesMap().get(deviceId);
236 if (ndev == null) {
237 log.debug("netconf device " + deviceId + " is not found, returning null session");
238 return null;
239 }
240 NetconfSession ns = ndev.getSession();
241 if (ns == null) {
242 log.error("discoverPorts called with null session for {}", deviceId);
243 return null;
244 }
245 try {
246 String reply = ns.requestSync(buildLoginRpc(userName, passwd));
247 if (reply.contains("<ok/>")) {
248 return ns;
249 } else {
250 log.debug(reply);
251 return null;
252 }
253 } catch (NetconfException e) {
254 log.error("can not login to device", e);
255 }
qphamvan9aedb562019-02-19 14:38:53 +0100256 return ns;
quan PHAM VAN32d70e52018-08-01 17:35:30 -0700257 }
258
259 //crude way of removing rpc-reply envelope (copy from netconf session)
260 private String getDataOfRpcReply(String rpcReply) {
261 String data = null;
262 int begin = rpcReply.indexOf("<data>");
263 int end = rpcReply.lastIndexOf("</data>");
264 if (begin != -1 && end != -1) {
265 data = (String) rpcReply.subSequence(begin, end + "</data>".length());
266 } else {
267 data = rpcReply;
268 }
269 return data;
270 }
271
272 /**
273 * Construct a rpc request message to get system software component.
274 *
275 * @return RPC message
276 */
277 private String buildGetSystemSoftwareRpc() {
278
279 StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
280 rpc.append("<get>");
281 rpc.append("<filter type='subtree'>");
282 rpc.append("<components xmlns=\"http://openconfig.net/yang/platform\">");
283 rpc.append("<component>");
284 rpc.append("<name>SYSTEM-SOFTWARE</name>");
285 rpc.append("</component>");
286 rpc.append("</components>");
287 rpc.append("</filter>");
288 rpc.append("</get>");
289 rpc.append(RPC_CLOSE_TAG);
290 return rpc.toString();
291 }
292
293 /**
294 * Construct a rpc request message to get openconfig platform components.
295 *
296 * @return RPC message
297 */
298 private String buildGetPlatformComponentsRpc() {
299 StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
300 rpc.append("<get>");
301 rpc.append("<filter type='subtree'>");
302 rpc.append("<components xmlns=\"http://openconfig.net/yang/platform\"></components>");
303 rpc.append("</filter>");
304 rpc.append("</get>");
305 rpc.append(RPC_CLOSE_TAG);
306 return rpc.toString();
307 }
308
309 /**
310 * Construct a rpc login message.
311 *
312 * @param userName
313 * @param passwd
314 * @return RPC message
315 */
316 private String buildLoginRpc(String userName, String passwd) {
317 StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
318 rpc.append("<login xmlns=\"http://nokia.com/yang/nokia-security\">");
Eroshkin Ivan85f21c82019-12-12 15:20:34 +0100319 rpc.append("<username>");
quan PHAM VAN32d70e52018-08-01 17:35:30 -0700320 rpc.append(userName);
Eroshkin Ivan85f21c82019-12-12 15:20:34 +0100321 rpc.append("</username>");
quan PHAM VAN32d70e52018-08-01 17:35:30 -0700322 rpc.append("<password>");
323 rpc.append(passwd);
324 rpc.append("</password>");
325 rpc.append("</login>");
326 rpc.append(RPC_CLOSE_TAG);
327 return rpc.toString();
328 }
329
330}