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