Yuta HIGUCHI | 8c6e194 | 2018-04-05 13:40:51 -0700 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.drivers.odtn; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Objects; |
| 26 | import java.util.Optional; |
| 27 | import java.util.stream.Collectors; |
| 28 | |
| 29 | import org.apache.commons.configuration.ConfigurationException; |
| 30 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 31 | import org.apache.commons.configuration.XMLConfiguration; |
| 32 | import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine; |
| 33 | import org.onosproject.net.DefaultAnnotations; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Port.Type; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.device.DefaultPortDescription; |
| 38 | import org.onosproject.net.device.DefaultPortDescription.Builder; |
| 39 | import org.onosproject.net.device.DeviceDescription; |
| 40 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 41 | import org.onosproject.net.device.PortDescription; |
| 42 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 43 | import org.onosproject.netconf.NetconfController; |
| 44 | import org.onosproject.netconf.NetconfDevice; |
Yuta HIGUCHI | 8c6e194 | 2018-04-05 13:40:51 -0700 | [diff] [blame] | 45 | import org.onosproject.netconf.NetconfSession; |
| 46 | import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import com.google.common.annotations.VisibleForTesting; |
| 50 | import com.google.common.collect.ImmutableList; |
| 51 | import com.google.common.io.CharSource; |
| 52 | |
| 53 | /** |
| 54 | * OpenConfig based device and port discovery. |
| 55 | */ |
| 56 | public 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 HIGUCHI | f733373 | 2018-05-14 16:07:37 -0700 | [diff] [blame] | 88 | String reply = ns.asyncGet() |
| 89 | .join().toString(); |
Yuta HIGUCHI | 8c6e194 | 2018-04-05 13:40:51 -0700 | [diff] [blame] | 90 | |
Yuta HIGUCHI | f733373 | 2018-05-14 16:07:37 -0700 | [diff] [blame] | 91 | // workaround until asyncGet().join() start failing exceptionally |
| 92 | String data = null; |
| 93 | if (reply.startsWith("<data")) { |
| 94 | data = reply; |
Yuta HIGUCHI | 8c6e194 | 2018-04-05 13:40:51 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Yuta HIGUCHI | 8c6e194 | 2018-04-05 13:40:51 -0700 | [diff] [blame] | 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("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"); |
| 155 | checkNotNull(name); |
| 156 | checkNotNull(type); |
| 157 | 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 HIGUCHI | 44e3a61 | 2018-05-11 15:03:39 -0700 | [diff] [blame] | 166 | if (!props.containsKey(ONOS_PORT_INDEX)) { |
Yuta HIGUCHI | 8c6e194 | 2018-04-05 13:40:51 -0700 | [diff] [blame] | 167 | 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 HIGUCHI | 44e3a61 | 2018-05-11 15:03:39 -0700 | [diff] [blame] | 173 | builder.withPortNumber(PortNumber.portNumber(Long.parseLong(props.get(ONOS_PORT_INDEX)), name)); |
Yuta HIGUCHI | 8c6e194 | 2018-04-05 13:40:51 -0700 | [diff] [blame] | 174 | |
| 175 | switch (type) { |
| 176 | case "oc-platform-types:PORT": |
| 177 | case "oc-platform-types:TRANSCEIVER": |
| 178 | case "oc-opt-types:OPTICAL_CHANNEL": |
| 179 | // TODO assign appropriate port type & annotations at some point |
| 180 | // for now we just need a Port with annotations |
| 181 | builder.type(Type.PACKET); |
| 182 | break; |
| 183 | |
| 184 | default: |
| 185 | log.info("DEBUG: Unknown component type {}", type); |
| 186 | return null; |
| 187 | } |
| 188 | |
| 189 | builder.annotations(DefaultAnnotations.builder().putAll(props).build()); |
| 190 | |
| 191 | return builder.build(); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | } |