Search in sources :

Example 1 with SnmpException

use of org.onosproject.snmp.SnmpException in project onos by opennetworkinglab.

the class DefaultSnmpController method getSession.

@Override
@Deprecated
public ISnmpSession getSession(DeviceId deviceId) throws IOException {
    if (!sessionMap.containsKey(deviceId)) {
        SnmpDevice device = snmpDeviceMap.get(deviceId);
        ISnmpSessionFactory sessionFactory = factoryMap.get(device.getVersion());
        if (Objects.isNull(sessionFactory)) {
            log.error("Invalid session factory", deviceId);
            throw new SnmpException("Invalid session factory");
        }
        String ipAddress = null;
        int port = -1;
        if (device != null) {
            ipAddress = device.getSnmpHost();
            port = device.getSnmpPort();
        } else {
            String[] deviceComponents = deviceId.toString().split(":");
            if (deviceComponents.length > 1) {
                ipAddress = deviceComponents[1];
                port = Integer.parseInt(deviceComponents[2]);
            } else {
                log.error("Cannot obtain correct information from device id", deviceId);
            }
        }
        Preconditions.checkNotNull(ipAddress, "ip address is empty, cannot start session");
        Preconditions.checkArgument(port != -1, "port is incorrect, cannot start session");
        ISnmpConfiguration config;
        if (device.getVersion() == SnmpConstants.version2c) {
            config = new V2cSnmpConfiguration();
            config.setCommunity(device.getCommunity());
        } else if (device.getVersion() == SnmpConstants.version3) {
            DefaultSnmpv3Device v3Device = (DefaultSnmpv3Device) device;
            config = V3SnmpConfiguration.builder().setAddress(ipAddress).setSecurityName(v3Device.getSecurityName()).setSecurityLevel(v3Device.getSecurityLevel()).setAuthenticationProtocol(v3Device.getAuthProtocol()).setAuthenticationPassword(v3Device.getAuthPassword()).setPrivacyProtocol(v3Device.getPrivProtocol()).setPrivacyPassword(v3Device.getPrivPassword()).setContextName(v3Device.getContextName()).build();
        } else {
            throw new SnmpException(String.format("Invalid snmp version %d", device.getVersion()));
        }
        config.setPort(port);
        sessionMap.put(deviceId, sessionFactory.createSession(config, ipAddress));
    }
    return sessionMap.get(deviceId);
}
Also used : SnmpException(org.onosproject.snmp.SnmpException) SnmpDevice(org.onosproject.snmp.SnmpDevice) ISnmpSessionFactory(com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory) V2cSnmpConfiguration(com.btisystems.pronx.ems.core.snmp.V2cSnmpConfiguration) ISnmpConfiguration(com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration)

Example 2 with SnmpException

use of org.onosproject.snmp.SnmpException in project onos by opennetworkinglab.

the class SnmpDeviceProvider method connectDevices.

// Method to register devices provided via net-cfg under devices/ tree
private void connectDevices() {
    Set<DeviceId> deviceSubjects = netCfgService.getSubjects(DeviceId.class, SnmpDeviceConfig.class);
    deviceSubjects.forEach(deviceId -> {
        SnmpDeviceConfig config = netCfgService.getConfig(deviceId, SnmpDeviceConfig.class);
        if (config.version() == SnmpConstants.version2c) {
            buildDevice(new DefaultSnmpDevice(config));
        } else if (config.version() == SnmpConstants.version3) {
            buildDevice(new DefaultSnmpv3Device(config));
        } else {
            throw new SnmpException(String.format("Invalid snmp version %d", config.version()));
        }
    });
}
Also used : DefaultSnmpDevice(org.onosproject.snmp.ctl.DefaultSnmpDevice) SnmpException(org.onosproject.snmp.SnmpException) DefaultSnmpv3Device(org.onosproject.snmp.ctl.DefaultSnmpv3Device) DeviceId(org.onosproject.net.DeviceId) SnmpDeviceConfig(org.onosproject.snmp.SnmpDeviceConfig)

Aggregations

SnmpException (org.onosproject.snmp.SnmpException)2 ISnmpConfiguration (com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration)1 ISnmpSessionFactory (com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory)1 V2cSnmpConfiguration (com.btisystems.pronx.ems.core.snmp.V2cSnmpConfiguration)1 DeviceId (org.onosproject.net.DeviceId)1 SnmpDevice (org.onosproject.snmp.SnmpDevice)1 SnmpDeviceConfig (org.onosproject.snmp.SnmpDeviceConfig)1 DefaultSnmpDevice (org.onosproject.snmp.ctl.DefaultSnmpDevice)1 DefaultSnmpv3Device (org.onosproject.snmp.ctl.DefaultSnmpv3Device)1