Search in sources :

Example 1 with NetconfDeviceInfo

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

the class FujitsuNetconfControllerMock method setUp.

/**
 * Sets up initial test environment.
 *
 * @param listener listener to be added
 * @return driver handler
 * @throws NetconfException when there is a problem
 */
public FujitsuDriverHandlerAdapter setUp(FujitsuNetconfSessionListenerTest listener) throws NetconfException {
    try {
        NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(VOLT_DEVICE_USERNAME, VOLT_DEVICE_PASSWORD, IpAddress.valueOf(VOLT_DEVICE_IP), VOLT_DEVICE_PORT);
        NetconfDevice netconfDevice = connectDevice(deviceInfo.getDeviceId());
        FujitsuNetconfSessionMock session = (FujitsuNetconfSessionMock) netconfDevice.getSession();
        session.setListener(listener);
        DeviceId deviceId = deviceInfo.getDeviceId();
        DefaultDriver driver = new DefaultDriver(VOLT_DRIVER_NAME, new ArrayList<>(), "Fujitsu", "1.0", "1.0", ImmutableMap.of(), ImmutableMap.of());
        DefaultDriverData driverData = new DefaultDriverData(driver, deviceId);
        FujitsuDriverHandlerAdapter driverHandler;
        driverHandler = new FujitsuDriverHandlerAdapter(driverData);
        driverHandler.setUp(this);
        return driverHandler;
    } catch (NetconfException e) {
        throw new NetconfException("Cannot create a device ", e);
    }
}
Also used : NetconfException(org.onosproject.netconf.NetconfException) NetconfDevice(org.onosproject.netconf.NetconfDevice) NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) DeviceId(org.onosproject.net.DeviceId) DefaultDriver(org.onosproject.net.driver.DefaultDriver) DefaultDriverData(org.onosproject.net.driver.DefaultDriverData)

Example 2 with NetconfDeviceInfo

use of org.onosproject.netconf.NetconfDeviceInfo 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 3 with NetconfDeviceInfo

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

the class FujitsuNetconfControllerMock method connectDevice.

@Override
public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
    if (netconfDeviceMap.containsKey(deviceId)) {
        log.debug("Device {} is already present", deviceId);
        return netconfDeviceMap.get(deviceId);
    } else {
        log.debug("Creating NETCONF device {}", deviceId);
        String ip;
        int port;
        String[] info = deviceId.toString().split(":");
        if (info.length == 3) {
            ip = info[1];
            port = Integer.parseInt(info[2]);
        } else {
            ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) && !el.equals(info[info.length - 1])).reduce((t, u) -> t + ":" + u).get();
            log.debug("ip v6 {}", ip);
            port = Integer.parseInt(info[info.length - 1]);
        }
        try {
            NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(VOLT_DEVICE_USERNAME, VOLT_DEVICE_PASSWORD, IpAddress.valueOf(ip), port);
            NetconfDevice netconfDevice = new FujitsuNetconfDeviceMock(deviceInfo);
            netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
            return netconfDevice;
        } catch (NullPointerException e) {
            throw new NetconfException("Cannot connect a device " + deviceId, e);
        }
    }
}
Also used : NetconfException(org.onosproject.netconf.NetconfException) Arrays(java.util.Arrays) DefaultDriver(org.onosproject.net.driver.DefaultDriver) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NetconfDevice(org.onosproject.netconf.NetconfDevice) Set(java.util.Set) DefaultDriverData(org.onosproject.net.driver.DefaultDriverData) ArrayList(java.util.ArrayList) NetconfControllerImpl(org.onosproject.netconf.ctl.impl.NetconfControllerImpl) Map(java.util.Map) DeviceId(org.onosproject.net.DeviceId) NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) IpAddress(org.onlab.packet.IpAddress) NetconfException(org.onosproject.netconf.NetconfException) NetconfDevice(org.onosproject.netconf.NetconfDevice) NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo)

Example 4 with NetconfDeviceInfo

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

the class MockNetconfController method connectDevice.

@Override
public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
    NetconfDevice mockNetconfDevice = null;
    String[] nameParts = deviceId.uri().toASCIIString().split(":");
    IpAddress ipAddress = Ip4Address.valueOf(nameParts[1]);
    int port = Integer.parseInt(nameParts[2]);
    NetconfDeviceInfo ncdi = new NetconfDeviceInfo("mock", "mock", ipAddress, port, null);
    mockNetconfDevice = new MockNetconfDevice(ncdi);
    devicesMap.put(deviceId, mockNetconfDevice);
    return mockNetconfDevice;
}
Also used : NetconfDevice(org.onosproject.netconf.NetconfDevice) NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) IpAddress(org.onlab.packet.IpAddress)

Example 5 with NetconfDeviceInfo

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

the class NetconfControllerImpl method createDeviceInfo.

private NetconfDeviceInfo createDeviceInfo(DeviceId deviceId) throws NetconfException {
    Device device = deviceService.getDevice(deviceId);
    String ip, path = null;
    int port;
    if (device != null) {
        ip = device.annotations().value("ipaddress");
        port = Integer.parseInt(device.annotations().value("port"));
    } else {
        Triple<String, Integer, Optional<String>> info = extractIpPortPath(deviceId);
        ip = info.getLeft();
        port = info.getMiddle();
        path = (info.getRight().isPresent() ? info.getRight().get() : null);
    }
    try {
        DeviceKey deviceKey = deviceKeyService.getDeviceKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
        if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) {
            UsernamePassword usernamepasswd = deviceKey.asUsernamePassword();
            return new NetconfDeviceInfo(usernamepasswd.username(), usernamepasswd.password(), IpAddress.valueOf(ip), port, path);
        } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) {
            String username = deviceKey.annotations().value(AnnotationKeys.USERNAME);
            String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD);
            String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY);
            return new NetconfDeviceInfo(username, password, IpAddress.valueOf(ip), port, path, sshkey);
        } else {
            log.error("Unknown device key for device {}", deviceId);
            throw new NetconfException("Unknown device key for device " + deviceId);
        }
    } catch (NullPointerException e) {
        log.error("No Device Key for device {}, {}", deviceId, e);
        throw new NetconfException("No Device Key for device " + deviceId, e);
    }
}
Also used : NetconfException(org.onosproject.netconf.NetconfException) Optional(java.util.Optional) NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) Device(org.onosproject.net.Device) NetconfDevice(org.onosproject.netconf.NetconfDevice) DeviceKey(org.onosproject.net.key.DeviceKey) UsernamePassword(org.onosproject.net.key.UsernamePassword)

Aggregations

NetconfDeviceInfo (org.onosproject.netconf.NetconfDeviceInfo)8 NetconfDevice (org.onosproject.netconf.NetconfDevice)6 NetconfException (org.onosproject.netconf.NetconfException)4 DeviceId (org.onosproject.net.DeviceId)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Optional (java.util.Optional)2 IpAddress (org.onlab.packet.IpAddress)2 DefaultDriver (org.onosproject.net.driver.DefaultDriver)2 LoggerFactory (org.slf4j.LoggerFactory)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1