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