Search in sources :

Example 6 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)

Example 7 with NetconfDeviceInfo

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

the class NetconfSessionMinaImplTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    int portNumber = TestTools.findAvailablePort(50830);
    sshServerNetconf = SshServer.setUpDefaultServer();
    sshServerNetconf.setPasswordAuthenticator(new PasswordAuthenticator() {

        @Override
        public boolean authenticate(String username, String password, ServerSession session) {
            return TEST_USERNAME.equals(username) && TEST_PASSWORD.equals(password);
        }
    });
    TestUtils.setField(NetconfSessionMinaImpl.class, "directory", TEST_DIRECTORY);
    sshServerNetconf.setPort(portNumber);
    SimpleGeneratorHostKeyProvider provider = new SimpleGeneratorHostKeyProvider();
    provider.setFile(new File(TEST_SERFILE));
    sshServerNetconf.setKeyPairProvider(provider);
    sshServerNetconf.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new NetconfSshdTestSubsystem.Factory()));
    sshServerNetconf.open();
    log.info("SSH Server opened on port {}", portNumber);
    NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(TEST_USERNAME, TEST_PASSWORD, Ip4Address.valueOf(TEST_HOSTNAME), portNumber);
    deviceInfo.setConnectTimeoutSec(OptionalInt.of(30));
    deviceInfo.setReplyTimeoutSec(OptionalInt.of(30));
    session1 = new NetconfSessionMinaImpl(deviceInfo, ImmutableList.of("urn:ietf:params:netconf:base:1.0"));
    log.info("Started NETCONF Session {} with test SSHD server in Unit Test", session1.getSessionId());
    assertTrue("Incorrect sessionId", !session1.getSessionId().equalsIgnoreCase("-1"));
    assertTrue("Incorrect sessionId", !session1.getSessionId().equalsIgnoreCase("0"));
    assertThat(session1.getDeviceCapabilitiesSet(), containsInAnyOrder(DEFAULT_CAPABILITIES.toArray()));
    session2 = new NetconfSessionMinaImpl(deviceInfo, ImmutableList.of("urn:ietf:params:netconf:base:1.0"));
    log.info("Started NETCONF Session {} with test SSHD server in Unit Test", session2.getSessionId());
    assertTrue("Incorrect sessionId", !session2.getSessionId().equalsIgnoreCase("-1"));
    assertTrue("Incorrect sessionId", !session2.getSessionId().equalsIgnoreCase("0"));
    assertThat(session2.getDeviceCapabilitiesSet(), containsInAnyOrder(DEFAULT_CAPABILITIES.toArray()));
    session3 = new NetconfSessionMinaImpl(deviceInfo);
    log.info("Started NETCONF Session {} with test SSHD server in Unit Test", session3.getSessionId());
    assertTrue("Incorrect sessionId", !session3.getSessionId().equalsIgnoreCase("-1"));
    assertTrue("Incorrect sessionId", !session3.getSessionId().equalsIgnoreCase("0"));
    assertThat(session3.getDeviceCapabilitiesSet(), containsInAnyOrder(DEFAULT_CAPABILITIES_1_1.toArray()));
    session4 = new NetconfSessionMinaImpl(deviceInfo);
    log.info("Started NETCONF Session {} with test SSHD server in Unit Test", session4.getSessionId());
    assertTrue("Incorrect sessionId", !session4.getSessionId().equalsIgnoreCase("-1"));
    assertTrue("Incorrect sessionId", !session4.getSessionId().equalsIgnoreCase("0"));
    assertThat(session4.getDeviceCapabilitiesSet(), containsInAnyOrder(DEFAULT_CAPABILITIES_1_1.toArray()));
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ServerSession(org.apache.sshd.server.session.ServerSession) PasswordAuthenticator(org.apache.sshd.server.auth.password.PasswordAuthenticator) Command(org.apache.sshd.server.Command) NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) LoggerFactory(org.slf4j.LoggerFactory) NamedFactory(org.apache.sshd.common.NamedFactory) File(java.io.File) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) BeforeClass(org.junit.BeforeClass)

Example 8 with NetconfDeviceInfo

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

the class NetconfStreamThread method dealWithReply.

private void dealWithReply(String deviceReply) {
    if (deviceReply.contains(RPC_REPLY) || deviceReply.contains(RPC_ERROR) || deviceReply.contains(HELLO)) {
        log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}", netconfDeviceInfo, getMsgId(deviceReply), deviceReply);
        NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_REPLY, null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo);
        sessionDelegate.notify(event);
        netconfDeviceEventListeners.forEach(listener -> listener.event(event));
    } else if (deviceReply.contains(NOTIFICATION_LABEL)) {
        log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}", netconfDeviceInfo, enableNotifications, getMsgId(deviceReply), deviceReply);
        if (enableNotifications) {
            log.debug("dispatching to {} listeners", netconfDeviceEventListeners.size());
            final String finalDeviceReply = deviceReply;
            netconfDeviceEventListeners.forEach(listener -> listener.event(new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null, finalDeviceReply, getMsgId(finalDeviceReply), netconfDeviceInfo)));
        }
    } else {
        log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply);
    }
}
Also used : NetconfException(org.onosproject.netconf.NetconfException) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent) LoggerFactory(org.slf4j.LoggerFactory) MatchResult(java.util.regex.MatchResult) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) NetconfDeviceOutputEventListener(org.onosproject.netconf.NetconfDeviceOutputEventListener) ArrayList(java.util.ArrayList) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) List(java.util.List) Lists(com.google.common.collect.Lists) Matcher(java.util.regex.Matcher) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) Optional(java.util.Optional) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NetconfDeviceInfo(org.onosproject.netconf.NetconfDeviceInfo) InputStream(java.io.InputStream) NetconfDeviceOutputEvent(org.onosproject.netconf.NetconfDeviceOutputEvent)

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