Search in sources :

Example 1 with Tl1Device

use of org.onosproject.tl1.Tl1Device in project onos by opennetworkinglab.

the class LumentumWaveReadyDiscovery method logout.

// Unused but provided here for convenience.
private void logout() {
    DeviceId deviceId = handler().data().deviceId();
    Tl1Controller ctrl = checkNotNull(handler().get(Tl1Controller.class));
    Optional<Tl1Device> device = ctrl.getDevice(deviceId);
    if (!device.isPresent()) {
        return;
    }
    // Logout command
    Tl1Command logoutCmd = DefaultTl1Command.builder().withVerb(CANC).withModifier(USER).withAid(device.get().username()).withCtag(103).build();
    Future<String> logout = ctrl.sendMsg(deviceId, logoutCmd);
    try {
        String logoutResponse = logout.get(TIMEOUT, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        log.error("Lougout failed", e);
    }
}
Also used : Tl1Command(org.onosproject.tl1.Tl1Command) DefaultTl1Command(org.onosproject.tl1.DefaultTl1Command) Tl1Controller(org.onosproject.tl1.Tl1Controller) DeviceId(org.onosproject.net.DeviceId) Tl1Device(org.onosproject.tl1.Tl1Device) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with Tl1Device

use of org.onosproject.tl1.Tl1Device in project onos by opennetworkinglab.

the class DefaultTl1Controller method sendMsg.

@Override
public /**
 * This implementation returns an empty string on failure.
 */
CompletableFuture<String> sendMsg(DeviceId deviceId, Tl1Command msg) {
    log.debug("Sending TL1 message to device {}: {}", deviceId, msg);
    Tl1Device device = deviceMap.get(deviceId);
    if (device == null || !device.isConnected() || !mastershipService.isLocalMaster(deviceId)) {
        return CompletableFuture.completedFuture(StringUtils.EMPTY);
    }
    // Create and store completable future, complete it in the channel handler when we receive a response
    CompletableFuture<String> future = new CompletableFuture<>();
    Channel channel = device.channel();
    if (!msgMap.containsKey(channel)) {
        return CompletableFuture.completedFuture(StringUtils.EMPTY);
    }
    msgMap.get(channel).put(msg.ctag(), future);
    // Write message to channel
    channel.writeAndFlush(Unpooled.copiedBuffer(msg.toString(), CharsetUtil.UTF_8));
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) Tl1Device(org.onosproject.tl1.Tl1Device)

Example 3 with Tl1Device

use of org.onosproject.tl1.Tl1Device in project onos by opennetworkinglab.

the class DefaultTl1Controller method connectDevice.

@Override
public void connectDevice(DeviceId deviceId) {
    Tl1Device device = deviceMap.get(deviceId);
    if (device == null || device.isConnected()) {
        return;
    }
    Bootstrap b = new Bootstrap();
    b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(8192, DELIMITER));
            socketChannel.pipeline().addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
            // TODO
            // socketChannel.pipeline().addLast(new Tl1Decoder());
            socketChannel.pipeline().addLast(new Tl1InboundHandler());
        }
    }).remoteAddress(device.ip().toInetAddress(), device.port()).connect().addListener((ChannelFuture channelFuture) -> {
        if (channelFuture.isSuccess()) {
            msgMap.put(channelFuture.channel(), new ConcurrentHashMap<>());
            device.connect(channelFuture.channel());
            tl1Listeners.forEach(l -> executor.execute(() -> l.deviceConnected(deviceId)));
        }
    });
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) Bootstrap(io.netty.bootstrap.Bootstrap) StringDecoder(io.netty.handler.codec.string.StringDecoder) Tl1Device(org.onosproject.tl1.Tl1Device) ChannelInitializer(io.netty.channel.ChannelInitializer)

Example 4 with Tl1Device

use of org.onosproject.tl1.Tl1Device in project onos by opennetworkinglab.

the class LumentumWaveReadyDiscovery method discoverDeviceDetails.

@Override
public DeviceDescription discoverDeviceDetails() {
    DeviceId deviceId = handler().data().deviceId();
    Tl1Controller ctrl = checkNotNull(handler().get(Tl1Controller.class));
    // Something reasonable, unavailable by default
    DeviceDescription defaultDescription = new DefaultDeviceDescription(deviceId.uri(), Device.Type.OTN, LUMENTUM, WAVEREADY, SWVERSION, SERIAL, new ChassisId(), false, DefaultAnnotations.EMPTY);
    Optional<Tl1Device> device = ctrl.getDevice(deviceId);
    if (!device.isPresent()) {
        return defaultDescription;
    }
    // Login
    Tl1Command loginCmd = DefaultTl1Command.builder().withVerb(ACT).withModifier(USER).withAid(device.get().username()).withCtag(100).withParameters(device.get().password()).build();
    Future<String> login = ctrl.sendMsg(deviceId, loginCmd);
    try {
        String loginResponse = login.get(TIMEOUT, TimeUnit.MILLISECONDS);
        if (loginResponse.contains("Access denied")) {
            log.error("Access denied: {}", loginResponse);
            return defaultDescription;
        }
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        log.error("Login failed", e);
        return defaultDescription;
    }
    // Fetch device description
    Tl1Command ddCmd = DefaultTl1Command.builder().withVerb(RTRV).withModifier(NETYPE).withCtag(101).build();
    Future<String> dd = ctrl.sendMsg(deviceId, ddCmd);
    try {
        String ddResponse = dd.get(TIMEOUT, TimeUnit.MILLISECONDS);
        return new DefaultDeviceDescription(defaultDescription, true, extractAnnotations(ddResponse));
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        log.error("Device description not found", e);
        return defaultDescription;
    }
}
Also used : DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) DeviceDescription(org.onosproject.net.device.DeviceDescription) ChassisId(org.onlab.packet.ChassisId) Tl1Command(org.onosproject.tl1.Tl1Command) DefaultTl1Command(org.onosproject.tl1.DefaultTl1Command) DeviceId(org.onosproject.net.DeviceId) Tl1Controller(org.onosproject.tl1.Tl1Controller) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) Tl1Device(org.onosproject.tl1.Tl1Device) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with Tl1Device

use of org.onosproject.tl1.Tl1Device in project onos by opennetworkinglab.

the class Tl1DeviceProvider method isReachable.

// Assumes device is registered in TL1 controller.
@Override
public boolean isReachable(DeviceId deviceId) {
    try {
        // First check if device is already connected.
        // If not, try to open a socket.
        Tl1Device device = controller.getDevice(deviceId).get();
        if (device.isConnected()) {
            return true;
        }
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(device.ip().toInetAddress(), device.port()), REACHABILITY_TIMEOUT);
        socket.close();
        return true;
    } catch (NoSuchElementException | IOException | IllegalArgumentException e) {
        log.error("Cannot reach device {}", deviceId, e);
        return false;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Tl1Device(org.onosproject.tl1.Tl1Device) DefaultTl1Device(org.onosproject.tl1.DefaultTl1Device) IOException(java.io.IOException) Socket(java.net.Socket) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Tl1Device (org.onosproject.tl1.Tl1Device)6 SocketChannel (io.netty.channel.socket.SocketChannel)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 Channel (io.netty.channel.Channel)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 DeviceId (org.onosproject.net.DeviceId)2 DefaultTl1Command (org.onosproject.tl1.DefaultTl1Command)2 Tl1Command (org.onosproject.tl1.Tl1Command)2 Tl1Controller (org.onosproject.tl1.Tl1Controller)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)1 StringDecoder (io.netty.handler.codec.string.StringDecoder)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 NoSuchElementException (java.util.NoSuchElementException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1