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