Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.drivers.polatis.netconf; |
| 18 | |
| 19 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 20 | import com.google.common.collect.ImmutableList; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.DeviceId; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 24 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 25 | import org.onosproject.net.device.DeviceDescription; |
| 26 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.device.PortDescription; |
| 29 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 30 | |
Laszlo Papp | 4e3780e | 2017-10-14 08:25:55 +0100 | [diff] [blame] | 31 | import org.onlab.packet.ChassisId; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 32 | |
| 33 | import org.slf4j.Logger; |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | import java.util.List; |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 37 | import java.util.regex.Matcher; |
| 38 | import java.util.regex.Pattern; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.onosproject.net.Device.Type.FIBER_SWITCH; |
| 42 | |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 43 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.getPortsFilter; |
| 44 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.getProdInfoFilter; |
| 45 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.parsePorts; |
| 46 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_MANUFACTURER; |
| 47 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_HWVERSION; |
| 48 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_SWVERSION; |
| 49 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_SERIALNUMBER; |
| 50 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_INPUTPORTS; |
| 51 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_OUTPUTPORTS; |
| 52 | |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 53 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configAt; |
Laszlo Papp | bdb6408 | 2018-09-11 12:21:29 +0100 | [diff] [blame] | 54 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.netconfGet; |
| 55 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.subscribe; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 56 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_PRODINF; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Representation of device information and ports via NETCONF for all Polatis |
| 60 | * optical circuit switches. |
| 61 | */ |
| 62 | public class PolatisDeviceDescription extends AbstractHandlerBehaviour |
| 63 | implements DeviceDescriptionDiscovery { |
| 64 | |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 65 | private final Logger log = getLogger(getClass()); |
| 66 | |
| 67 | /** |
| 68 | * Discovers device details, for polatis device by getting the system |
| 69 | * information. |
| 70 | * |
| 71 | * @return device description |
| 72 | */ |
| 73 | @Override |
| 74 | public DeviceDescription discoverDeviceDetails() { |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 75 | log.debug("Discovering Polatis device detais..."); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 76 | return parseProductInformation(); |
| 77 | } |
| 78 | |
| 79 | private DeviceDescription parseProductInformation() { |
| 80 | DeviceService devsvc = checkNotNull(handler().get(DeviceService.class)); |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 81 | DeviceId devID = handler().data().deviceId(); |
| 82 | String reply = netconfGet(handler(), getProdInfoFilter()); |
Laszlo Papp | bdb6408 | 2018-09-11 12:21:29 +0100 | [diff] [blame] | 83 | subscribe(handler()); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 84 | HierarchicalConfiguration cfg = configAt(reply, KEY_DATA_PRODINF); |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 85 | String hw = cfg.getString(KEY_HWVERSION); |
| 86 | String numInputPorts = "0"; |
| 87 | String numOutputPorts = "0"; |
| 88 | if (!hw.equals("")) { |
| 89 | Pattern patternSize = Pattern.compile("\\d+x[\\dC]+"); |
| 90 | Matcher matcher = patternSize.matcher(hw); |
| 91 | if (matcher.find()) { |
| 92 | String switchSize = matcher.group(); |
| 93 | log.debug("Got switch size: " + switchSize); |
| 94 | Pattern patternNumber = Pattern.compile("[\\dC]+"); |
| 95 | matcher = patternNumber.matcher(switchSize); |
| 96 | if (matcher.find()) { |
| 97 | numInputPorts = matcher.group(); |
| 98 | log.debug("numInputPorts=" + numInputPorts); |
| 99 | if (matcher.find()) { |
| 100 | if (!matcher.group().equals("CC")) { |
| 101 | numOutputPorts = matcher.group(); |
| 102 | } |
| 103 | } |
| 104 | log.debug("numOutputPorts=" + numOutputPorts); |
| 105 | } |
| 106 | } |
| 107 | } else { |
| 108 | log.warn("Unable to determine type of Polatis switch " + devID.toString()); |
| 109 | } |
| 110 | DefaultAnnotations annotations = DefaultAnnotations.builder() |
| 111 | .set(KEY_INPUTPORTS, numInputPorts) |
| 112 | .set(KEY_OUTPUTPORTS, numOutputPorts) |
| 113 | .build(); |
| 114 | |
| 115 | return new DefaultDeviceDescription(devID.uri(), FIBER_SWITCH, |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 116 | cfg.getString(KEY_MANUFACTURER), cfg.getString(KEY_HWVERSION), |
| 117 | cfg.getString(KEY_SWVERSION), cfg.getString(KEY_SERIALNUMBER), |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 118 | new ChassisId(cfg.getString(KEY_SERIALNUMBER)), true, annotations); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Discovers port details, for polatis device. |
| 123 | * |
| 124 | * @return port list |
| 125 | */ |
| 126 | @Override |
| 127 | public List<PortDescription> discoverPortDetails() { |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 128 | log.debug("Discovering ports on Polatis switch..."); |
| 129 | DeviceService deviceService = handler().get(DeviceService.class); |
| 130 | DeviceId deviceID = handler().data().deviceId(); |
| 131 | Device device = deviceService.getDevice(deviceID); |
| 132 | int numInputPorts = Integer.parseInt(device.annotations().value(KEY_INPUTPORTS)); |
| 133 | int numOutputPorts = Integer.parseInt(device.annotations().value(KEY_OUTPUTPORTS)); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 134 | String reply = netconfGet(handler(), getPortsFilter()); |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 135 | List<PortDescription> descriptions = parsePorts(reply, numInputPorts, numOutputPorts); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 136 | return ImmutableList.copyOf(descriptions); |
| 137 | } |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 138 | } |