Split out RFC8040-only constructs from yang-common
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfStreamsSubscriptionServiceImpl.java
index b69a6609dc4326129f163a51e8c3f0cd801652e1..9e31cfba3c9e088224dc7ed9ac80227d0f4b7cd8 100644 (file)
@@ -7,7 +7,8 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkState;
+
 import java.net.URI;
 import java.time.Instant;
 import java.time.format.DateTimeFormatter;
@@ -15,10 +16,7 @@ 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.ws.rs.Path;
@@ -26,24 +24,17 @@ import javax.ws.rs.core.UriInfo;
 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.legacy.NormalizedNodePayload;
 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.streams.Configuration;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
+import org.opendaylight.yangtools.yang.common.QName;
 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.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;
@@ -58,54 +49,43 @@ import org.slf4j.LoggerFactory;
 @Path("/")
 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
     private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
+    private static final QName LOCATION_QNAME =
+        QName.create("subscribe:to:notification", "2016-10-28", "location").intern();
+    private static final NodeIdentifier LOCATION_NODEID = NodeIdentifier.create(LOCATION_QNAME);
+    private static final QName NOTIFI_QNAME = QName.create(LOCATION_QNAME, "notifi").intern();
+    private static final YangInstanceIdentifier LOCATION_PATH =
+        YangInstanceIdentifier.create(NodeIdentifier.create(NOTIFI_QNAME), LOCATION_NODEID);
 
     private final SubscribeToStreamUtil streamUtils;
-
-    private HandlersHolder handlersHolder;
+    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, final Configuration configuration) {
-        this.handlersHolder = new HandlersHolder(domDataBrokerHandler, notificationServiceHandler,
-                transactionChainHandler, schemaHandler);
+    public RestconfStreamsSubscriptionServiceImpl(final DOMDataBroker dataBroker,
+            final DOMNotificationService notificationService, final SchemaContextHandler schemaHandler,
+            final Configuration configuration) {
+        handlersHolder = new HandlersHolder(dataBroker, notificationService, schemaHandler);
         streamUtils = configuration.isUseSSE() ? SubscribeToStreamUtil.serverSentEvents()
                 : SubscribeToStreamUtil.webSockets();
     }
 
     @Override
-    public synchronized void updateHandlers(final Object... handlers) {
-        for (final Object object : handlers) {
-            if (object instanceof HandlersHolder) {
-                handlersHolder = (HandlersHolder) object;
-            }
-        }
-    }
-
-    @Override
-    public NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
+    public NormalizedNodePayload subscribeToStream(final String identifier, final UriInfo uriInfo) {
         final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
 
         final URI response;
         if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCRIPTION)) {
-            response = streamUtils.subscribeToDataStream(identifier, uriInfo, notificationQueryParams,
-                    this.handlersHolder);
+            response = streamUtils.subscribeToDataStream(identifier, uriInfo, notificationQueryParams, handlersHolder);
         } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
-            response = streamUtils.subscribeToYangStream(identifier, uriInfo, notificationQueryParams,
-                    this.handlersHolder);
+            response = streamUtils.subscribeToYangStream(identifier, uriInfo, notificationQueryParams, handlersHolder);
         } else {
             final String msg = "Bad type of notification of sal-remote";
             LOG.warn(msg);
@@ -113,16 +93,8 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
         }
 
         // 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);
-
-        return new NormalizedNodeContext(iid, builder.build(), headers);
+        return NormalizedNodePayload.ofLocation(prepareIIDSubsStreamOutput(handlersHolder.getSchemaHandler()),
+            LOCATION_NODEID, response);
     }
 
     /**
@@ -132,67 +104,48 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
      * @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 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());
+        final Optional<Module> module = schemaHandler.get().findModule(NOTIFI_QNAME.getModule());
+        checkState(module.isPresent());
+        final DataSchemaNode notify = module.get().dataChildByName(NOTIFI_QNAME);
+        checkState(notify instanceof ContainerSchemaNode, "Unexpected non-container %s", notify);
+        final DataSchemaNode location = ((ContainerSchemaNode) notify).dataChildByName(LOCATION_QNAME);
+        checkState(location != null, "Missing location");
+
+        return new InstanceIdentifierContext<SchemaNode>(LOCATION_PATH, location, null, schemaHandler.get());
     }
 
     /**
      * Holder of all handlers for notifications.
      */
+    // FIXME: why do we even need this class?!
     public static final class HandlersHolder {
-
-        private final DOMDataBrokerHandler domDataBrokerHandler;
-        private final NotificationServiceHandler notificationServiceHandler;
-        private final TransactionChainHandler transactionChainHandler;
+        private final DOMDataBroker dataBroker;
+        private final DOMNotificationService notificationService;
         private final SchemaContextHandler schemaHandler;
 
-        private HandlersHolder(final DOMDataBrokerHandler domDataBrokerHandler,
-                final NotificationServiceHandler notificationServiceHandler,
-                final TransactionChainHandler transactionChainHandler, final SchemaContextHandler schemaHandler) {
-            this.domDataBrokerHandler = domDataBrokerHandler;
-            this.notificationServiceHandler = notificationServiceHandler;
-            this.transactionChainHandler = transactionChainHandler;
+        private HandlersHolder(final DOMDataBroker dataBroker, final DOMNotificationService notificationService,
+                final SchemaContextHandler schemaHandler) {
+            this.dataBroker = dataBroker;
+            this.notificationService = notificationService;
             this.schemaHandler = schemaHandler;
         }
 
         /**
-         * Get {@link DOMDataBrokerHandler}.
-         *
-         * @return the domDataBrokerHandler
-         */
-        public DOMDataBrokerHandler getDomDataBrokerHandler() {
-            return this.domDataBrokerHandler;
-        }
-
-        /**
-         * Get {@link NotificationServiceHandler}.
+         * Get {@link DOMDataBroker}.
          *
-         * @return the notificationServiceHandler
+         * @return the dataBroker
          */
-        public NotificationServiceHandler getNotificationServiceHandler() {
-            return this.notificationServiceHandler;
+        public DOMDataBroker getDataBroker() {
+            return dataBroker;
         }
 
         /**
-         * Get {@link TransactionChainHandler}.
+         * Get {@link DOMNotificationService}.
          *
-         * @return the transactionChainHandler
+         * @return the notificationService
          */
-        public TransactionChainHandler getTransactionChainHandler() {
-            return this.transactionChainHandler;
+        public DOMNotificationService getNotificationServiceHandler() {
+            return notificationService;
         }
 
         /**
@@ -201,7 +154,7 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
          * @return the schemaHandler
          */
         public SchemaContextHandler getSchemaHandler() {
-            return this.schemaHandler;
+            return schemaHandler;
         }
     }