blob: 347a0c8314fff6fa354daf6981cbbcc0fc8baefb [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;
34import org.onosproject.net.DefaultAnnotations;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.Port.Type;
37import org.onosproject.net.PortNumber;
38import org.onosproject.net.device.DefaultPortDescription;
39import org.onosproject.net.device.DefaultPortDescription.Builder;
40import org.onosproject.net.device.DeviceDescription;
41import org.onosproject.net.device.DeviceDescriptionDiscovery;
42import org.onosproject.net.device.PortDescription;
43import org.onosproject.net.driver.AbstractHandlerBehaviour;
44import org.onosproject.netconf.NetconfController;
45import org.onosproject.netconf.NetconfDevice;
46import org.onosproject.netconf.NetconfSession;
47import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
48import org.slf4j.Logger;
49
50import static com.google.common.base.Preconditions.checkNotNull;
51import static org.slf4j.LoggerFactory.getLogger;
52
53/**
54 * OpenConfig based device and port discovery.
55 */
56public class InfineraOpenConfigDeviceDiscovery
57 extends AbstractHandlerBehaviour
58 implements OdtnDeviceDescriptionDiscovery, DeviceDescriptionDiscovery {
59
60 private static final Logger log = getLogger(InfineraOpenConfigDeviceDiscovery.class);
61
62 @Override
63 public DeviceDescription discoverDeviceDetails() {
64 // TODO Auto-generated method stub
65 // Not really used right now
66 return null;
67 }
68
69 @Override
70 public List<PortDescription> discoverPortDetails() {
71 try {
72 return discoverPorts();
73 } catch (Exception e) {
74 log.error("Error discovering port details on {}", data().deviceId(), e);
75 return ImmutableList.of();
76 }
77 }
78
79 private List<PortDescription> discoverPorts() throws ConfigurationException, IOException {
80 DeviceId did = data().deviceId();
81 NetconfSession ns = Optional.ofNullable(handler().get(NetconfController.class))
82 .map(c -> c.getNetconfDevice(did))
83 .map(NetconfDevice::getSession)
84 .orElseThrow(() -> new IllegalStateException("No NetconfSession found for " + did));
85
86 // TODO convert this method into non-blocking form?
87
88 String reply = ns.asyncGet()
89 .join().toString();
90
91 // workaround until asyncGet().join() start failing exceptionally
92 String data = null;
93 if (reply.startsWith("<data")) {
94 data = reply;
95 }
96
97 if (data == null) {
98 log.error("No valid response found from {}:\n{}", did, reply);
99 return ImmutableList.of();
100 }
101
102 XMLConfiguration cfg = new XMLConfiguration();
103 cfg.load(CharSource.wrap(data).openStream());
104
105 return discoverPorts(cfg);
106 }
107
108 /**
109 * Parses port information from OpenConfig XML configuration.
110 *
111 * @param cfg tree where the root node is {@literal <data>}
112 * @return List of ports
113 */
114 @VisibleForTesting
115 protected List<PortDescription> discoverPorts(XMLConfiguration cfg) {
116 // If we want to use XPath
117 cfg.setExpressionEngine(new XPathExpressionEngine());
118
119 // converting components into PortDescription.
120 List<HierarchicalConfiguration> components = cfg.configurationsAt("interfaces/interface");
121 return components.stream()
122 .map(this::toPortDescription)
123 .filter(Objects::nonNull)
124 .collect(Collectors.toList());
125 }
126
127 // wrapper to make parsing exception safe
128 private PortDescription toPortDescription(HierarchicalConfiguration component) {
129 try {
130 return toPortDescriptionInternal(component);
131 } catch (Exception e) {
132 log.error("Unexpected exception parsing component {} on {}",
133 component.getString("name"),
134 data().deviceId(), e);
135 return null;
136 }
137 }
138
139 /**
140 * Converts Component subtree to PortDescription.
141 *
142 * @param component subtree to parse
143 * @return PortDescription or null if component is not an ONOS Port
144 */
145 private PortDescription toPortDescriptionInternal(HierarchicalConfiguration component) {
146
147 // to access other part of <data> tree:
148 //log.warn("parent data Node: {}",
149 // ((SubnodeConfiguration) component).getParent().getRootNode().getName());
150
151
152 String name = component.getString("name");
153 checkNotNull(name);
154 if (!name.contains("GIGECLIENTCTP")) {
155 return null;
156 }
157
158
159 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()) {
171 props.putIfAbsent(PORT_TYPE, OdtnPortType.CLIENT.value());
172 builder.withPortNumber(PortNumber.portNumber(Long.parseLong(clientMatch.group(1)), name));
173 builder.type(Type.PACKET);
174 } else if (lineMatch.find()) {
175 props.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value());
176 builder.withPortNumber(PortNumber.portNumber(100 + Long.parseLong(lineMatch.group(1)), name));
177 builder.type(Type.OCH);
178 } else {
179 return null;
180 }
181
182 builder.annotations(DefaultAnnotations.builder().putAll(props).build());
183 return builder.build();
184
185 }
186
187}