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();
}
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);
}
}
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;
}
}
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);
}
Aggregations