blob: 086c5ab2aadf39b4f0ef40eb61704807f79c26b0 [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;
27import java.util.stream.Collectors;
28
29import org.apache.commons.configuration.ConfigurationException;
30import org.apache.commons.configuration.HierarchicalConfiguration;
31import org.apache.commons.configuration.XMLConfiguration;
32import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
Andrea Campanella77e9b332018-11-29 13:33:19 -080033import org.onlab.packet.ChassisId;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070034import org.onosproject.net.DefaultAnnotations;
Andrea Campanella77e9b332018-11-29 13:33:19 -080035import org.onosproject.net.Device;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070036import org.onosproject.net.DeviceId;
37import org.onosproject.net.Port.Type;
38import org.onosproject.net.PortNumber;
Andrea Campanella77e9b332018-11-29 13:33:19 -080039import org.onosproject.net.device.DefaultDeviceDescription;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070040import org.onosproject.net.device.DefaultPortDescription;
41import org.onosproject.net.device.DefaultPortDescription.Builder;
42import org.onosproject.net.device.DeviceDescription;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070043import org.onosproject.net.device.PortDescription;
44import org.onosproject.net.driver.AbstractHandlerBehaviour;
45import org.onosproject.netconf.NetconfController;
46import org.onosproject.netconf.NetconfDevice;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070047import org.onosproject.netconf.NetconfSession;
48import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
49import org.slf4j.Logger;
50
51import com.google.common.annotations.VisibleForTesting;
52import com.google.common.collect.ImmutableList;
53import com.google.common.io.CharSource;
54
55/**
56 * OpenConfig based device and port discovery.
57 */
58public class OpenConfigDeviceDiscovery
59 extends AbstractHandlerBehaviour
Andrea Campanella77e9b332018-11-29 13:33:19 -080060 implements OdtnDeviceDescriptionDiscovery {
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070061
62 private static final Logger log = getLogger(OpenConfigDeviceDiscovery.class);
63
64 @Override
65 public DeviceDescription discoverDeviceDetails() {
Andrea Campanella77e9b332018-11-29 13:33:19 -080066 return new DefaultDeviceDescription(handler().data().deviceId().uri(),
Andrea Campanella1c24fb92018-12-20 16:43:59 +010067 Device.Type.TERMINAL_DEVICE, "unknown", "unknown",
Andrea Campanella77e9b332018-11-29 13:33:19 -080068 "unknown", "unknown", new ChassisId());
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -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
Yuta HIGUCHIf7333732018-05-14 16:07:37 -070090 String reply = ns.asyncGet()
91 .join().toString();
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070092
Yuta HIGUCHIf7333732018-05-14 16:07:37 -070093 // workaround until asyncGet().join() start failing exceptionally
94 String data = null;
95 if (reply.startsWith("<data")) {
96 data = reply;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070097 }
98
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070099 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("components/component");
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
153 Map<String, String> props = new HashMap<>();
154
155 String name = component.getString("name");
156 String type = component.getString("state/type");
Yuta HIGUCHI318809a2018-05-25 11:27:35 -0700157 checkNotNull(name, "name not found");
158 checkNotNull(type, "state/type not found");
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700159 props.put(OdtnDeviceDescriptionDiscovery.OC_NAME, name);
160 props.put(OdtnDeviceDescriptionDiscovery.OC_TYPE, type);
161
162 component.configurationsAt("properties/property").forEach(prop -> {
163 String pName = prop.getString("name");
164 String pValue = prop.getString("config/value");
165 props.put(pName, pValue);
166 });
167
Yuta HIGUCHI44e3a612018-05-11 15:03:39 -0700168 if (!props.containsKey(ONOS_PORT_INDEX)) {
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700169 log.info("DEBUG: Component {} does not include onos-index, skipping", name);
170 // ODTN: port must have onos-index property
171 return null;
172 }
173
174 Builder builder = DefaultPortDescription.builder();
Yuta HIGUCHI44e3a612018-05-11 15:03:39 -0700175 builder.withPortNumber(PortNumber.portNumber(Long.parseLong(props.get(ONOS_PORT_INDEX)), name));
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700176
177 switch (type) {
178 case "oc-platform-types:PORT":
hirokiec18d3a2018-05-16 15:27:37 -0700179 case "oc-opt-types:OPTICAL_CHANNEL":
180 // TODO assign appropriate port type & annotations at some point
181 // for now we just need a Port with annotations
182 builder.type(Type.OCH);
183
Yuta HIGUCHIe4702af2018-05-18 17:17:34 -0700184 props.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value());
185
hirokiec18d3a2018-05-16 15:27:37 -0700186 // Just a heuristics to deal with simple transponder
187 // if the device declare odtn-connection-id, just use them
188 // if not assign same value to relevant ports types
189 props.putIfAbsent(CONNECTION_ID, "the-only-one");
190 break;
Yuta HIGUCHIe4702af2018-05-18 17:17:34 -0700191
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700192 case "oc-platform-types:TRANSCEIVER":
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700193 // TODO assign appropriate port type & annotations at some point
194 // for now we just need a Port with annotations
195 builder.type(Type.PACKET);
Yuta HIGUCHI4b11fab2018-05-15 13:03:29 -0700196
Yuta HIGUCHIe4702af2018-05-18 17:17:34 -0700197 props.putIfAbsent(PORT_TYPE, OdtnPortType.CLIENT.value());
198
Yuta HIGUCHI4b11fab2018-05-15 13:03:29 -0700199 // Just a heuristics to deal with simple transponder
200 // if the device declare odtn-connection-id, just use them
201 // if not assign same value to relevant ports types
202 props.putIfAbsent(CONNECTION_ID, "the-only-one");
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700203 break;
204
205 default:
206 log.info("DEBUG: Unknown component type {}", type);
207 return null;
208 }
209
210 builder.annotations(DefaultAnnotations.builder().putAll(props).build());
211
212 return builder.build();
213
214 }
215
216}