blob: ad7764851e2343197bab32089c473ae9ac9868a8 [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;
21import com.google.common.collect.Lists;
22import org.onosproject.net.AnnotationKeys;
23import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.device.DefaultDeviceDescription;
28import org.onosproject.net.device.DeviceDescription;
29import org.onosproject.net.device.DeviceDescriptionDiscovery;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.device.PortDescription;
32import org.onosproject.net.driver.AbstractHandlerBehaviour;
33
34import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
35
Laszlo Papp4e3780e2017-10-14 08:25:55 +010036import org.onlab.packet.ChassisId;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010037import org.onlab.util.Frequency;
38import org.onlab.util.Spectrum;
39
40import org.slf4j.Logger;
41import static org.slf4j.LoggerFactory.getLogger;
42
43import java.util.List;
44
45import static com.google.common.base.Preconditions.checkNotNull;
46import static org.onosproject.net.Device.Type.FIBER_SWITCH;
47
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010048import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configAt;
49import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configsAt;
Laszlo Pappbdb64082018-09-11 12:21:29 +010050import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.netconfGet;
51import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.subscribe;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010052import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlOpen;
53import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlClose;
54import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlEmpty;
55import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PORT;
56import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PORTID;
57import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PORTCONFIG;
58import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PRODINF;
59import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PORTCONFIG_XMLNS;
60import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PRODINF_XMLNS;
61import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_PRODINF;
62import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_PORTCONFIG;
63
64/**
65 * Representation of device information and ports via NETCONF for all Polatis
66 * optical circuit switches.
67 */
68public class PolatisDeviceDescription extends AbstractHandlerBehaviour
69 implements DeviceDescriptionDiscovery {
70
71 public static final String DEFAULT_MANUFACTURER = "Polatis";
72 public static final String DEFAULT_DESCRIPTION_DATA = "Unknown";
73 public static final String KEY_MANUFACTURER = "manufacturer";
74 public static final String KEY_HWVERSION = "model-name";
75 public static final String KEY_SWVERSION = "software-version";
76 public static final String KEY_SERIALNUMBER = "serial-number";
77 public static final String KEY_PORTSTATUS = "status";
78 public static final String PORT_ENABLED = "ENABLED";
79 public static final String KEY_PORTLABEL = "label";
80 public static final int POLATIS_NUM_OF_WAVELENGTHS = 39;
81
82 private final Logger log = getLogger(getClass());
83
84 /**
85 * Discovers device details, for polatis device by getting the system
86 * information.
87 *
88 * @return device description
89 */
90 @Override
91 public DeviceDescription discoverDeviceDetails() {
92 return parseProductInformation();
93 }
94
95 private DeviceDescription parseProductInformation() {
96 DeviceService devsvc = checkNotNull(handler().get(DeviceService.class));
97 DeviceId devid = handler().data().deviceId();
98 Device dev = devsvc.getDevice(devid);
99 if (dev == null) {
Laszlo Papp4e3780e2017-10-14 08:25:55 +0100100 return new DefaultDeviceDescription(devid.uri(), FIBER_SWITCH,
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100101 DEFAULT_MANUFACTURER, DEFAULT_DESCRIPTION_DATA,
102 DEFAULT_DESCRIPTION_DATA, DEFAULT_DESCRIPTION_DATA,
Laszlo Papp4e3780e2017-10-14 08:25:55 +0100103 new ChassisId());
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100104 }
105 String reply = netconfGet(handler(), getProductInformationFilter());
Laszlo Pappbdb64082018-09-11 12:21:29 +0100106 subscribe(handler());
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100107 HierarchicalConfiguration cfg = configAt(reply, KEY_DATA_PRODINF);
108 return new DefaultDeviceDescription(dev.id().uri(), FIBER_SWITCH,
109 cfg.getString(KEY_MANUFACTURER), cfg.getString(KEY_HWVERSION),
110 cfg.getString(KEY_SWVERSION), cfg.getString(KEY_SERIALNUMBER),
111 dev.chassisId());
112 }
113
114 private String getProductInformationFilter() {
115 return new StringBuilder(xmlOpen(KEY_PRODINF_XMLNS))
116 .append(xmlClose(KEY_PRODINF))
117 .toString();
118 }
119
120 /**
121 * Discovers port details, for polatis device.
122 *
123 * @return port list
124 */
125 @Override
126 public List<PortDescription> discoverPortDetails() {
127 String reply = netconfGet(handler(), getPortsFilter());
128 List<PortDescription> descriptions = parsePorts(reply);
129 return ImmutableList.copyOf(descriptions);
130 }
131
132 private String getPortsFilter() {
133 return new StringBuilder(xmlOpen(KEY_PORTCONFIG_XMLNS))
134 .append(xmlOpen(KEY_PORT))
135 .append(xmlEmpty(KEY_PORTID))
136 .append(xmlEmpty(KEY_PORTSTATUS))
137 .append(xmlEmpty(KEY_PORTLABEL))
138 .append(xmlClose(KEY_PORT))
139 .append(xmlClose(KEY_PORTCONFIG))
140 .toString();
141 }
142
143 private List<PortDescription> parsePorts(String content) {
144 List<HierarchicalConfiguration> subtrees = configsAt(content, KEY_DATA_PORTCONFIG);
145 List<PortDescription> portDescriptions = Lists.newArrayList();
146 for (HierarchicalConfiguration portConfig : subtrees) {
147 portDescriptions.add(parsePort(portConfig));
148 }
149 return portDescriptions;
150 }
151
152 private PortDescription parsePort(HierarchicalConfiguration cfg) {
153 PortNumber portNumber = PortNumber.portNumber(cfg.getLong(KEY_PORTID));
154 DefaultAnnotations annotations = DefaultAnnotations.builder()
155 .set(AnnotationKeys.PORT_NAME, cfg.getString(KEY_PORTLABEL))
156 .build();
157 return omsPortDescription(portNumber,
158 cfg.getString(KEY_PORTSTATUS).equals(PORT_ENABLED),
159 Spectrum.O_BAND_MIN, Spectrum.L_BAND_MAX,
160 Frequency.ofGHz((Spectrum.O_BAND_MIN.asGHz() -
161 Spectrum.L_BAND_MAX.asGHz()) /
162 POLATIS_NUM_OF_WAVELENGTHS), annotations);
163 }
164}