Bump MRI upstreams
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfStreamsSubscriptionServiceImpl.java
index f320c115e7b2b97d51f5f4894c4f6f9bab28bf5e..2afc1b91763f969d47f2ae24c824a2cc6b67e2a7 100644 (file)
  */
 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
 
+import com.google.common.base.Preconditions;
 import java.net.URI;
 import java.time.Instant;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeFormatterBuilder;
+import java.time.format.DateTimeParseException;
+import java.time.temporal.ChronoField;
+import java.time.temporal.TemporalAccessor;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
-import javax.annotation.Nonnull;
 import javax.ws.rs.Path;
 import javax.ws.rs.core.UriInfo;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
-import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
+import org.opendaylight.mdsal.dom.api.DOMNotificationService;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
-import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
-import org.opendaylight.restconf.nb.rfc8040.rests.utils.SubscribeToStreamUtil;
-import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
+import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Implementation of {@link RestconfStreamsSubscriptionService}.
- *
  */
 @Path("/")
 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
-
     private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
 
-    private HandlersHolder handlersHolder;
+    private final SubscribeToStreamUtil streamUtils;
+    private final HandlersHolder handlersHolder;
 
     /**
      * Initialize holder of handlers with holders as parameters.
      *
-     * @param domDataBrokerHandler
-     *             handler of {@link DOMDataBroker}
-     * @param notificationServiceHandler
-     *             handler of {@link DOMNotificationService}
+     * @param dataBroker {@link DOMDataBroker}
+     * @param notificationService {@link DOMNotificationService}
      * @param schemaHandler
      *             handler of {@link SchemaContext}
      * @param transactionChainHandler
      *             handler of {@link DOMTransactionChain}
+     * @param configuration
+     *             configuration for restconf {@link Configuration}}
      */
-    public RestconfStreamsSubscriptionServiceImpl(final DOMDataBrokerHandler domDataBrokerHandler,
-            final NotificationServiceHandler notificationServiceHandler, final SchemaContextHandler schemaHandler,
-            final TransactionChainHandler transactionChainHandler) {
-        this.handlersHolder = new HandlersHolder(domDataBrokerHandler, notificationServiceHandler,
+    public RestconfStreamsSubscriptionServiceImpl(final DOMDataBroker dataBroker,
+            final DOMNotificationService notificationService, final SchemaContextHandler schemaHandler,
+            final TransactionChainHandler transactionChainHandler, final Configuration configuration) {
+        this.handlersHolder = new HandlersHolder(dataBroker, notificationService,
                 transactionChainHandler, schemaHandler);
-    }
-
-    @Override
-    public synchronized void updateHandlers(final Object... handlers) {
-        for (final Object object : handlers) {
-            if (object instanceof HandlersHolder) {
-                handlersHolder = (HandlersHolder) object;
-            }
-        }
+        streamUtils = configuration.isUseSSE() ? SubscribeToStreamUtil.serverSentEvents()
+                : SubscribeToStreamUtil.webSockets();
     }
 
     @Override
     public NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
         final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
 
-        URI response = null;
-        if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCR)) {
-            response = SubscribeToStreamUtil.notifiDataStream(identifier, uriInfo, notificationQueryParams,
+        final URI response;
+        if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCRIPTION)) {
+            response = streamUtils.subscribeToDataStream(identifier, uriInfo, notificationQueryParams,
                     this.handlersHolder);
         } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
-            response = SubscribeToStreamUtil.notifYangStream(identifier, uriInfo, notificationQueryParams,
+            response = streamUtils.subscribeToYangStream(identifier, uriInfo, notificationQueryParams,
                     this.handlersHolder);
+        } else {
+            final String msg = "Bad type of notification of sal-remote";
+            LOG.warn(msg);
+            throw new RestconfDocumentedException(msg);
         }
 
-        if (response != null) {
-            // prepare node with value of location
-            final InstanceIdentifierContext<?> iid =
-                    SubscribeToStreamUtil.prepareIIDSubsStreamOutput(this.handlersHolder.getSchemaHandler());
-            final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> builder =
-                    ImmutableLeafNodeBuilder.create().withValue(response.toString());
-            builder.withNodeIdentifier(
-                    NodeIdentifier.create(QName.create("subscribe:to:notification", "2016-10-28", "location")));
+        // prepare node with value of location
+        final InstanceIdentifierContext<?> iid = prepareIIDSubsStreamOutput(this.handlersHolder.getSchemaHandler());
+        final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> builder =
+                ImmutableLeafNodeBuilder.create().withValue(response.toString());
+        builder.withNodeIdentifier(NodeIdentifier.create(RestconfStreamsConstants.LOCATION_QNAME));
 
-            // prepare new header with location
-            final Map<String, Object> headers = new HashMap<>();
-            headers.put("Location", response);
+        // prepare new header with location
+        final Map<String, Object> headers = new HashMap<>();
+        headers.put("Location", response);
 
-            return new NormalizedNodeContext(iid, builder.build(), headers);
-        }
+        return new NormalizedNodeContext(iid, builder.build(), headers);
+    }
+
+    /**
+     * Prepare InstanceIdentifierContext for Location leaf.
+     *
+     * @param schemaHandler Schema context handler.
+     * @return InstanceIdentifier of Location leaf.
+     */
+    private static InstanceIdentifierContext<?> prepareIIDSubsStreamOutput(final SchemaContextHandler schemaHandler) {
+        final Optional<Module> module = schemaHandler.get()
+                .findModule(RestconfStreamsConstants.NOTIFI_QNAME.getModule());
+        Preconditions.checkState(module.isPresent());
+        final Optional<DataSchemaNode> notify = module.get()
+                .findDataChildByName(RestconfStreamsConstants.NOTIFI_QNAME);
+        Preconditions.checkState(notify.isPresent());
+        final Optional<DataSchemaNode> location = ((ContainerSchemaNode) notify.get())
+                .findDataChildByName(RestconfStreamsConstants.LOCATION_QNAME);
+        Preconditions.checkState(location.isPresent());
 
-        final String msg = "Bad type of notification of sal-remote";
-        LOG.warn(msg);
-        throw new RestconfDocumentedException(msg);
+        final List<PathArgument> path = new ArrayList<>();
+        path.add(NodeIdentifier.create(RestconfStreamsConstants.NOTIFI_QNAME));
+        path.add(NodeIdentifier.create(RestconfStreamsConstants.LOCATION_QNAME));
+        return new InstanceIdentifierContext<SchemaNode>(YangInstanceIdentifier.create(path), location.get(),
+                null, schemaHandler.get());
     }
 
     /**
      * Holder of all handlers for notifications.
      */
-    public final class HandlersHolder {
-
-        private final DOMDataBrokerHandler domDataBrokerHandler;
-        private final NotificationServiceHandler notificationServiceHandler;
+    // FIXME: why do we even need this class?!
+    public static final class HandlersHolder {
+        private final DOMDataBroker dataBroker;
+        private final DOMNotificationService notificationService;
         private final TransactionChainHandler transactionChainHandler;
         private final SchemaContextHandler schemaHandler;
 
-        private HandlersHolder(final DOMDataBrokerHandler domDataBrokerHandler,
-                final NotificationServiceHandler notificationServiceHandler,
+        private HandlersHolder(final DOMDataBroker dataBroker, final DOMNotificationService notificationService,
                 final TransactionChainHandler transactionChainHandler, final SchemaContextHandler schemaHandler) {
-            this.domDataBrokerHandler = domDataBrokerHandler;
-            this.notificationServiceHandler = notificationServiceHandler;
+            this.dataBroker = dataBroker;
+            this.notificationService = notificationService;
             this.transactionChainHandler = transactionChainHandler;
             this.schemaHandler = schemaHandler;
         }
 
         /**
-         * Get {@link DOMDataBrokerHandler}.
+         * Get {@link DOMDataBroker}.
          *
-         * @return the domDataBrokerHandler
+         * @return the dataBroker
          */
-        public DOMDataBrokerHandler getDomDataBrokerHandler() {
-            return this.domDataBrokerHandler;
+        public DOMDataBroker getDataBroker() {
+            return this.dataBroker;
         }
 
         /**
-         * Get {@link NotificationServiceHandler}.
+         * Get {@link DOMNotificationService}.
          *
-         * @return the notificationServiceHandler
+         * @return the notificationService
          */
-        public NotificationServiceHandler getNotificationServiceHandler() {
-            return this.notificationServiceHandler;
+        public DOMNotificationService getNotificationServiceHandler() {
+            return this.notificationService;
         }
 
         /**
@@ -170,18 +192,29 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
 
     /**
      * Parser and holder of query paramteres from uriInfo for notifications.
-     *
      */
     public static final class NotificationQueryParams {
+        private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
+                .appendValue(ChronoField.YEAR, 4).appendLiteral('-')
+                .appendValue(ChronoField.MONTH_OF_YEAR, 2).appendLiteral('-')
+                .appendValue(ChronoField.DAY_OF_MONTH, 2).appendLiteral('T')
+                .appendValue(ChronoField.HOUR_OF_DAY, 2).appendLiteral(':')
+                .appendValue(ChronoField.MINUTE_OF_HOUR, 2).appendLiteral(':')
+                .appendValue(ChronoField.SECOND_OF_MINUTE, 2)
+                .appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
+                .appendOffset("+HH:MM", "Z").toFormatter();
 
         private final Instant start;
         private final Instant stop;
         private final String filter;
+        private final boolean skipNotificationData;
 
-        private NotificationQueryParams(final Instant start, final Instant stop, final String filter) {
+        private NotificationQueryParams(final Instant start, final Instant stop, final String filter,
+                final boolean skipNotificationData) {
             this.start = start == null ? Instant.now() : start;
             this.stop = stop;
             this.filter = filter;
+            this.skipNotificationData = skipNotificationData;
         }
 
         static NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
@@ -191,13 +224,15 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
             boolean stopTimeUsed = false;
             String filter = null;
             boolean filterUsed = false;
+            boolean skipNotificationDataUsed = false;
+            boolean skipNotificationData = false;
 
             for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
                 switch (entry.getKey()) {
                     case "start-time":
                         if (!startTimeUsed) {
                             startTimeUsed = true;
-                            start = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
+                            start = parseDateFromQueryParam(entry);
                         } else {
                             throw new RestconfDocumentedException("Start-time parameter can be used only once.");
                         }
@@ -205,7 +240,7 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
                     case "stop-time":
                         if (!stopTimeUsed) {
                             stopTimeUsed = true;
-                            stop = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
+                            stop = parseDateFromQueryParam(entry);
                         } else {
                             throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
                         }
@@ -216,6 +251,15 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
                             filter = entry.getValue().iterator().next();
                         }
                         break;
+                    case "odl-skip-notification-data":
+                        if (!skipNotificationDataUsed) {
+                            skipNotificationDataUsed = true;
+                            skipNotificationData = Boolean.parseBoolean(entry.getValue().iterator().next());
+                        } else {
+                            throw new RestconfDocumentedException(
+                                    "Odl-skip-notification-data parameter can be used only once.");
+                        }
+                        break;
                     default:
                         throw new RestconfDocumentedException(
                                 "Bad parameter used with notifications: " + entry.getKey());
@@ -225,7 +269,27 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
                 throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
             }
 
-            return new NotificationQueryParams(start, stop, filter);
+            return new NotificationQueryParams(start, stop, filter, skipNotificationData);
+        }
+
+
+        /**
+         * Parse input of query parameters - start-time or stop-time - from {@link DateAndTime} format
+         * to {@link Instant} format.
+         *
+         * @param entry Start-time or stop-time as string in {@link DateAndTime} format.
+         * @return Parsed {@link Instant} by entry.
+         */
+        private static Instant parseDateFromQueryParam(final Entry<String, List<String>> entry) {
+            final DateAndTime event = new DateAndTime(entry.getValue().iterator().next());
+            final String value = event.getValue();
+            final TemporalAccessor accessor;
+            try {
+                accessor = FORMATTER.parse(value);
+            } catch (final DateTimeParseException e) {
+                throw new RestconfDocumentedException("Cannot parse of value in date: " + value, e);
+            }
+            return Instant.from(accessor);
         }
 
         /**
@@ -233,8 +297,7 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
          *
          * @return start-time
          */
-        @Nonnull
-        public Instant getStart() {
+        public @NonNull Instant getStart() {
             return start;
         }
 
@@ -255,6 +318,14 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
         public Optional<String> getFilter() {
             return Optional.ofNullable(filter);
         }
-    }
 
+        /**
+         * Check whether this query should notify changes without data.
+         *
+         * @return true if this query should notify about changes with  data
+         */
+        public boolean isSkipNotificationData() {
+            return skipNotificationData;
+        }
+    }
 }