blob: ebc1c65a2d1c9e2a8860696d9839d8e36de1cb71 [file] [log] [blame]
hiroki9e1484d2018-12-07 09:36:49 -08001/*
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 *
16 * This work was partially supported by EC H2020 project METRO-HAUL (761727).
17 */
18
19package org.onosproject.drivers.odtn;
20
Andrea Campanella3a361452019-08-02 10:17:53 +020021import com.google.common.annotations.VisibleForTesting;
hiroki9e1484d2018-12-07 09:36:49 -080022import com.google.common.collect.ImmutableList;
23import org.apache.commons.configuration.HierarchicalConfiguration;
24import org.apache.commons.configuration.XMLConfiguration;
25import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
26import org.onlab.packet.ChassisId;
27import org.onosproject.drivers.utilities.XmlConfigParser;
Andrea Campanella3a361452019-08-02 10:17:53 +020028import org.onosproject.net.ChannelSpacing;
hiroki9e1484d2018-12-07 09:36:49 -080029import org.onosproject.net.DefaultAnnotations;
30import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
Andrea Campanella3a361452019-08-02 10:17:53 +020032import org.onosproject.net.OchSignal;
33import org.onosproject.net.OduSignalType;
hiroki9e1484d2018-12-07 09:36:49 -080034import org.onosproject.net.PortNumber;
35import org.onosproject.net.device.DefaultDeviceDescription;
hiroki9e1484d2018-12-07 09:36:49 -080036import org.onosproject.net.device.DeviceDescription;
37import org.onosproject.net.device.DeviceDescriptionDiscovery;
38import org.onosproject.net.device.PortDescription;
39import org.onosproject.net.driver.AbstractHandlerBehaviour;
Andrea Campanella3a361452019-08-02 10:17:53 +020040import org.onosproject.net.optical.device.OchPortHelper;
hiroki9e1484d2018-12-07 09:36:49 -080041import org.onosproject.netconf.NetconfController;
42import org.onosproject.netconf.NetconfDevice;
43import org.onosproject.netconf.NetconfSession;
44import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
45import org.slf4j.Logger;
46
47import java.util.HashMap;
48import java.util.List;
49import java.util.Map;
50import java.util.Objects;
51import java.util.concurrent.CompletableFuture;
hiroki9e1484d2018-12-07 09:36:49 -080052import java.util.stream.Collectors;
53
Andrea Campanella3a361452019-08-02 10:17:53 +020054import static com.google.common.base.Preconditions.checkNotNull;
hiroki9e1484d2018-12-07 09:36:49 -080055import static org.slf4j.LoggerFactory.getLogger;
56
57
58/**
59 * Driver Implementation of the DeviceDescrption discovery for OpenConfig
60 * terminal devices.
hiroki9e1484d2018-12-07 09:36:49 -080061 */
62public class CassiniTerminalDeviceDiscovery
Andrea Campanella3a361452019-08-02 10:17:53 +020063 extends AbstractHandlerBehaviour
64 implements OdtnDeviceDescriptionDiscovery, DeviceDescriptionDiscovery {
hiroki9e1484d2018-12-07 09:36:49 -080065
66 private static final String RPC_TAG_NETCONF_BASE =
Andrea Campanella3a361452019-08-02 10:17:53 +020067 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">";
hiroki9e1484d2018-12-07 09:36:49 -080068
69 private static final String RPC_CLOSE_TAG = "</rpc>";
70
hiroki9e1484d2018-12-07 09:36:49 -080071 private static final String OC_TRANSPORT_TYPES_OPTICAL_CHANNEL =
Andrea Campanella3a361452019-08-02 10:17:53 +020072 "OPTICAL_CHANNEL";
hiroki9e1484d2018-12-07 09:36:49 -080073
74 private static final Logger log = getLogger(CassiniTerminalDeviceDiscovery.class);
75
76
77 /**
78 * Returns the NetconfSession with the device for which the method was called.
79 *
80 * @param deviceId device indetifier
hiroki9e1484d2018-12-07 09:36:49 -080081 * @return The netconf session or null
82 */
83 private NetconfSession getNetconfSession(DeviceId deviceId) {
84 NetconfController controller = handler().get(NetconfController.class);
85 NetconfDevice ncdev = controller.getDevicesMap().get(deviceId);
86 if (ncdev == null) {
87 log.trace("No netconf device, returning null session");
88 return null;
89 }
90 return ncdev.getSession();
91 }
92
93
94 /**
95 * Get the deviceId for which the methods apply.
96 *
97 * @return The deviceId as contained in the handler data
98 */
99 private DeviceId did() {
100 return handler().data().deviceId();
101 }
102
103
104 /**
105 * Construct a String with a Netconf filtered get RPC Message.
106 *
107 * @param filter A valid XML tree with the filter to apply in the get
108 * @return a String containing the RPC XML Document
109 */
110 private String filteredGetBuilder(String filter) {
111 StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
112 rpc.append("<get>");
113 rpc.append("<filter type='subtree'>");
114 rpc.append(filter);
115 rpc.append("</filter>");
116 rpc.append("</get>");
117 rpc.append(RPC_CLOSE_TAG);
118 return rpc.toString();
119 }
120
121
hiroki9e1484d2018-12-07 09:36:49 -0800122 @Override
123 public DeviceDescription discoverDeviceDetails() {
124 return new DefaultDeviceDescription(handler().data().deviceId().uri(),
Andrea Campanella3a361452019-08-02 10:17:53 +0200125 Device.Type.TERMINAL_DEVICE,
126 "EDGECORE",
127 "Cassini",
128 "OcNOS",
129 "",
130 new ChassisId("1"));
hiroki9e1484d2018-12-07 09:36:49 -0800131 }
132
133
hiroki9e1484d2018-12-07 09:36:49 -0800134 /**
135 * Returns a list of PortDescriptions for the device.
136 *
137 * @return a list of descriptions.
Andrea Campanella3a361452019-08-02 10:17:53 +0200138 * <p>
hiroki9e1484d2018-12-07 09:36:49 -0800139 * The RPC reply follows the following pattern:
140 * //CHECKSTYLE:OFF
141 * <pre>{@code
142 * <?xml version="1.0" encoding="UTF-8"?>
143 * <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="7">
144 * <data>
145 * <components xmlns="http://openconfig.net/yang/platform">
146 * <component>....
147 * </component>
148 * <component>....
149 * </component>
150 * </components>
151 * </data>
152 * </rpc-reply>
153 * }</pre>
154 * //CHECKSTYLE:ON
155 */
156 @Override
157 public List<PortDescription> discoverPortDetails() {
158 try {
159 NetconfSession session = getNetconfSession(did());
160 if (session == null) {
161 log.error("discoverPortDetails called with null session for {}", did());
162 return ImmutableList.of();
163 }
Andrea Campanella3a361452019-08-02 10:17:53 +0200164 CompletableFuture<CharSequence> fut1 = session.asyncGet();
165 String rpcReplyTest = fut1.get().toString();
hiroki9e1484d2018-12-07 09:36:49 -0800166
Andrea Campanella3a361452019-08-02 10:17:53 +0200167 XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReplyTest);
hiroki9e1484d2018-12-07 09:36:49 -0800168 xconf.setExpressionEngine(new XPathExpressionEngine());
169
Andrea Campanella3a361452019-08-02 10:17:53 +0200170 HierarchicalConfiguration logicalChannels = xconf.configurationAt("components");
171 return discoverPorts(logicalChannels);
hiroki9e1484d2018-12-07 09:36:49 -0800172 } catch (Exception e) {
173 log.error("Exception discoverPortDetails() {}", did(), e);
174 return ImmutableList.of();
175 }
176 }
177
hiroki9e1484d2018-12-07 09:36:49 -0800178 /**
Andrea Campanella3a361452019-08-02 10:17:53 +0200179 * Parses port information from OpenConfig XML configuration.
hiroki9e1484d2018-12-07 09:36:49 -0800180 *
Andrea Campanella3a361452019-08-02 10:17:53 +0200181 * @param cfg tree where the root node is {@literal <data>}
hiroki9e1484d2018-12-07 09:36:49 -0800182 * @return List of ports
hiroki9e1484d2018-12-07 09:36:49 -0800183 */
Andrea Campanella3a361452019-08-02 10:17:53 +0200184 @VisibleForTesting
185 private List<PortDescription> discoverPorts(HierarchicalConfiguration cfg) {
186 // If we want to use XPath
187 cfg.setExpressionEngine(new XPathExpressionEngine());
188 // converting components into PortDescription.
189 List<HierarchicalConfiguration> components = cfg.configurationsAt("component");
190 return components.stream()
191 .map(this::toPortDescriptionInternal)
192 .filter(Objects::nonNull)
193 .collect(Collectors.toList());
hiroki9e1484d2018-12-07 09:36:49 -0800194 }
195
Andrea Campanella3a361452019-08-02 10:17:53 +0200196 /**
197 * Converts Component subtree to PortDescription.
198 *
199 * @param component subtree to parse
200 * @return PortDescription or null if component is not an ONOS Port
201 */
202 private PortDescription toPortDescriptionInternal(HierarchicalConfiguration component) {
203 Map<String, String> annotations = new HashMap<>();
204 /*
205 <components xmlns="http://openconfig.net/yang/platform">
206 <component>
207 <name>oc1/0</name>
208 <config>
209 <name>oc1/0</name>
210 </config>
211 <state>
212 <name>oc1/0</name>
213 <type>OPTICAL_CHANNEL</type>
214 <id/>
215 <description/>
216 <mfg-name/>
217 <hardware-version/>
218 <firmware-version/>
219 <software-version/>
220 <serial-no/>
221 <part-no/>
222 <removable>true</removable>
223 <empty>false</empty>
224 <parent/>
225 <temperature>
226 <instant>0.0</instant>
227 <avg>0.0</avg>
228 <min>0.0</min>
229 <max>0.0</max>
230 <interval>0</interval>
231 <min-time>0</min-time>
232 <max-time>0</max-time>
233 <alarm-status>true</alarm-status>
234 <alarm-threshold>0</alarm-threshold>
235 </temperature>
236 <memory>
237 <available>0</available>
238 <utilized>0</utilized>
239 </memory>
240 <allocated-power>0</allocated-power>
241 <used-power>0</used-power>
242 </state>
243 <optical-channel xmlns="http://openconfig.net/yang/terminal-device">
244 <config>
245 <line-port>port-10101</line-port>
246 </config>
247 <state>
248 <output-power/>
249 <input-power/>
250 </state>
251 </optical-channel>
252 </component>
253 */
254 String name = component.getString("name");
255 String type = component.getString("state/type");
256 checkNotNull(name, "name not found");
257 checkNotNull(type, "state/type not found");
258 annotations.put(OdtnDeviceDescriptionDiscovery.OC_NAME, name);
259 annotations.put(OdtnDeviceDescriptionDiscovery.OC_TYPE, type);
hiroki9e1484d2018-12-07 09:36:49 -0800260
Andrea Campanella3a361452019-08-02 10:17:53 +0200261 //TODO this currently support only line-side ports through parsing of optical channels.
262 if (type.equals(OC_TRANSPORT_TYPES_OPTICAL_CHANNEL)) {
263 String portName = component.getString("optical-channel/config/line-port");
264 String portIndex = portName.split("-")[1];
265 annotations.putIfAbsent("name", portName);
266 annotations.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value());
267 annotations.putIfAbsent(ONOS_PORT_INDEX, portIndex);
268 annotations.putIfAbsent(CONNECTION_ID, "connection-" + portIndex);
hiroki9e1484d2018-12-07 09:36:49 -0800269
Andrea Campanella3a361452019-08-02 10:17:53 +0200270 OchSignal signalId = OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1);
271 return OchPortHelper.ochPortDescription(
272 PortNumber.portNumber(Long.parseLong(portIndex)),
273 true,
274 OduSignalType.ODU4, // TODO Client signal to be discovered
275 true,
276 signalId,
277 DefaultAnnotations.builder().putAll(annotations).build());
hiroki9e1484d2018-12-07 09:36:49 -0800278
Andrea Campanella3a361452019-08-02 10:17:53 +0200279 } else {
280 log.debug("Unknown port component type {}", type);
281 return null;
282 }
283 }
hiroki9e1484d2018-12-07 09:36:49 -0800284}