Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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.lumentum; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.onosproject.net.AnnotationKeys; |
| 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.PortNumber; |
| 23 | import org.onosproject.net.SparseAnnotations; |
| 24 | import org.onosproject.net.behaviour.PortDiscovery; |
| 25 | import org.onosproject.net.device.OmsPortDescription; |
| 26 | import org.onosproject.net.device.PortDescription; |
| 27 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.snmp4j.smi.OID; |
| 30 | import org.snmp4j.smi.VariableBinding; |
| 31 | import org.snmp4j.util.TreeEvent; |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | import java.util.Collections; |
| 35 | import java.util.List; |
| 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Discovers the ports of a Lumentum SDN ROADM device using SNMP. |
| 41 | */ |
| 42 | public class PortDiscoveryLumentumRoadm extends AbstractHandlerBehaviour |
| 43 | implements PortDiscovery { |
| 44 | |
| 45 | private final Logger log = getLogger(PortDiscoveryLumentumRoadm.class); |
| 46 | |
| 47 | private static final String CTRL_PORT_STATE = ".1.3.6.1.4.1.46184.1.4.1.1.3."; |
| 48 | |
| 49 | private LumentumSnmpDevice snmp; |
| 50 | |
| 51 | @Override |
| 52 | public List<PortDescription> getPorts() { |
| 53 | try { |
| 54 | snmp = new LumentumSnmpDevice(handler().data().deviceId()); |
| 55 | } catch (IOException e) { |
| 56 | log.error("Failed to connect to device: ", e); |
| 57 | |
| 58 | return Collections.emptyList(); |
| 59 | } |
| 60 | |
| 61 | List<PortDescription> ports = Lists.newLinkedList(); |
| 62 | |
| 63 | OID[] oids = { |
| 64 | new OID(CTRL_PORT_STATE + "1"), |
| 65 | new OID(CTRL_PORT_STATE + "2") |
| 66 | }; |
| 67 | |
| 68 | for (OID oid : oids) { |
| 69 | |
| 70 | for (TreeEvent event : snmp.get(oid)) { |
| 71 | if (event != null) { |
| 72 | VariableBinding[] varBindings = event.getVariableBindings(); |
| 73 | for (VariableBinding varBinding : varBindings) { |
| 74 | if (varBinding.getVariable().toInt() == 1) { |
| 75 | int portNumber = varBinding.getOid().removeLast(); |
| 76 | int portDirection = varBinding.getOid().removeLast(); |
| 77 | SparseAnnotations ann = DefaultAnnotations.builder() |
| 78 | .set(AnnotationKeys.PORT_NAME, portDirection + "-" + portNumber) |
| 79 | .build(); |
| 80 | PortDescription p = new OmsPortDescription( |
| 81 | PortNumber.portNumber(ports.size() + 1), |
| 82 | true, |
| 83 | LumentumSnmpDevice.START_CENTER_FREQ, |
| 84 | LumentumSnmpDevice.END_CENTER_FREQ, |
| 85 | LumentumSnmpDevice.CHANNEL_SPACING.frequency(), |
| 86 | ann); |
| 87 | ports.add(p); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Create LINE IN and LINE OUT ports as these are not reported through SNMP |
| 95 | SparseAnnotations annLineIn = DefaultAnnotations.builder() |
| 96 | .set(AnnotationKeys.PORT_NAME, "LINE IN") |
| 97 | .build(); |
| 98 | ports.add(new OmsPortDescription( |
| 99 | PortNumber.portNumber(ports.size() + 1), |
| 100 | true, |
| 101 | LumentumSnmpDevice.START_CENTER_FREQ, |
| 102 | LumentumSnmpDevice.END_CENTER_FREQ, |
| 103 | LumentumSnmpDevice.CHANNEL_SPACING.frequency(), |
| 104 | annLineIn |
| 105 | )); |
| 106 | |
| 107 | SparseAnnotations annLineOut = DefaultAnnotations.builder() |
| 108 | .set(AnnotationKeys.PORT_NAME, "LINE OUT") |
| 109 | .build(); |
| 110 | ports.add(new OmsPortDescription( |
| 111 | PortNumber.portNumber(ports.size() + 1), |
| 112 | true, |
| 113 | LumentumSnmpDevice.START_CENTER_FREQ, |
| 114 | LumentumSnmpDevice.END_CENTER_FREQ, |
| 115 | LumentumSnmpDevice.CHANNEL_SPACING.frequency(), |
| 116 | annLineOut |
| 117 | )); |
| 118 | |
| 119 | return ports; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |