blob: 1d5563a2bc33ceb16faf6ffead49d44d4191a39a [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;
33import org.onosproject.net.DefaultAnnotations;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Port.Type;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.device.DefaultPortDescription;
38import org.onosproject.net.device.DefaultPortDescription.Builder;
39import org.onosproject.net.device.DeviceDescription;
40import org.onosproject.net.device.DeviceDescriptionDiscovery;
41import org.onosproject.net.device.PortDescription;
42import org.onosproject.net.driver.AbstractHandlerBehaviour;
43import org.onosproject.netconf.NetconfController;
44import org.onosproject.netconf.NetconfDevice;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070045import org.onosproject.netconf.NetconfSession;
46import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
47import org.slf4j.Logger;
48
49import com.google.common.annotations.VisibleForTesting;
50import com.google.common.collect.ImmutableList;
51import com.google.common.io.CharSource;
52
53/**
54 * OpenConfig based device and port discovery.
55 */
56public class OpenConfigDeviceDiscovery
57 extends AbstractHandlerBehaviour
58 implements OdtnDeviceDescriptionDiscovery, DeviceDescriptionDiscovery {
59
60 private static final Logger log = getLogger(OpenConfigDeviceDiscovery.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
Yuta HIGUCHIf7333732018-05-14 16:07:37 -070088 String reply = ns.asyncGet()
89 .join().toString();
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070090
Yuta HIGUCHIf7333732018-05-14 16:07:37 -070091 // workaround until asyncGet().join() start failing exceptionally
92 String data = null;
93 if (reply.startsWith("<data")) {
94 data = reply;
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070095 }
96
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -070097 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("components/component");
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 Map<String, String> props = new HashMap<>();
152
153 String name = component.getString("name");
154 String type = component.getString("state/type");
Yuta HIGUCHI318809a2018-05-25 11:27:35 -0700155 checkNotNull(name, "name not found");
156 checkNotNull(type, "state/type not found");
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700157 props.put(OdtnDeviceDescriptionDiscovery.OC_NAME, name);
158 props.put(OdtnDeviceDescriptionDiscovery.OC_TYPE, type);
159
160 component.configurationsAt("properties/property").forEach(prop -> {
161 String pName = prop.getString("name");
162 String pValue = prop.getString("config/value");
163 props.put(pName, pValue);
164 });
165
Yuta HIGUCHI44e3a612018-05-11 15:03:39 -0700166 if (!props.containsKey(ONOS_PORT_INDEX)) {
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700167 log.info("DEBUG: Component {} does not include onos-index, skipping", name);
168 // ODTN: port must have onos-index property
169 return null;
170 }
171
172 Builder builder = DefaultPortDescription.builder();
Yuta HIGUCHI44e3a612018-05-11 15:03:39 -0700173 builder.withPortNumber(PortNumber.portNumber(Long.parseLong(props.get(ONOS_PORT_INDEX)), name));
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700174
175 switch (type) {
176 case "oc-platform-types:PORT":
hirokiec18d3a2018-05-16 15:27:37 -0700177 case "oc-opt-types:OPTICAL_CHANNEL":
178 // TODO assign appropriate port type & annotations at some point
179 // for now we just need a Port with annotations
180 builder.type(Type.OCH);
181
Yuta HIGUCHIe4702af2018-05-18 17:17:34 -0700182 props.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value());
183
hirokiec18d3a2018-05-16 15:27:37 -0700184 // Just a heuristics to deal with simple transponder
185 // if the device declare odtn-connection-id, just use them
186 // if not assign same value to relevant ports types
187 props.putIfAbsent(CONNECTION_ID, "the-only-one");
188 break;
Yuta HIGUCHIe4702af2018-05-18 17:17:34 -0700189
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700190 case "oc-platform-types:TRANSCEIVER":
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700191 // TODO assign appropriate port type & annotations at some point
192 // for now we just need a Port with annotations
193 builder.type(Type.PACKET);
Yuta HIGUCHI4b11fab2018-05-15 13:03:29 -0700194
Yuta HIGUCHIe4702af2018-05-18 17:17:34 -0700195 props.putIfAbsent(PORT_TYPE, OdtnPortType.CLIENT.value());
196
Yuta HIGUCHI4b11fab2018-05-15 13:03:29 -0700197 // Just a heuristics to deal with simple transponder
198 // if the device declare odtn-connection-id, just use them
199 // if not assign same value to relevant ports types
200 props.putIfAbsent(CONNECTION_ID, "the-only-one");
Yuta HIGUCHI8c6e1942018-04-05 13:40:51 -0700201 break;
202
203 default:
204 log.info("DEBUG: Unknown component type {}", type);
205 return null;
206 }
207
208 builder.annotations(DefaultAnnotations.builder().putAll(props).build());
209
210 return builder.build();
211
212 }
213
214}