blob: 916a0293a63f57b3eeaac48a353330a965e9a840 [file] [log] [blame]
Laszlo Papp8b3a5f62017-10-05 13:32:00 +01001/*
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
17package org.onosproject.drivers.polatis.netconf;
18
19import org.apache.commons.configuration.HierarchicalConfiguration;
20import com.google.common.collect.ImmutableList;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010021import org.onosproject.net.DefaultAnnotations;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010024import org.onosproject.net.device.DefaultDeviceDescription;
25import org.onosproject.net.device.DeviceDescription;
26import org.onosproject.net.device.DeviceDescriptionDiscovery;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.device.PortDescription;
29import org.onosproject.net.driver.AbstractHandlerBehaviour;
30
Laszlo Papp4e3780e2017-10-14 08:25:55 +010031import org.onlab.packet.ChassisId;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010032
33import org.slf4j.Logger;
34import static org.slf4j.LoggerFactory.getLogger;
35
36import java.util.List;
Michael Enrico584eebd2021-07-22 08:04:13 +000037import java.util.regex.Matcher;
38import java.util.regex.Pattern;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010039
40import static com.google.common.base.Preconditions.checkNotNull;
41import static org.onosproject.net.Device.Type.FIBER_SWITCH;
42
Michael Enrico584eebd2021-07-22 08:04:13 +000043import static org.onosproject.drivers.polatis.netconf.PolatisUtility.getPortsFilter;
44import static org.onosproject.drivers.polatis.netconf.PolatisUtility.getProdInfoFilter;
45import static org.onosproject.drivers.polatis.netconf.PolatisUtility.parsePorts;
46import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_MANUFACTURER;
47import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_HWVERSION;
48import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_SWVERSION;
49import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_SERIALNUMBER;
50import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_INPUTPORTS;
51import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_OUTPUTPORTS;
52
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010053import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configAt;
Laszlo Pappbdb64082018-09-11 12:21:29 +010054import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.netconfGet;
55import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.subscribe;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010056import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_PRODINF;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010057
58/**
59 * Representation of device information and ports via NETCONF for all Polatis
60 * optical circuit switches.
61 */
62public class PolatisDeviceDescription extends AbstractHandlerBehaviour
63 implements DeviceDescriptionDiscovery {
64
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010065 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 Enrico584eebd2021-07-22 08:04:13 +000075 log.debug("Discovering Polatis device detais...");
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010076 return parseProductInformation();
77 }
78
79 private DeviceDescription parseProductInformation() {
80 DeviceService devsvc = checkNotNull(handler().get(DeviceService.class));
Michael Enrico584eebd2021-07-22 08:04:13 +000081 DeviceId devID = handler().data().deviceId();
82 String reply = netconfGet(handler(), getProdInfoFilter());
Laszlo Pappbdb64082018-09-11 12:21:29 +010083 subscribe(handler());
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010084 HierarchicalConfiguration cfg = configAt(reply, KEY_DATA_PRODINF);
Michael Enrico584eebd2021-07-22 08:04:13 +000085 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 Papp8b3a5f62017-10-05 13:32:00 +0100116 cfg.getString(KEY_MANUFACTURER), cfg.getString(KEY_HWVERSION),
117 cfg.getString(KEY_SWVERSION), cfg.getString(KEY_SERIALNUMBER),
Michael Enrico584eebd2021-07-22 08:04:13 +0000118 new ChassisId(cfg.getString(KEY_SERIALNUMBER)), true, annotations);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100119 }
120
121 /**
122 * Discovers port details, for polatis device.
123 *
124 * @return port list
125 */
126 @Override
127 public List<PortDescription> discoverPortDetails() {
Michael Enrico584eebd2021-07-22 08:04:13 +0000128 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 Papp8b3a5f62017-10-05 13:32:00 +0100134 String reply = netconfGet(handler(), getPortsFilter());
Michael Enrico584eebd2021-07-22 08:04:13 +0000135 List<PortDescription> descriptions = parsePorts(reply, numInputPorts, numOutputPorts);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100136 return ImmutableList.copyOf(descriptions);
137 }
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100138}