blob: ab0b659782eb70e5338f4330760533e8db0fa862 [file] [log] [blame]
Andrea Campanellac2d754b2016-03-29 17:51:07 -07001/*
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
17package org.onosproject.drivers.bti;
18
19import com.btisystems.mibbler.mibs.netsnmp.netsnmp.I_Device;
20import com.btisystems.mibbler.mibs.netsnmp.netsnmp._OidRegistry;
21import com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.System;
22import com.btisystems.pronx.ems.core.model.ClassRegistry;
23import com.btisystems.pronx.ems.core.model.IClassRegistry;
24import com.btisystems.pronx.ems.core.model.NetworkDevice;
25import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
26import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
27import com.btisystems.pronx.ems.core.snmp.V2cSnmpConfiguration;
28import com.google.common.collect.ImmutableList;
29import org.apache.commons.lang.StringUtils;
30import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.SparseAnnotations;
33import org.onosproject.net.device.DefaultDeviceDescription;
34import org.onosproject.net.device.DeviceDescription;
35import org.onosproject.net.device.DeviceDescriptionDiscovery;
36import org.onosproject.net.device.DeviceService;
37import org.onosproject.net.device.PortDescription;
38import org.onosproject.net.driver.AbstractHandlerBehaviour;
39import org.onosproject.snmp.SnmpController;
40import org.onosproject.snmp.SnmpDevice;
41import org.slf4j.Logger;
42
43import java.io.IOException;
44import java.util.Collections;
45import java.util.List;
46
47import static com.google.common.base.Preconditions.checkNotNull;
48import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Net SNMP device description behaviour. Provides device description and port information.
52 */
53public class NetSnmpDeviceDescriptor extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery {
54 private final Logger log = getLogger(getClass());
55 protected static final IClassRegistry CLASS_REGISTRY =
56 new ClassRegistry(_OidRegistry.oidRegistry, I_Device.class);
57 private static final String UNKNOWN = "unknown";
58
59
60 //TODO evaluate a common abstract class for all Snmp description discovery
61 @Override
62 public DeviceDescription discoverDeviceDetails() {
63 SnmpController controller = checkNotNull(handler().get(SnmpController.class));
64 DeviceId deviceId = handler().data().deviceId();
65 SnmpDevice snmpDevice = controller.getDevice(deviceId);
66 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
67 Device device = deviceService.getDevice(deviceId);
68 DeviceDescription desc = null;
69 String ipAddress = snmpDevice.getSnmpHost();
70 int port = snmpDevice.getSnmpPort();
71
72 ISnmpConfiguration config = new V2cSnmpConfiguration();
73 config.setPort(port);
74
75 try (ISnmpSession session = controller.getSession(deviceId)) {
76 // Each session will be auto-closed.
77 String deviceOid = session.identifyDevice();
78 //TODO obtain desctiption
79 desc = populateDescription(session, device);
80
81 } catch (IOException | RuntimeException ex) {
82 log.error("Failed to walk device.", ex.getMessage());
83 log.debug("Detailed problem was ", ex);
84 }
85
86 return desc;
87 }
88
89 @Override
90 public List<PortDescription> discoverPortDetails() {
91 //TODO implement
92 return ImmutableList.of();
93 }
94
95 private DeviceDescription populateDescription(ISnmpSession session, Device device) {
96 NetworkDevice networkDevice = new NetworkDevice(CLASS_REGISTRY,
97 session.getAddress().getHostAddress());
98 try {
99 session.walkDevice(networkDevice, Collections.singletonList(CLASS_REGISTRY.getClassToOidMap().get(
100 System.class)));
101
102 com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.System systemTree =
103 (com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.System)
104 networkDevice.getRootObject().getEntity(CLASS_REGISTRY.getClassToOidMap().get(
105 com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.System.class));
106 if (systemTree != null) {
107 // TODO SNMP sys-contacts may be verbose; ONOS-GUI doesn't abbreviate fields neatly;
108 // so cut it here until supported in prop displayer
109 String manufacturer = StringUtils.abbreviate(systemTree.getSysContact(), 20);
110 return new DefaultDeviceDescription(device.id().uri(), device.type(),
111 manufacturer, UNKNOWN, UNKNOWN, UNKNOWN,
112 device.chassisId(), (SparseAnnotations) device.annotations());
113 }
114 } catch (IOException ex) {
115 throw new IllegalArgumentException("Error reading details for device." + session.getAddress(), ex);
116 }
117 return null;
118 }
119}