blob: ab780b5853ca141b7cf690908b93cf8ba541a13e [file] [log] [blame]
Andrea Campanellac2d754b2016-03-29 17:51:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andrea Campanellac2d754b2016-03-29 17:51:07 -07003 *
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.bti7000.bti7000_13_2_0.I_Device;
20import com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0._OidRegistry;
21import com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0.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.onosproject.net.Device;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.SparseAnnotations;
32import org.onosproject.net.device.DefaultDeviceDescription;
33import org.onosproject.net.device.DeviceDescription;
34import org.onosproject.net.device.DeviceDescriptionDiscovery;
35import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.device.PortDescription;
37import org.onosproject.net.driver.AbstractHandlerBehaviour;
38import org.onosproject.snmp.SnmpController;
39import org.onosproject.snmp.SnmpDevice;
40import org.slf4j.Logger;
41
42import java.io.IOException;
43import java.util.Collections;
44import java.util.List;
45
46import static com.google.common.base.Preconditions.checkNotNull;
47import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * Bti 7000 SNMP device description behaviour. Provides device description and port information.
51 */
52public class Bti7000DeviceDescriptor extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery {
53
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 @Override
60 public DeviceDescription discoverDeviceDetails() {
61 SnmpController controller = checkNotNull(handler().get(SnmpController.class));
62 DeviceId deviceId = handler().data().deviceId();
63 SnmpDevice snmpDevice = controller.getDevice(deviceId);
64 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
65 Device device = deviceService.getDevice(deviceId);
66 DeviceDescription desc = null;
67 String ipAddress = snmpDevice.getSnmpHost();
68 int port = snmpDevice.getSnmpPort();
69
70 ISnmpConfiguration config = new V2cSnmpConfiguration();
71 config.setPort(port);
72
73 try (ISnmpSession session = controller.getSession(deviceId)) {
74 // Each session will be auto-closed.
75 String deviceOid = session.identifyDevice();
76 desc = populateDescription(session, device);
77
78 } catch (IOException | RuntimeException ex) {
79 log.error("Failed to walk device.", ex.getMessage());
80 log.debug("Detailed problem was ", ex);
81 }
82 return desc;
83 }
84
85 @Override
86 public List<PortDescription> discoverPortDetails() {
87 //TODO implement
88 return ImmutableList.of();
89 }
90
91 private DeviceDescription populateDescription(ISnmpSession session, Device device) {
92 NetworkDevice networkDevice = new NetworkDevice(CLASS_REGISTRY,
93 session.getAddress().getHostAddress());
94 try {
95 session.walkDevice(networkDevice, Collections.singletonList(CLASS_REGISTRY.getClassToOidMap().get(
96 System.class)));
97
98 com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0.mib_2.System systemTree =
99 (com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0.mib_2.System)
100 networkDevice.getRootObject().getEntity(CLASS_REGISTRY.getClassToOidMap().get(
101 com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0.mib_2.System.class));
102 if (systemTree != null) {
103 String[] systemComponents = systemTree.getSysDescr().split(";");
104 return new DefaultDeviceDescription(device.id().uri(), device.type(),
105 systemComponents[0], systemComponents[2],
106 systemComponents[3], UNKNOWN, device.chassisId(),
107 (SparseAnnotations) device.annotations());
108 }
109 } catch (IOException ex) {
110 throw new IllegalArgumentException("Error reading details for device." + session.getAddress(), ex);
111 }
112 return null;
113 }
114}