Search in sources :

Example 1 with NotificationListenerAdapter

use of org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter in project netconf by opendaylight.

the class RestconfImpl method notifStream.

/**
 * Register notification listener by stream name.
 *
 * @param identifier
 *            stream name
 * @param uriInfo
 *            uriInfo
 * @param stop
 *            stop-time of getting notification
 * @param start
 *            start-time of getting notification
 * @param filter
 *            indicate which subset of all possible events are of interest
 * @return {@link URI} of location
 */
private URI notifStream(final String identifier, final UriInfo uriInfo, final Instant start, final Instant stop, final String filter) {
    final String streamName = Notificator.createStreamNameFromUri(identifier);
    if (Strings.isNullOrEmpty(streamName)) {
        throw new RestconfDocumentedException("Stream name is empty.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    final List<NotificationListenerAdapter> listeners = Notificator.getNotificationListenerFor(streamName);
    if (listeners == null || listeners.isEmpty()) {
        throw new RestconfDocumentedException("Stream was not found.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
    }
    for (final NotificationListenerAdapter listener : listeners) {
        broker.registerToListenNotification(listener);
        listener.setQueryParams(start, Optional.ofNullable(stop), Optional.ofNullable(filter), false, false);
    }
    final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
    final WebSocketServer webSocketServerInstance = WebSocketServer.getInstance(NOTIFICATION_PORT);
    final int notificationPort = webSocketServerInstance.getPort();
    final UriBuilder uriToWebsocketServerBuilder = uriBuilder.port(notificationPort).scheme(getWsScheme(uriInfo));
    return uriToWebsocketServerBuilder.replacePath(streamName).build();
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) NotificationListenerAdapter(org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter) WebSocketServer(org.opendaylight.netconf.sal.streams.websockets.WebSocketServer) UriBuilder(javax.ws.rs.core.UriBuilder) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint)

Example 2 with NotificationListenerAdapter

use of org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter in project netconf by opendaylight.

the class WebSocketServerHandler method handleHttpRequest.

/**
 * Checks if HTTP request method is GET and if is possible to decode HTTP result of request.
 *
 * @param ctx ChannelHandlerContext
 * @param req FullHttpRequest
 */
private void handleHttpRequest(final ChannelHandlerContext ctx, final FullHttpRequest req) {
    // Handle a bad request.
    if (!req.decoderResult().isSuccess()) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;
    }
    // Allow only GET methods.
    if (req.method() != GET) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return;
    }
    final String streamName = Notificator.createStreamNameFromUri(req.uri());
    if (streamName.contains(RestconfImpl.DATA_SUBSCR)) {
        final ListenerAdapter listener = Notificator.getListenerFor(streamName);
        if (listener != null) {
            listener.addSubscriber(ctx.channel());
            LOG.debug("Subscriber successfully registered.");
        } else {
            LOG.error("Listener for stream with name '{}' was not found.", streamName);
            sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR));
        }
    } else if (streamName.contains(RestconfImpl.NOTIFICATION_STREAM)) {
        final List<NotificationListenerAdapter> listeners = Notificator.getNotificationListenerFor(streamName);
        if (listeners != null && !listeners.isEmpty()) {
            for (final NotificationListenerAdapter listener : listeners) {
                listener.addSubscriber(ctx.channel());
                LOG.debug("Subscriber successfully registered.");
            }
        } else {
            LOG.error("Listener for stream with name '{}' was not found.", streamName);
            sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR));
        }
    }
    // Handshake
    final WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, false);
    this.handshaker = wsFactory.newHandshaker(req);
    if (this.handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    } else {
        this.handshaker.handshake(ctx.channel(), req);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ListenerAdapter(org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter) NotificationListenerAdapter(org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter) NotificationListenerAdapter(org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter) List(java.util.List) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)

Example 3 with NotificationListenerAdapter

use of org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter in project netconf by opendaylight.

the class WebSocketServerHandler method handleWebSocketFrame.

/**
 * Handles web socket frame.
 *
 * @param ctx {@link ChannelHandlerContext}
 * @param frame {@link WebSocketFrame}
 */
private void handleWebSocketFrame(final ChannelHandlerContext ctx, final WebSocketFrame frame) {
    if (frame instanceof CloseWebSocketFrame) {
        this.handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
        final String streamName = Notificator.createStreamNameFromUri(((CloseWebSocketFrame) frame).reasonText());
        if (streamName.contains(RestconfImpl.DATA_SUBSCR)) {
            final ListenerAdapter listener = Notificator.getListenerFor(streamName);
            if (listener != null) {
                listener.removeSubscriber(ctx.channel());
                LOG.debug("Subscriber successfully registered.");
                Notificator.removeListenerIfNoSubscriberExists(listener);
            }
        } else if (streamName.contains(RestconfImpl.NOTIFICATION_STREAM)) {
            final List<NotificationListenerAdapter> listeners = Notificator.getNotificationListenerFor(streamName);
            if (listeners != null && !listeners.isEmpty()) {
                for (final NotificationListenerAdapter listener : listeners) {
                    listener.removeSubscriber(ctx.channel());
                }
            }
        }
        return;
    } else if (frame instanceof PingWebSocketFrame) {
        ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content().retain()));
        return;
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) ListenerAdapter(org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter) NotificationListenerAdapter(org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) NotificationListenerAdapter(org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter) List(java.util.List) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame)

Example 4 with NotificationListenerAdapter

use of org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter in project netconf by opendaylight.

the class BrokerFacadeTest method testRegisterToListenNotificationChanges.

/**
 * Create, register, close and remove notification listener.
 */
@Test
public void testRegisterToListenNotificationChanges() throws Exception {
    // create test notification listener
    final String identifier = "create-notification-stream/toaster:toastDone";
    final SchemaPath path = SchemaPath.create(true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
    Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML", controllerContext);
    final NotificationListenerAdapter listener = Notificator.getNotificationListenerFor(identifier).get(0);
    // mock registration
    final ListenerRegistration<NotificationListenerAdapter> registration = mock(ListenerRegistration.class);
    when(this.domNotification.registerNotificationListener(listener, Absolute.of(ImmutableList.copyOf(listener.getSchemaPath().getPathFromRoot())))).thenReturn(registration);
    // test to register listener for the first time
    this.brokerFacade.registerToListenNotification(listener);
    assertEquals("Registration was not successful", true, listener.isListening());
    // try to register for the second time
    this.brokerFacade.registerToListenNotification(listener);
    assertEquals("Registration was not successful", true, listener.isListening());
    // registrations should be invoked only once
    verify(this.domNotification, times(1)).registerNotificationListener(listener, Absolute.of(ImmutableList.copyOf(listener.getSchemaPath().getPathFromRoot())));
    final DOMTransactionChain transactionChain = mock(DOMTransactionChain.class);
    final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
    // close and remove test notification listener
    listener.close();
    Notificator.removeListenerIfNoSubscriberExists(listener);
}
Also used : NotificationListenerAdapter(org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) DOMDataTreeWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction) DOMTransactionChain(org.opendaylight.mdsal.dom.api.DOMTransactionChain) Test(org.junit.Test)

Aggregations

NotificationListenerAdapter (org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter)4 List (java.util.List)2 ListenerAdapter (org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter)2 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)1 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)1 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)1 WebSocketServerHandshakerFactory (io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 Test (org.junit.Test)1 DOMDataTreeWriteTransaction (org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction)1 DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)1 DOMTransactionChain (org.opendaylight.mdsal.dom.api.DOMTransactionChain)1 WebSocketServer (org.opendaylight.netconf.sal.streams.websockets.WebSocketServer)1 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)1 SchemaPath (org.opendaylight.yangtools.yang.model.api.SchemaPath)1