blob: 5c7a0a7e163889c7cf72574aea02e49832e20b8b [file] [log] [blame]
hirokieec31ef2018-05-21 07:34:25 -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;
hirokieec31ef2018-05-21 07:34:25 -070021import org.apache.commons.configuration.ConfigurationException;
22import org.apache.commons.configuration.HierarchicalConfiguration;
23import org.apache.commons.configuration.XMLConfiguration;
24import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
Andrea Campanella77e9b332018-11-29 13:33:19 -080025import org.onlab.packet.ChassisId;
hirokieec31ef2018-05-21 07:34:25 -070026import org.onosproject.net.DefaultAnnotations;
Andrea Campanella77e9b332018-11-29 13:33:19 -080027import org.onosproject.net.Device;
hirokieec31ef2018-05-21 07:34:25 -070028import org.onosproject.net.DeviceId;
29import org.onosproject.net.Port.Type;
30import org.onosproject.net.PortNumber;
Andrea Campanella77e9b332018-11-29 13:33:19 -080031import org.onosproject.net.device.DefaultDeviceDescription;
hirokieec31ef2018-05-21 07:34:25 -070032import org.onosproject.net.device.DefaultPortDescription;
33import org.onosproject.net.device.DefaultPortDescription.Builder;
34import org.onosproject.net.device.DeviceDescription;
hirokieec31ef2018-05-21 07:34:25 -070035import org.onosproject.net.device.PortDescription;
36import org.onosproject.net.driver.AbstractHandlerBehaviour;
37import org.onosproject.netconf.NetconfController;
38import org.onosproject.netconf.NetconfDevice;
39import org.onosproject.netconf.NetconfSession;
40import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
41import org.slf4j.Logger;
42
Andrea Campanellaa9cdaf62019-09-08 10:30:16 -070043import java.io.IOException;
44import java.util.HashMap;
45import java.util.List;
46import java.util.Map;
47import java.util.Objects;
48import java.util.Optional;
49import java.util.regex.Matcher;
50import java.util.regex.Pattern;
51import java.util.stream.Collectors;
52
hirokieec31ef2018-05-21 07:34:25 -070053import static com.google.common.base.Preconditions.checkNotNull;
54import static org.slf4j.LoggerFactory.getLogger;
55
56/**
57 * OpenConfig based device and port discovery.
58 */
59public class InfineraOpenConfigDeviceDiscovery
60 extends AbstractHandlerBehaviour
Andrea Campanella77e9b332018-11-29 13:33:19 -080061 implements OdtnDeviceDescriptionDiscovery {
hirokieec31ef2018-05-21 07:34:25 -070062
63 private static final Logger log = getLogger(InfineraOpenConfigDeviceDiscovery.class);
64
65 @Override
66 public DeviceDescription discoverDeviceDetails() {
Andrea Campanella77e9b332018-11-29 13:33:19 -080067 return new DefaultDeviceDescription(handler().data().deviceId().uri(),
Andrea Campanellaa9cdaf62019-09-08 10:30:16 -070068 Device.Type.TERMINAL_DEVICE, "Infinera",
69 "XT-3300", "unknown",
70 "unknown", new ChassisId());
hirokieec31ef2018-05-21 07:34:25 -070071 }
72
73 @Override
74 public List<PortDescription> discoverPortDetails() {
75 try {
76 return discoverPorts();
77 } catch (Exception e) {
78 log.error("Error discovering port details on {}", data().deviceId(), e);
79 return ImmutableList.of();
80 }
81 }
82
83 private List<PortDescription> discoverPorts() throws ConfigurationException, IOException {
84 DeviceId did = data().deviceId();
85 NetconfSession ns = Optional.ofNullable(handler().get(NetconfController.class))
86 .map(c -> c.getNetconfDevice(did))
87 .map(NetconfDevice::getSession)
88 .orElseThrow(() -> new IllegalStateException("No NetconfSession found for " + did));
89
90 // TODO convert this method into non-blocking form?
91
92 String reply = ns.asyncGet()
93 .join().toString();
94
95 // workaround until asyncGet().join() start failing exceptionally
96 String data = null;
97 if (reply.startsWith("<data")) {
98 data = reply;
99 }
100
101 if (data == null) {
102 log.error("No valid response found from {}:\n{}", did, reply);
103 return ImmutableList.of();
104 }
105
106 XMLConfiguration cfg = new XMLConfiguration();
107 cfg.load(CharSource.wrap(data).openStream());
108
109 return discoverPorts(cfg);
110 }
111
112 /**
113 * Parses port information from OpenConfig XML configuration.
114 *
115 * @param cfg tree where the root node is {@literal <data>}
116 * @return List of ports
117 */
118 @VisibleForTesting
119 protected List<PortDescription> discoverPorts(XMLConfiguration cfg) {
120 // If we want to use XPath
121 cfg.setExpressionEngine(new XPathExpressionEngine());
122
123 // converting components into PortDescription.
124 List<HierarchicalConfiguration> components = cfg.configurationsAt("interfaces/interface");
125 return components.stream()
126 .map(this::toPortDescription)
127 .filter(Objects::nonNull)
128 .collect(Collectors.toList());
129 }
130
131 // wrapper to make parsing exception safe
132 private PortDescription toPortDescription(HierarchicalConfiguration component) {
133 try {
134 return toPortDescriptionInternal(component);
135 } catch (Exception e) {
136 log.error("Unexpected exception parsing component {} on {}",
Andrea Campanellaa9cdaf62019-09-08 10:30:16 -0700137 component.getString("name"),
138 data().deviceId(), e);
hirokieec31ef2018-05-21 07:34:25 -0700139 return null;
140 }
141 }
142
143 /**
144 * Converts Component subtree to PortDescription.
145 *
146 * @param component subtree to parse
147 * @return PortDescription or null if component is not an ONOS Port
148 */
149 private PortDescription toPortDescriptionInternal(HierarchicalConfiguration component) {
150
151 // to access other part of <data> tree:
152 //log.warn("parent data Node: {}",
153 // ((SubnodeConfiguration) component).getParent().getRootNode().getName());
154
hirokieec31ef2018-05-21 07:34:25 -0700155 String name = component.getString("name");
156 checkNotNull(name);
157 if (!name.contains("GIGECLIENTCTP")) {
158 return null;
159 }
160
hirokieec31ef2018-05-21 07:34:25 -0700161 Builder builder = DefaultPortDescription.builder();
162
163 Map<String, String> props = new HashMap<>();
164 props.put(OdtnDeviceDescriptionDiscovery.OC_NAME, name);
165 props.put(OdtnDeviceDescriptionDiscovery.OC_TYPE, name);
166
167 Pattern clientPattern = Pattern.compile("GIGECLIENTCTP.1-A-2-T(\\d+)");
168 Pattern linePattern = Pattern.compile("GIGECLIENTCTP.1-L(\\d+)-1-1");
169 Matcher clientMatch = clientPattern.matcher(name);
170 Matcher lineMatch = linePattern.matcher(name);
171
172 if (clientMatch.find()) {
hirokif4ed5212018-05-26 22:39:38 -0700173 String num = clientMatch.group(1);
174 Integer connection = (Integer.parseInt(num) + 1) / 2;
hirokieec31ef2018-05-21 07:34:25 -0700175 props.putIfAbsent(PORT_TYPE, OdtnPortType.CLIENT.value());
hirokif4ed5212018-05-26 22:39:38 -0700176 props.putIfAbsent(CONNECTION_ID, "connection:" + connection.toString());
177 builder.withPortNumber(PortNumber.portNumber(Long.parseLong(num), name));
hirokieec31ef2018-05-21 07:34:25 -0700178 builder.type(Type.PACKET);
179 } else if (lineMatch.find()) {
hirokif4ed5212018-05-26 22:39:38 -0700180 String num = lineMatch.group(1);
hirokieec31ef2018-05-21 07:34:25 -0700181 props.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value());
hirokif4ed5212018-05-26 22:39:38 -0700182 props.putIfAbsent(CONNECTION_ID, "connection:" + num);
183 builder.withPortNumber(PortNumber.portNumber(100 + Long.parseLong(num), name));
hirokieec31ef2018-05-21 07:34:25 -0700184 builder.type(Type.OCH);
185 } else {
186 return null;
187 }
188
189 builder.annotations(DefaultAnnotations.builder().putAll(props).build());
190 return builder.build();
191
192 }
193
194}