Search in sources :

Example 1 with NetconfDeviceConfig

use of org.onosproject.netconf.config.NetconfDeviceConfig in project onos by opennetworkinglab.

the class NetconfControllerImplTest method setUp.

@Before
public void setUp() throws Exception {
    ctrl = new NetconfControllerImpl();
    ctrl.deviceFactory = (ncDevInfo) -> new TestNetconfDevice(ncDevInfo);
    ctrl.cfgService = cfgService;
    ctrl.deviceService = deviceService;
    ctrl.deviceKeyService = deviceKeyService;
    ctrl.netCfgService = netCfgService;
    ctrl.mastershipService = mastershipService;
    NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
    NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
    NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
    ctrl.clusterCommunicator = clusterCommunicationService;
    ctrl.clusterService = mockClusterService;
    // Creating mock devices
    deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
    deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
    deviceInfo2.setSshClientLib(Optional.of(NetconfSshClientLib.APACHE_MINA));
    badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
    deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
    deviceConfig10Id = DeviceId.deviceId("netconf:" + DEVICE_10_IP + ":" + DEVICE_10_PORT);
    // Create a JSON entry just like Network Config accepts
    ObjectMapper mapper = new ObjectMapper();
    String jsonMessage = "{\n" + "  \"ip\":\"" + DEVICE_10_IP + "\",\n" + "  \"port\":" + DEVICE_10_PORT + ",\n" + "  \"username\":\"" + DEVICE_10_USERNAME + "\",\n" + "  \"password\":\"" + DEVICE_10_PASSWORD + "\",\n" + "  \"" + NetconfDeviceConfig.CONNECT_TIMEOUT + "\":" + DEVICE_10_CONNECT_TIMEOUT + ",\n" + "  \"" + NetconfDeviceConfig.REPLY_TIMEOUT + "\":" + DEVICE_10_REPLY_TIMEOUT + ",\n" + "  \"" + NetconfDeviceConfig.IDLE_TIMEOUT + "\":" + DEVICE_10_IDLE_TIMEOUT + ",\n" + "  \"" + NetconfDeviceConfig.SSHCLIENT + "\":\"" + NetconfSshClientLib.APACHE_MINA.toString() + "\"\n" + "}";
    InputStream jsonStream = new ByteArrayInputStream(jsonMessage.getBytes());
    JsonNode jsonNode = mapper.readTree(jsonStream);
    jsonStream.close();
    ConfigApplyDelegate delegate = new MockDelegate();
    deviceConfig10 = new NetconfDeviceConfig();
    deviceConfig10.init(deviceConfig10Id, "netconf", jsonNode, mapper, delegate);
    device1 = new TestNetconfDevice(deviceInfo1);
    deviceId1 = deviceInfo1.getDeviceId();
    device2 = new TestNetconfDevice(deviceInfo2);
    deviceId2 = deviceInfo2.getDeviceId();
    // Adding to the map for testing get device calls.
    Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
    field1.setAccessible(true);
    reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
    reflectedDeviceMap.put(deviceId1, device1);
    reflectedDeviceMap.put(deviceId2, device2);
    // Creating mock events for testing NetconfDeviceOutputEventListener
    Field field2 = ctrl.getClass().getDeclaredField("downListener");
    field2.setAccessible(true);
    reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
    eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null, null, Optional.of(1), deviceInfo1);
    eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null, null, Optional.of(2), deviceInfo2);
}
Also used : NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DeviceId(org.onosproject.net.DeviceId) JsonNode(com.fasterxml.jackson.databind.JsonNode) ConfigApplyDelegate(org.onosproject.net.config.ConfigApplyDelegate) NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent) Field(java.lang.reflect.Field) NetconfDevice(org.onosproject.netconf.NetconfDevice) ByteArrayInputStream(java.io.ByteArrayInputStream) NetconfDeviceConfig(org.onosproject.netconf.config.NetconfDeviceConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 2 with NetconfDeviceConfig

use of org.onosproject.netconf.config.NetconfDeviceConfig in project onos by opennetworkinglab.

the class NetconfDeviceProvider method initiateConnection.

// initiating the SSh connection the a given device.
private void initiateConnection(DeviceId deviceId) {
    if (!isReachable(deviceId)) {
        log.warn("Can't connect to device {}", deviceId);
        return;
    }
    try {
        NetconfDevice deviceNetconf = controller.connectDevice(deviceId);
        if (deviceNetconf != null) {
            // storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId);
            NetconfDeviceConfig config = cfgService.getConfig(deviceId, NetconfDeviceConfig.class);
            // getting the device description
            DeviceDescription deviceDescription = getDeviceDescription(deviceId, config);
            // connecting device to ONOS
            log.debug("Connected NETCONF device {}, on {}:{} {} with username {}", deviceId, config.ip(), config.port(), (config.path().isPresent() ? "/" + config.path().get() : ""), config.username());
            providerService.deviceConnected(deviceId, deviceDescription);
        } else {
            mastershipService.relinquishMastership(deviceId);
            deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
            log.error("Can't connect to NETCONF device {}", deviceId);
        }
    } catch (Exception e) {
        mastershipService.relinquishMastership(deviceId);
        deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
        throw new IllegalStateException(new NetconfException("Can't connect to NETCONF device " + deviceId, e));
    }
}
Also used : DeviceDescription(org.onosproject.net.device.DeviceDescription) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) NetconfException(org.onosproject.netconf.NetconfException) NetconfDevice(org.onosproject.netconf.NetconfDevice) NetconfDeviceConfig(org.onosproject.netconf.config.NetconfDeviceConfig) NetconfException(org.onosproject.netconf.NetconfException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with NetconfDeviceConfig

use of org.onosproject.netconf.config.NetconfDeviceConfig in project onos by opennetworkinglab.

the class NetconfControllerImpl method connectDevice.

@Override
public NetconfDevice connectDevice(DeviceId deviceId, boolean isMaster) throws NetconfException {
    NetconfDeviceConfig netCfg = netCfgService.getConfig(deviceId, NetconfDeviceConfig.class);
    NetconfDeviceInfo deviceInfo = null;
    /*
         * A bit of an ugly race condition can be found here. It is possible
         * that this method is called to create a connection to device A and
         * while that device is in the process of being created another call
         * to this method for A will be invoked. Since the first call to
         * create A has not been completed device A is not in the the
         * netconfDeviceMap yet.
         *
         * To prevent this situation a mutex is introduced so that the first
         * call will be allowed to complete before the second is processed.
         * The mutex is based on the device ID, so that it should be still
         * possible to connect to different devices concurrently.
         */
    Lock mutex;
    synchronized (netconfCreateMutex) {
        mutex = netconfCreateMutex.get(deviceId);
        if (mutex == null) {
            mutex = new ReentrantLock();
            netconfCreateMutex.put(deviceId, mutex);
        }
    }
    mutex.lock();
    try {
        if (netconfDeviceMap.containsKey(deviceId)) {
            // If not master or already has session: return, otherwise create device again.
            if (!isMaster || netconfDeviceMap.get(deviceId).isMasterSession()) {
                log.debug("Device {} is already present", deviceId);
                return netconfDeviceMap.get(deviceId);
            }
        }
        if (netCfg != null) {
            log.debug("Device {} is present in NetworkConfig", deviceId);
            deviceInfo = new NetconfDeviceInfo(netCfg, deviceId);
        } else {
            log.debug("Creating NETCONF device {}", deviceId);
            deviceInfo = createDeviceInfo(deviceId);
        }
        NetconfDevice netconfDevice = createDevice(deviceInfo, isMaster);
        if (isMaster) {
            netconfDevice.getSession().addDeviceOutputListener(downListener);
        }
        return netconfDevice;
    } finally {
        mutex.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) NetconfDevice(org.onosproject.netconf.NetconfDevice) NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) NetconfDeviceConfig(org.onosproject.netconf.config.NetconfDeviceConfig) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

NetconfDevice (org.onosproject.netconf.NetconfDevice)3 NetconfDeviceConfig (org.onosproject.netconf.config.NetconfDeviceConfig)3 NetconfDeviceInfo (org.onosproject.netconf.NetconfDeviceInfo)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Before (org.junit.Before)1 DeviceId (org.onosproject.net.DeviceId)1 ConfigApplyDelegate (org.onosproject.net.config.ConfigApplyDelegate)1 DefaultDeviceDescription (org.onosproject.net.device.DefaultDeviceDescription)1 DeviceDescription (org.onosproject.net.device.DeviceDescription)1 NetconfDeviceOutputEvent (org.onosproject.netconf.NetconfDeviceOutputEvent)1 NetconfException (org.onosproject.netconf.NetconfException)1