blob: 337a8a31f66468c492302e33515c1f00252f5d5c [file] [log] [blame]
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -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 static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.io.IOException;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
25import java.util.Objects;
26import java.util.Optional;
harjakb0518c42019-07-01 11:42:48 +020027import java.util.concurrent.atomic.AtomicInteger;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070028import java.util.stream.Collectors;
29
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;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070035import org.onosproject.net.DefaultAnnotations;
Andrea Campanella77e9b332018-11-29 13:33:19 -080036import org.onosproject.net.Device;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -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;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070041import org.onosproject.net.device.DefaultPortDescription;
42import org.onosproject.net.device.DefaultPortDescription.Builder;
43import org.onosproject.net.device.DeviceDescription;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070044import org.onosproject.net.device.PortDescription;
45import org.onosproject.net.driver.AbstractHandlerBehaviour;
46import org.onosproject.netconf.NetconfController;
47import org.onosproject.netconf.NetconfDevice;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070048import org.onosproject.netconf.NetconfSession;
49import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
50import org.slf4j.Logger;
51
52import com.google.common.annotations.VisibleForTesting;
53import com.google.common.collect.ImmutableList;
54import com.google.common.io.CharSource;
55
56/**
57 * OpenConfig based device and port discovery.
58 */
59public class OpenConfigDeviceDiscovery
60 extends AbstractHandlerBehaviour
Andrea Campanella77e9b332018-11-29 13:33:19 -080061 implements OdtnDeviceDescriptionDiscovery {
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070062
63 private static final Logger log = getLogger(OpenConfigDeviceDiscovery.class);
64
harjakb0518c42019-07-01 11:42:48 +020065 private static final AtomicInteger COUNTER = new AtomicInteger();
66
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070067 @Override
68 public DeviceDescription discoverDeviceDetails() {
Andrea Campanella77e9b332018-11-29 13:33:19 -080069 return new DefaultDeviceDescription(handler().data().deviceId().uri(),
Andrea Campanella1c24fb92018-12-20 16:43:59 +010070 Device.Type.TERMINAL_DEVICE, "unknown", "unknown",
Andrea Campanella77e9b332018-11-29 13:33:19 -080071 "unknown", "unknown", new ChassisId());
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070072 }
73
74 @Override
75 public List<PortDescription> discoverPortDetails() {
76 try {
77 return discoverPorts();
78 } catch (Exception e) {
79 log.error("Error discovering port details on {}", data().deviceId(), e);
80 return ImmutableList.of();
81 }
82 }
83
84 private List<PortDescription> discoverPorts() throws ConfigurationException, IOException {
85 DeviceId did = data().deviceId();
86 NetconfSession ns = Optional.ofNullable(handler().get(NetconfController.class))
87 .map(c -> c.getNetconfDevice(did))
88 .map(NetconfDevice::getSession)
89 .orElseThrow(() -> new IllegalStateException("No NetconfSession found for " + did));
90
91 // TODO convert this method into non-blocking form?
92
Yuta HIGUCHIf7333732018-05-14 16:07:37 -070093 String reply = ns.asyncGet()
94 .join().toString();
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070095
Yuta HIGUCHIf7333732018-05-14 16:07:37 -070096 // workaround until asyncGet().join() start failing exceptionally
97 String data = null;
98 if (reply.startsWith("<data")) {
99 data = reply;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700100 }
101
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700102 if (data == null) {
103 log.error("No valid response found from {}:\n{}", did, reply);
104 return ImmutableList.of();
105 }
106
107 XMLConfiguration cfg = new XMLConfiguration();
108 cfg.load(CharSource.wrap(data).openStream());
109
110 return discoverPorts(cfg);
111 }
112
113 /**
114 * Parses port information from OpenConfig XML configuration.
115 *
116 * @param cfg tree where the root node is {@literal <data>}
117 * @return List of ports
118 */
119 @VisibleForTesting
120 protected List<PortDescription> discoverPorts(XMLConfiguration cfg) {
121 // If we want to use XPath
122 cfg.setExpressionEngine(new XPathExpressionEngine());
123
124 // converting components into PortDescription.
125 List<HierarchicalConfiguration> components = cfg.configurationsAt("components/component");
126 return components.stream()
127 .map(this::toPortDescription)
128 .filter(Objects::nonNull)
129 .collect(Collectors.toList());
130 }
131
132 // wrapper to make parsing exception safe
133 private PortDescription toPortDescription(HierarchicalConfiguration component) {
134 try {
135 return toPortDescriptionInternal(component);
136 } catch (Exception e) {
137 log.error("Unexpected exception parsing component {} on {}",
138 component.getString("name"),
139 data().deviceId(), e);
140 return null;
141 }
142 }
143
144 /**
145 * Converts Component subtree to PortDescription.
146 *
147 * @param component subtree to parse
148 * @return PortDescription or null if component is not an ONOS Port
149 */
150 private PortDescription toPortDescriptionInternal(HierarchicalConfiguration component) {
151
152 // to access other part of <data> tree:
153 //log.warn("parent data Node: {}",
154 // ((SubnodeConfiguration) component).getParent().getRootNode().getName());
155
156 Map<String, String> props = new HashMap<>();
157
158 String name = component.getString("name");
159 String type = component.getString("state/type");
Yuta HIGUCHI318809a2018-05-25 11:27:35 -0700160 checkNotNull(name, "name not found");
161 checkNotNull(type, "state/type not found");
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700162 props.put(OdtnDeviceDescriptionDiscovery.OC_NAME, name);
163 props.put(OdtnDeviceDescriptionDiscovery.OC_TYPE, type);
164
165 component.configurationsAt("properties/property").forEach(prop -> {
166 String pName = prop.getString("name");
167 String pValue = prop.getString("config/value");
168 props.put(pName, pValue);
169 });
170
harjakb0518c42019-07-01 11:42:48 +0200171 PortNumber number = null;
172
Yuta HIGUCHI44e3a612018-05-11 15:03:39 -0700173 if (!props.containsKey(ONOS_PORT_INDEX)) {
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700174 log.info("DEBUG: Component {} does not include onos-index, skipping", name);
175 // ODTN: port must have onos-index property
harjakb0518c42019-07-01 11:42:48 +0200176 number = PortNumber.portNumber(COUNTER.getAndIncrement(), name);
177 } else {
178 number = PortNumber.portNumber(Long.parseLong(props.get(ONOS_PORT_INDEX)), name);
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700179 }
180
181 Builder builder = DefaultPortDescription.builder();
harjakb0518c42019-07-01 11:42:48 +0200182 builder.withPortNumber(number);
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700183
184 switch (type) {
harjakb0518c42019-07-01 11:42:48 +0200185 case "oc-platform-types:PORT": case "PORT":
186
187
188 case "oc-opt-types:OPTICAL_CHANNEL": case "OPTICAL CHANNEL":
hirokiec18d3a2018-05-16 15:27:37 -0700189 // TODO assign appropriate port type & annotations at some point
190 // for now we just need a Port with annotations
191 builder.type(Type.OCH);
192
Yuta HIGUCHIe4702af2018-05-18 17:17:34 -0700193 props.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value());
194
hirokiec18d3a2018-05-16 15:27:37 -0700195 // Just a heuristics to deal with simple transponder
196 // if the device declare odtn-connection-id, just use them
197 // if not assign same value to relevant ports types
198 props.putIfAbsent(CONNECTION_ID, "the-only-one");
199 break;
Yuta HIGUCHIe4702af2018-05-18 17:17:34 -0700200
harjakb0518c42019-07-01 11:42:48 +0200201 case "oc-platform-types:TRANSCEIVER": case "TRANSCEIVER":
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700202 // TODO assign appropriate port type & annotations at some point
203 // for now we just need a Port with annotations
204 builder.type(Type.PACKET);
Yuta HIGUCHI4b11fab2018-05-15 13:03:29 -0700205
Yuta HIGUCHIe4702af2018-05-18 17:17:34 -0700206 props.putIfAbsent(PORT_TYPE, OdtnPortType.CLIENT.value());
207
Yuta HIGUCHI4b11fab2018-05-15 13:03:29 -0700208 // Just a heuristics to deal with simple transponder
209 // if the device declare odtn-connection-id, just use them
210 // if not assign same value to relevant ports types
211 props.putIfAbsent(CONNECTION_ID, "the-only-one");
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700212 break;
213
214 default:
215 log.info("DEBUG: Unknown component type {}", type);
216 return null;
217 }
218
219 builder.annotations(DefaultAnnotations.builder().putAll(props).build());
220
221 return builder.build();
222
223 }
224
225}