Search in sources :

Example 11 with NetconfDevice

use of org.onosproject.netconf.NetconfDevice 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 12 with NetconfDevice

use of org.onosproject.netconf.NetconfDevice in project onos by opennetworkinglab.

the class NetconfControllerImplTest method testGetNetconfDevice.

@Test
public void testGetNetconfDevice() {
    NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
    assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
    NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
    assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
}
Also used : NetconfDevice(org.onosproject.netconf.NetconfDevice) Test(org.junit.Test)

Example 13 with NetconfDevice

use of org.onosproject.netconf.NetconfDevice in project onos by opennetworkinglab.

the class NetconfGetConfigCommand method doExecute.

@Override
protected void doExecute() {
    deviceId = DeviceId.deviceId(uri);
    NetconfController controller = get(NetconfController.class);
    checkNotNull(controller, "Netconf controller is null");
    NetconfDevice device = controller.getDevicesMap().get(deviceId);
    if (device == null) {
        print("Netconf device object not found for %s", deviceId);
        return;
    }
    NetconfSession session = device.getSession();
    if (session == null) {
        print("Netconf session not found for %s", deviceId);
        return;
    }
    try {
        CharSequence res = session.asyncGetConfig(datastore(datastore.toLowerCase())).get(timeoutSec, TimeUnit.SECONDS);
        print("%s", res);
    } catch (NetconfException | InterruptedException | ExecutionException | TimeoutException e) {
        log.error("Configuration could not be retrieved", e);
        print("Error occurred retrieving configuration");
    }
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) NetconfException(org.onosproject.netconf.NetconfException) NetconfDevice(org.onosproject.netconf.NetconfDevice) ExecutionException(java.util.concurrent.ExecutionException) NetconfController(org.onosproject.netconf.NetconfController) TimeoutException(java.util.concurrent.TimeoutException)

Example 14 with NetconfDevice

use of org.onosproject.netconf.NetconfDevice in project onos by opennetworkinglab.

the class NetconfControllerImpl method stopDevice.

private void stopDevice(DeviceId deviceId, boolean remove) {
    Lock mutex;
    synchronized (netconfCreateMutex) {
        mutex = netconfCreateMutex.remove(deviceId);
    }
    NetconfDevice nc;
    if (mutex == null) {
        log.warn("Unexpected stoping a device that has no lock");
        nc = netconfDeviceMap.remove(deviceId);
    } else {
        mutex.lock();
        try {
            nc = netconfDeviceMap.remove(deviceId);
        } finally {
            mutex.unlock();
        }
    }
    if (nc != null) {
        nc.disconnect();
    }
    if (remove) {
        for (NetconfDeviceListener l : netconfDeviceListeners) {
            l.deviceRemoved(deviceId);
        }
    }
}
Also used : NetconfDevice(org.onosproject.netconf.NetconfDevice) NetconfDeviceListener(org.onosproject.netconf.NetconfDeviceListener) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 15 with NetconfDevice

use of org.onosproject.netconf.NetconfDevice in project onos by opennetworkinglab.

the class NetconfAlarmProvider method deactivate.

@Deactivate
public void deactivate() {
    providerRegistry.unregister(this);
    idNotificationListenerMap.forEach((id, listener) -> {
        NetconfDevice device = controller.getNetconfDevice(id);
        if (device.isMasterSession()) {
            try {
                device.getSession().removeDeviceOutputListener(listener);
            } catch (NetconfException e) {
                log.error("RemoveDeviceOutputListener Error {}", e.getMessage());
            }
        }
    });
    controller.removeDeviceListener(deviceListener);
    providerService = null;
    log.info("NetconfAlarmProvider Stopped");
}
Also used : NetconfException(org.onosproject.netconf.NetconfException) NetconfDevice(org.onosproject.netconf.NetconfDevice) Deactivate(org.osgi.service.component.annotations.Deactivate)

Aggregations

NetconfDevice (org.onosproject.netconf.NetconfDevice)37 NetconfController (org.onosproject.netconf.NetconfController)20 NetconfException (org.onosproject.netconf.NetconfException)13 DeviceId (org.onosproject.net.DeviceId)8 NetconfSession (org.onosproject.netconf.NetconfSession)6 Test (org.junit.Test)5 NetconfDeviceInfo (org.onosproject.netconf.NetconfDeviceInfo)5 ExecutionException (java.util.concurrent.ExecutionException)4 NetconfDeviceConfig (org.onosproject.netconf.config.NetconfDeviceConfig)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 TimeoutException (java.util.concurrent.TimeoutException)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 IpAddress (org.onlab.packet.IpAddress)2 MastershipService (org.onosproject.mastership.MastershipService)2 DefaultDeviceDescription (org.onosproject.net.device.DefaultDeviceDescription)2 DeviceDescription (org.onosproject.net.device.DeviceDescription)2 DefaultDriver (org.onosproject.net.driver.DefaultDriver)2 DefaultDriverData (org.onosproject.net.driver.DefaultDriverData)2 DriverHandler (org.onosproject.net.driver.DriverHandler)2