Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 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.bti; |
| 18 | |
| 19 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp.I_Device; |
| 20 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp._OidRegistry; |
| 21 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.System; |
| 22 | import com.btisystems.pronx.ems.core.model.ClassRegistry; |
| 23 | import com.btisystems.pronx.ems.core.model.IClassRegistry; |
| 24 | import com.btisystems.pronx.ems.core.model.NetworkDevice; |
| 25 | import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration; |
| 26 | import com.btisystems.pronx.ems.core.snmp.ISnmpSession; |
| 27 | import com.btisystems.pronx.ems.core.snmp.V2cSnmpConfiguration; |
| 28 | import com.google.common.collect.ImmutableList; |
| 29 | import org.apache.commons.lang.StringUtils; |
| 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.SparseAnnotations; |
| 33 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 34 | import org.onosproject.net.device.DeviceDescription; |
| 35 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 36 | import org.onosproject.net.device.DeviceService; |
| 37 | import org.onosproject.net.device.PortDescription; |
| 38 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 39 | import org.onosproject.snmp.SnmpController; |
| 40 | import org.onosproject.snmp.SnmpDevice; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.io.IOException; |
| 44 | import java.util.Collections; |
| 45 | import java.util.List; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkNotNull; |
| 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Net SNMP device description behaviour. Provides device description and port information. |
| 52 | */ |
| 53 | public 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 | } |