Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.snmp; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 20 | import org.onosproject.net.DefaultAnnotations; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 23 | import org.onosproject.net.PortNumber; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 24 | import org.onosproject.net.SparseAnnotations; |
| 25 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 26 | import org.onosproject.net.device.DeviceDescription; |
| 27 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | import org.onosproject.net.device.PortDescription; |
| 30 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 31 | |
| 32 | import org.onlab.packet.ChassisId; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 33 | import org.onlab.util.Frequency; |
| 34 | import org.onlab.util.Spectrum; |
| 35 | |
| 36 | import org.snmp4j.smi.OID; |
| 37 | import org.snmp4j.smi.VariableBinding; |
| 38 | import org.snmp4j.util.TableEvent; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 39 | |
| 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.io.IOException; |
| 43 | import java.util.List; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 48 | import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 49 | import static org.onosproject.drivers.polatis.snmp.PolatisSnmpUtility.getOid; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 50 | import static org.onosproject.drivers.polatis.snmp.PolatisSnmpUtility.getTable; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Representation of device information and ports via SNMP for all Polatis |
| 54 | * optical circuit switches. |
| 55 | */ |
| 56 | public class PolatisDeviceDescription extends AbstractHandlerBehaviour |
| 57 | implements DeviceDescriptionDiscovery { |
| 58 | |
| 59 | private static final String DEFAULT_MANUFACTURER = "Polatis"; |
| 60 | private static final String DEFAULT_DESCRIPTION_DATA = "Unknown"; |
| 61 | |
| 62 | private static final String SOFTWARE_VERSION_OID = ".1.3.6.1.4.1.26592.2.1.2.2.3.0"; |
| 63 | private static final String PRODUCT_CODE_OID = ".1.3.6.1.4.1.26592.2.1.2.2.1.0"; |
| 64 | private static final String SERIAL_NUMBER_OID = ".1.3.6.1.4.1.26592.2.1.2.2.2.0"; |
| 65 | |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 66 | private static final String PORT_ENTRY_OID = ".1.3.6.1.4.1.26592.2.2.2.1.2"; |
| 67 | private static final String PORT_PATCH_OID = PORT_ENTRY_OID + ".1.2"; |
| 68 | private static final String PORT_CURRENT_STATE_OID = PORT_ENTRY_OID + ".1.3"; |
| 69 | |
| 70 | public static final int POLATIS_NUM_OF_WAVELENGTHS = 39; |
| 71 | |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
| 74 | /** |
| 75 | * Discovers device details, for Polatis Snmp device by getting the system |
| 76 | * information. |
| 77 | * |
| 78 | * @return device description |
| 79 | */ |
| 80 | @Override |
| 81 | public DeviceDescription discoverDeviceDetails() { |
| 82 | DeviceService deviceService = checkNotNull(handler().get(DeviceService.class)); |
| 83 | DeviceId deviceId = handler().data().deviceId(); |
| 84 | Device device = deviceService.getDevice(deviceId); |
| 85 | if (device == null) { |
| 86 | return new DefaultDeviceDescription(deviceId.uri(), Device.Type.FIBER_SWITCH, |
| 87 | DEFAULT_MANUFACTURER, DEFAULT_DESCRIPTION_DATA, |
| 88 | DEFAULT_DESCRIPTION_DATA, DEFAULT_DESCRIPTION_DATA, |
| 89 | new ChassisId()); |
| 90 | } |
| 91 | String hardwareVersion = DEFAULT_DESCRIPTION_DATA; |
| 92 | try { |
| 93 | hardwareVersion = hardwareVersion(); |
| 94 | } catch (IOException e) { |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 95 | log.error("Error reading hardware version for device {} exception ", deviceId, e); |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | String softwareVersion = DEFAULT_DESCRIPTION_DATA; |
| 99 | try { |
| 100 | softwareVersion = softwareVersion(); |
| 101 | } catch (IOException e) { |
| 102 | log.error("Error reading software version for device {} exception {}", deviceId, e); |
| 103 | } |
| 104 | |
| 105 | String serialNumber = DEFAULT_DESCRIPTION_DATA; |
| 106 | try { |
| 107 | serialNumber = serialNumber(); |
| 108 | } catch (IOException e) { |
| 109 | log.error("Error reading serial number for device {} exception {}", deviceId, e); |
| 110 | } |
| 111 | |
| 112 | return new DefaultDeviceDescription(deviceId.uri(), Device.Type.FIBER_SWITCH, |
| 113 | DEFAULT_MANUFACTURER, hardwareVersion, |
| 114 | softwareVersion, serialNumber, |
| 115 | device.chassisId(), (SparseAnnotations) device.annotations()); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Discovers port details, for Polatis Snmp device. |
| 120 | * |
| 121 | * @return port list |
| 122 | */ |
| 123 | @Override |
| 124 | public List<PortDescription> discoverPortDetails() { |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 125 | List<PortDescription> ports = Lists.newArrayList(); |
| 126 | List<TableEvent> events; |
| 127 | DeviceId deviceId = handler().data().deviceId(); |
| 128 | |
| 129 | try { |
| 130 | OID[] columnOIDs = {new OID(PORT_CURRENT_STATE_OID)}; |
| 131 | events = getTable(handler(), columnOIDs); |
| 132 | } catch (IOException e) { |
| 133 | log.error("Error reading ports table for device {} exception {}", deviceId, e); |
| 134 | return ports; |
| 135 | } |
| 136 | |
| 137 | if (events == null) { |
| 138 | log.error("Error reading ports table for device {}", deviceId); |
| 139 | return ports; |
| 140 | } |
| 141 | |
| 142 | for (TableEvent event : events) { |
| 143 | if (event == null) { |
| 144 | log.error("Error reading event for device {}", deviceId); |
| 145 | continue; |
| 146 | } |
| 147 | VariableBinding[] columns = event.getColumns(); |
| 148 | if (columns == null) { |
| 149 | log.error("Error reading columns for device {} event {}", deviceId, event); |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | VariableBinding portColumn = columns[0]; |
| 154 | if (portColumn == null) { |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | int port = event.getIndex().last(); |
| 159 | boolean enabled = (portColumn.getVariable().toInt() == 1); |
| 160 | PortNumber portNumber = PortNumber.portNumber(port); |
| 161 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 162 | double opticalBand = Spectrum.O_BAND_MIN.asGHz() - Spectrum.L_BAND_MAX.asGHz(); |
| 163 | Frequency opticalGrid = Frequency.ofGHz(opticalBand / POLATIS_NUM_OF_WAVELENGTHS); |
| 164 | PortDescription p = omsPortDescription(portNumber, |
| 165 | enabled, |
| 166 | Spectrum.O_BAND_MIN, |
| 167 | Spectrum.L_BAND_MAX, |
| 168 | opticalGrid, |
| 169 | annotations); |
| 170 | ports.add(p); |
| 171 | } |
| 172 | |
| 173 | return ports; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private String hardwareVersion() throws IOException { |
| 177 | return getOid(handler(), PRODUCT_CODE_OID); |
| 178 | } |
| 179 | |
| 180 | private String softwareVersion() throws IOException { |
| 181 | return getOid(handler(), SOFTWARE_VERSION_OID); |
| 182 | } |
| 183 | |
| 184 | private String serialNumber() throws IOException { |
| 185 | return getOid(handler(), SERIAL_NUMBER_OID); |
| 186 | } |
| 187 | } |