blob: 0bf9b1301d741023ab91e7f0141623cca8ffda34 [file] [log] [blame]
Marc De Leenheerc662d322016-02-18 16:05:10 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Marc De Leenheerc662d322016-02-18 16:05:10 -08003 *
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.lumentum;
18
19import com.google.common.collect.Lists;
20import org.onosproject.net.AnnotationKeys;
21import org.onosproject.net.DefaultAnnotations;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070022import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
Marc De Leenheerc662d322016-02-18 16:05:10 -080024import org.onosproject.net.PortNumber;
25import org.onosproject.net.SparseAnnotations;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070026import org.onosproject.net.device.DefaultDeviceDescription;
27import org.onosproject.net.device.DeviceDescription;
28import org.onosproject.net.device.DeviceDescriptionDiscovery;
29import org.onosproject.net.device.DeviceService;
Marc De Leenheerc662d322016-02-18 16:05:10 -080030import org.onosproject.net.device.PortDescription;
31import org.onosproject.net.driver.AbstractHandlerBehaviour;
32import org.slf4j.Logger;
33import org.snmp4j.smi.OID;
34import org.snmp4j.smi.VariableBinding;
35import org.snmp4j.util.TreeEvent;
36
37import java.io.IOException;
38import java.util.Collections;
39import java.util.List;
40
Andrea Campanellac2d754b2016-03-29 17:51:07 -070041import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070042import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
Marc De Leenheerc662d322016-02-18 16:05:10 -080043import static org.slf4j.LoggerFactory.getLogger;
44
45/**
Andrea Campanellac2d754b2016-03-29 17:51:07 -070046 * Device description behaviour for Lumentum Snmp devices.
Marc De Leenheerc662d322016-02-18 16:05:10 -080047 */
Andrea Campanellac2d754b2016-03-29 17:51:07 -070048public class LumentumRoadmDeviceDescription extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery {
Marc De Leenheerc662d322016-02-18 16:05:10 -080049
Andrea Campanellac2d754b2016-03-29 17:51:07 -070050 private final Logger log = getLogger(getClass());
Marc De Leenheerc662d322016-02-18 16:05:10 -080051
52 private static final String CTRL_PORT_STATE = ".1.3.6.1.4.1.46184.1.4.1.1.3.";
53
54 private LumentumSnmpDevice snmp;
55
56 @Override
Andrea Campanellac2d754b2016-03-29 17:51:07 -070057 public DeviceDescription discoverDeviceDetails() {
58 //TODO get device description
59 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
60 DeviceId deviceId = handler().data().deviceId();
61 Device device = deviceService.getDevice(deviceId);
62 return new DefaultDeviceDescription(device.id().uri(), Device.Type.ROADM,
63 "Lumentum", "SDN ROADM", "1.0", "v1",
64 device.chassisId(), (SparseAnnotations) device.annotations());
65 }
66
67 @Override
68 public List<PortDescription> discoverPortDetails() {
69 return this.getPorts();
70 }
71
72 private List<PortDescription> getPorts() {
Marc De Leenheerc662d322016-02-18 16:05:10 -080073 try {
74 snmp = new LumentumSnmpDevice(handler().data().deviceId());
75 } catch (IOException e) {
76 log.error("Failed to connect to device: ", e);
77
78 return Collections.emptyList();
79 }
80
81 List<PortDescription> ports = Lists.newLinkedList();
82
83 OID[] oids = {
84 new OID(CTRL_PORT_STATE + "1"),
85 new OID(CTRL_PORT_STATE + "2")
86 };
87
88 for (OID oid : oids) {
89
90 for (TreeEvent event : snmp.get(oid)) {
91 if (event != null) {
92 VariableBinding[] varBindings = event.getVariableBindings();
93 for (VariableBinding varBinding : varBindings) {
94 if (varBinding.getVariable().toInt() == 1) {
95 int portNumber = varBinding.getOid().removeLast();
96 int portDirection = varBinding.getOid().removeLast();
97 SparseAnnotations ann = DefaultAnnotations.builder()
98 .set(AnnotationKeys.PORT_NAME, portDirection + "-" + portNumber)
99 .build();
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700100 PortDescription p = omsPortDescription(
Marc De Leenheerc662d322016-02-18 16:05:10 -0800101 PortNumber.portNumber(ports.size() + 1),
102 true,
103 LumentumSnmpDevice.START_CENTER_FREQ,
104 LumentumSnmpDevice.END_CENTER_FREQ,
105 LumentumSnmpDevice.CHANNEL_SPACING.frequency(),
106 ann);
107 ports.add(p);
108 }
109 }
110 }
111 }
112 }
113
114 // Create LINE IN and LINE OUT ports as these are not reported through SNMP
115 SparseAnnotations annLineIn = DefaultAnnotations.builder()
116 .set(AnnotationKeys.PORT_NAME, "LINE IN")
117 .build();
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700118 ports.add(omsPortDescription(
Marc De Leenheerc662d322016-02-18 16:05:10 -0800119 PortNumber.portNumber(ports.size() + 1),
120 true,
121 LumentumSnmpDevice.START_CENTER_FREQ,
122 LumentumSnmpDevice.END_CENTER_FREQ,
123 LumentumSnmpDevice.CHANNEL_SPACING.frequency(),
124 annLineIn
125 ));
126
127 SparseAnnotations annLineOut = DefaultAnnotations.builder()
128 .set(AnnotationKeys.PORT_NAME, "LINE OUT")
129 .build();
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700130 ports.add(omsPortDescription(
Marc De Leenheerc662d322016-02-18 16:05:10 -0800131 PortNumber.portNumber(ports.size() + 1),
132 true,
133 LumentumSnmpDevice.START_CENTER_FREQ,
134 LumentumSnmpDevice.END_CENTER_FREQ,
135 LumentumSnmpDevice.CHANNEL_SPACING.frequency(),
136 annLineOut
137 ));
138
139 return ports;
140 }
141}