hiroki | eec31ef | 2018-05-21 07:34:25 -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 com.google.common.annotations.VisibleForTesting; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.io.CharSource; |
| 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.regex.Matcher; |
| 28 | import java.util.regex.Pattern; |
| 29 | import java.util.stream.Collectors; |
| 30 | import org.apache.commons.configuration.ConfigurationException; |
| 31 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 32 | import org.apache.commons.configuration.XMLConfiguration; |
| 33 | import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine; |
Andrea Campanella | 77e9b33 | 2018-11-29 13:33:19 -0800 | [diff] [blame^] | 34 | import org.onlab.packet.ChassisId; |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 35 | import org.onosproject.net.DefaultAnnotations; |
Andrea Campanella | 77e9b33 | 2018-11-29 13:33:19 -0800 | [diff] [blame^] | 36 | import org.onosproject.net.Device; |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Port.Type; |
| 39 | import org.onosproject.net.PortNumber; |
Andrea Campanella | 77e9b33 | 2018-11-29 13:33:19 -0800 | [diff] [blame^] | 40 | import org.onosproject.net.device.DefaultDeviceDescription; |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 41 | import org.onosproject.net.device.DefaultPortDescription; |
| 42 | import org.onosproject.net.device.DefaultPortDescription.Builder; |
| 43 | import org.onosproject.net.device.DeviceDescription; |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 44 | import org.onosproject.net.device.PortDescription; |
| 45 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 46 | import org.onosproject.netconf.NetconfController; |
| 47 | import org.onosproject.netconf.NetconfDevice; |
| 48 | import org.onosproject.netconf.NetconfSession; |
| 49 | import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery; |
| 50 | import org.slf4j.Logger; |
| 51 | |
| 52 | import static com.google.common.base.Preconditions.checkNotNull; |
| 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
| 56 | * OpenConfig based device and port discovery. |
| 57 | */ |
| 58 | public class InfineraOpenConfigDeviceDiscovery |
| 59 | extends AbstractHandlerBehaviour |
Andrea Campanella | 77e9b33 | 2018-11-29 13:33:19 -0800 | [diff] [blame^] | 60 | implements OdtnDeviceDescriptionDiscovery { |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 61 | |
| 62 | private static final Logger log = getLogger(InfineraOpenConfigDeviceDiscovery.class); |
| 63 | |
| 64 | @Override |
| 65 | public DeviceDescription discoverDeviceDetails() { |
Andrea Campanella | 77e9b33 | 2018-11-29 13:33:19 -0800 | [diff] [blame^] | 66 | return new DefaultDeviceDescription(handler().data().deviceId().uri(), |
| 67 | Device.Type.OTN, "Infinera", "XT-3300", |
| 68 | "unknown", "unknown", new ChassisId()); |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 69 | } |
| 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 | |
| 90 | String reply = ns.asyncGet() |
| 91 | .join().toString(); |
| 92 | |
| 93 | // workaround until asyncGet().join() start failing exceptionally |
| 94 | String data = null; |
| 95 | if (reply.startsWith("<data")) { |
| 96 | data = reply; |
| 97 | } |
| 98 | |
| 99 | 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("interfaces/interface"); |
| 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 | |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 153 | String name = component.getString("name"); |
| 154 | checkNotNull(name); |
| 155 | if (!name.contains("GIGECLIENTCTP")) { |
| 156 | return null; |
| 157 | } |
| 158 | |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 159 | Builder builder = DefaultPortDescription.builder(); |
| 160 | |
| 161 | Map<String, String> props = new HashMap<>(); |
| 162 | props.put(OdtnDeviceDescriptionDiscovery.OC_NAME, name); |
| 163 | props.put(OdtnDeviceDescriptionDiscovery.OC_TYPE, name); |
| 164 | |
| 165 | Pattern clientPattern = Pattern.compile("GIGECLIENTCTP.1-A-2-T(\\d+)"); |
| 166 | Pattern linePattern = Pattern.compile("GIGECLIENTCTP.1-L(\\d+)-1-1"); |
| 167 | Matcher clientMatch = clientPattern.matcher(name); |
| 168 | Matcher lineMatch = linePattern.matcher(name); |
| 169 | |
| 170 | if (clientMatch.find()) { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 171 | String num = clientMatch.group(1); |
| 172 | Integer connection = (Integer.parseInt(num) + 1) / 2; |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 173 | props.putIfAbsent(PORT_TYPE, OdtnPortType.CLIENT.value()); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 174 | props.putIfAbsent(CONNECTION_ID, "connection:" + connection.toString()); |
| 175 | builder.withPortNumber(PortNumber.portNumber(Long.parseLong(num), name)); |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 176 | builder.type(Type.PACKET); |
| 177 | } else if (lineMatch.find()) { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 178 | String num = lineMatch.group(1); |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 179 | props.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value()); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 180 | props.putIfAbsent(CONNECTION_ID, "connection:" + num); |
| 181 | builder.withPortNumber(PortNumber.portNumber(100 + Long.parseLong(num), name)); |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 182 | builder.type(Type.OCH); |
| 183 | } else { |
| 184 | return null; |
| 185 | } |
| 186 | |
| 187 | builder.annotations(DefaultAnnotations.builder().putAll(props).build()); |
| 188 | return builder.build(); |
| 189 | |
| 190 | } |
| 191 | |
| 192 | } |