blob: c49d6edab7da588614fbb641b6e2e373b44bf3a0 [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
Andrea Campanella6c71a052016-04-22 11:56:31 -070019import com.google.common.collect.ImmutableList;
Marc De Leenheerc662d322016-02-18 16:05:10 -080020import com.google.common.collect.Lists;
21import org.onosproject.net.AnnotationKeys;
22import org.onosproject.net.DefaultAnnotations;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070023import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
Marc De Leenheerc662d322016-02-18 16:05:10 -080025import org.onosproject.net.PortNumber;
26import org.onosproject.net.SparseAnnotations;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070027import org.onosproject.net.device.DefaultDeviceDescription;
28import org.onosproject.net.device.DeviceDescription;
29import org.onosproject.net.device.DeviceDescriptionDiscovery;
30import org.onosproject.net.device.DeviceService;
Marc De Leenheerc662d322016-02-18 16:05:10 -080031import org.onosproject.net.device.PortDescription;
32import org.onosproject.net.driver.AbstractHandlerBehaviour;
33import org.slf4j.Logger;
34import org.snmp4j.smi.OID;
35import org.snmp4j.smi.VariableBinding;
36import org.snmp4j.util.TreeEvent;
37
38import java.io.IOException;
39import java.util.Collections;
40import java.util.List;
41
Andrea Campanellac2d754b2016-03-29 17:51:07 -070042import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070043import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
Marc De Leenheerc662d322016-02-18 16:05:10 -080044import static org.slf4j.LoggerFactory.getLogger;
45
46/**
Andrea Campanellac2d754b2016-03-29 17:51:07 -070047 * Device description behaviour for Lumentum Snmp devices.
Marc De Leenheerc662d322016-02-18 16:05:10 -080048 */
Marc De Leenheer57a5af02016-12-02 20:54:41 -080049public class LumentumRoadmDiscovery extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery {
Marc De Leenheerc662d322016-02-18 16:05:10 -080050
Andrea Campanellac2d754b2016-03-29 17:51:07 -070051 private final Logger log = getLogger(getClass());
Marc De Leenheerc662d322016-02-18 16:05:10 -080052
53 private static final String CTRL_PORT_STATE = ".1.3.6.1.4.1.46184.1.4.1.1.3.";
54
55 private LumentumSnmpDevice snmp;
56
57 @Override
Andrea Campanellac2d754b2016-03-29 17:51:07 -070058 public DeviceDescription discoverDeviceDetails() {
59 //TODO get device description
60 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
61 DeviceId deviceId = handler().data().deviceId();
62 Device device = deviceService.getDevice(deviceId);
63 return new DefaultDeviceDescription(device.id().uri(), Device.Type.ROADM,
64 "Lumentum", "SDN ROADM", "1.0", "v1",
65 device.chassisId(), (SparseAnnotations) device.annotations());
66 }
67
68 @Override
69 public List<PortDescription> discoverPortDetails() {
Andrea Campanella6c71a052016-04-22 11:56:31 -070070 return ImmutableList.copyOf(this.getPorts());
Andrea Campanellac2d754b2016-03-29 17:51:07 -070071 }
72
73 private List<PortDescription> getPorts() {
Marc De Leenheerc662d322016-02-18 16:05:10 -080074 try {
75 snmp = new LumentumSnmpDevice(handler().data().deviceId());
76 } catch (IOException e) {
77 log.error("Failed to connect to device: ", e);
78
79 return Collections.emptyList();
80 }
81
82 List<PortDescription> ports = Lists.newLinkedList();
83
84 OID[] oids = {
85 new OID(CTRL_PORT_STATE + "1"),
86 new OID(CTRL_PORT_STATE + "2")
87 };
88
89 for (OID oid : oids) {
90
91 for (TreeEvent event : snmp.get(oid)) {
92 if (event != null) {
93 VariableBinding[] varBindings = event.getVariableBindings();
94 for (VariableBinding varBinding : varBindings) {
95 if (varBinding.getVariable().toInt() == 1) {
96 int portNumber = varBinding.getOid().removeLast();
97 int portDirection = varBinding.getOid().removeLast();
98 SparseAnnotations ann = DefaultAnnotations.builder()
99 .set(AnnotationKeys.PORT_NAME, portDirection + "-" + portNumber)
100 .build();
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700101 PortDescription p = omsPortDescription(
Marc De Leenheerc662d322016-02-18 16:05:10 -0800102 PortNumber.portNumber(ports.size() + 1),
103 true,
104 LumentumSnmpDevice.START_CENTER_FREQ,
105 LumentumSnmpDevice.END_CENTER_FREQ,
106 LumentumSnmpDevice.CHANNEL_SPACING.frequency(),
107 ann);
108 ports.add(p);
109 }
110 }
111 }
112 }
113 }
114
115 // Create LINE IN and LINE OUT ports as these are not reported through SNMP
116 SparseAnnotations annLineIn = DefaultAnnotations.builder()
117 .set(AnnotationKeys.PORT_NAME, "LINE IN")
118 .build();
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700119 ports.add(omsPortDescription(
Marc De Leenheerc662d322016-02-18 16:05:10 -0800120 PortNumber.portNumber(ports.size() + 1),
121 true,
122 LumentumSnmpDevice.START_CENTER_FREQ,
123 LumentumSnmpDevice.END_CENTER_FREQ,
124 LumentumSnmpDevice.CHANNEL_SPACING.frequency(),
125 annLineIn
126 ));
127
128 SparseAnnotations annLineOut = DefaultAnnotations.builder()
129 .set(AnnotationKeys.PORT_NAME, "LINE OUT")
130 .build();
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700131 ports.add(omsPortDescription(
Marc De Leenheerc662d322016-02-18 16:05:10 -0800132 PortNumber.portNumber(ports.size() + 1),
133 true,
134 LumentumSnmpDevice.START_CENTER_FREQ,
135 LumentumSnmpDevice.END_CENTER_FREQ,
136 LumentumSnmpDevice.CHANNEL_SPACING.frequency(),
137 annLineOut
138 ));
139
140 return ports;
141 }
142}