Fix various warnings
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / restful / utils / CreateStreamUtil.java
index 261c4dba7ec2ddbe27dde6b6ff994d89bc6ceb4b..c358b42bda523e374bf7ddbfb56a327bb0a88087 100644 (file)
@@ -8,7 +8,10 @@
 package org.opendaylight.restconf.restful.utils;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.CheckedFuture;
+import java.util.ArrayList;
+import java.util.List;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
@@ -17,6 +20,7 @@ import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
+import org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter;
 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
 import org.opendaylight.restconf.common.references.SchemaContextRef;
 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
@@ -30,12 +34,15 @@ import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Util class for streams
+ * Util class for streams.
  *
  * <ul>
  * <li>create stream
@@ -53,10 +60,10 @@ public final class CreateStreamUtil {
     }
 
     /**
-     * Create stream with POST operation via RPC
+     * Create stream with POST operation via RPC.
      *
      * @param payload
-     *            - input of rpc - example in JSON:
+     *             input of rpc - example in JSON:
      *
      *            <pre>
      *            {@code
@@ -71,7 +78,7 @@ public final class CreateStreamUtil {
      *            </pre>
      *
      * @param refSchemaCtx
-     *            - reference to {@link SchemaContext} -
+     *             reference to {@link SchemaContext} -
      *            {@link SchemaContextRef}
      * @return {@link CheckedFuture} with {@link DOMRpcResult} - This mean
      *         output of RPC - example in JSON:
@@ -87,31 +94,36 @@ public final class CreateStreamUtil {
      *         </pre>
      *
      */
-    public static DOMRpcResult createStream(final NormalizedNodeContext payload,
+    public static DOMRpcResult createDataChangeNotifiStream(final NormalizedNodeContext payload,
             final SchemaContextRef refSchemaCtx) {
         final ContainerNode data = (ContainerNode) payload.getData();
         final QName qname = payload.getInstanceIdentifierContext().getSchemaNode().getQName();
         final YangInstanceIdentifier path = preparePath(data, qname);
-        final String streamName = prepareStream(path, refSchemaCtx.get(), data);
+        String streamName = prepareDataChangeNotifiStreamName(path, refSchemaCtx.get(), data);
 
         final QName outputQname = QName.create(qname, "output");
         final QName streamNameQname = QName.create(qname, "stream-name");
 
-        final ContainerNode output = ImmutableContainerNodeBuilder.create()
-                .withNodeIdentifier(new NodeIdentifier(outputQname))
-                .withChild(ImmutableNodes.leafNode(streamNameQname, streamName)).build();
         final NotificationOutputType outputType = prepareOutputType(data);
+        if (outputType.equals(NotificationOutputType.JSON)) {
+            streamName = streamName + "/JSON";
+        }
 
         if (!Notificator.existListenerFor(streamName)) {
             Notificator.createListener(path, streamName, outputType);
         }
 
+        final ContainerNode output =
+                ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(outputQname))
+                        .withChild(ImmutableNodes.leafNode(streamNameQname, streamName)).build();
         return new DefaultDOMRpcResult(output);
     }
 
     /**
+     * Prepare {@code NotificationOutputType}.
+     *
      * @param data
-     *            - data of notification
+     *             data of notification
      * @return output type fo notification
      */
     private static NotificationOutputType prepareOutputType(final ContainerNode data) {
@@ -119,7 +131,8 @@ public final class CreateStreamUtil {
         return outputType = outputType == null ? NotificationOutputType.XML : outputType;
     }
 
-    private static String prepareStream(final YangInstanceIdentifier path, final SchemaContext schemaContext,
+    private static String prepareDataChangeNotifiStreamName(final YangInstanceIdentifier path,
+                                                            final SchemaContext schemaContext,
             final ContainerNode data) {
         LogicalDatastoreType ds = parseEnum(data, LogicalDatastoreType.class,
                 RestconfStreamsConstants.DATASTORE_PARAM_NAME);
@@ -128,7 +141,8 @@ public final class CreateStreamUtil {
         DataChangeScope scope = parseEnum(data, DataChangeScope.class, RestconfStreamsConstants.SCOPE_PARAM_NAME);
         scope = scope == null ? RestconfStreamsConstants.DEFAULT_SCOPE : scope;
 
-        final String streamName = Notificator
+        final String streamName = RestconfStreamsConstants.DATA_SUBSCR + "/"
+                + Notificator
                 .createStreamNameFromUri(ParserIdentifier.stringFromYangInstanceIdentifier(path, schemaContext)
                 + RestconfStreamsConstants.DS_URI + ds + RestconfStreamsConstants.SCOPE_URI + scope);
         return streamName;
@@ -151,21 +165,68 @@ public final class CreateStreamUtil {
             return null;
         }
 
-        return StreamUtil.resolveEnum(clazz, (String) value);
+        return ResolveEnumUtil.resolveEnum(clazz, (String) value);
     }
 
-    private static YangInstanceIdentifier preparePath(final ContainerNode data, final QName qName) {
+    private static YangInstanceIdentifier preparePath(final ContainerNode data, final QName qualifiedName) {
         final Optional<DataContainerChild<? extends PathArgument, ?>> path = data
-                .getChild(new YangInstanceIdentifier.NodeIdentifier(QName.create(qName, "path")));
+                .getChild(new YangInstanceIdentifier.NodeIdentifier(QName.create(qualifiedName, "path")));
         Object pathValue = null;
         if (path.isPresent()) {
             pathValue = path.get().getValue();
         }
         if (!(pathValue instanceof YangInstanceIdentifier)) {
             final String errMsg = "Instance identifier was not normalized correctly ";
-            LOG.debug(errMsg + qName);
+            LOG.debug(errMsg + qualifiedName);
             throw new RestconfDocumentedException(errMsg, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED);
         }
         return (YangInstanceIdentifier) pathValue;
     }
+
+    /**
+     * Create stream with POST operation via RPC.
+     *
+     * @param notificatinoDefinition
+     *             input of RPC
+     * @param refSchemaCtx
+     *             schemaContext
+     * @param outputType
+     *              output type
+     * @return {@link DOMRpcResult}
+     */
+    public static List<NotificationListenerAdapter> createYangNotifiStream(
+            final NotificationDefinition notificatinoDefinition, final SchemaContextRef refSchemaCtx,
+            final String outputType) {
+        final List<SchemaPath> paths = new ArrayList<>();
+        final QName notificatinoDefinitionQName = notificatinoDefinition.getQName();
+        final Module module =
+                refSchemaCtx.findModuleByNamespaceAndRevision(notificatinoDefinitionQName.getModule().getNamespace(),
+                        notificatinoDefinitionQName.getModule().getRevision());
+        Preconditions.checkNotNull(module,
+                "Module for namespace " + notificatinoDefinitionQName.getModule().getNamespace() + " does not exist");
+        NotificationDefinition notifiDef = null;
+        for (final NotificationDefinition notification : module.getNotifications()) {
+            if (notification.getQName().equals(notificatinoDefinitionQName)) {
+                notifiDef = notification;
+                break;
+            }
+        }
+        final String moduleName = module.getName();
+        Preconditions.checkNotNull(notifiDef,
+                "Notification " + notificatinoDefinitionQName + "doesn't exist in module " + moduleName);
+        paths.add(notifiDef.getPath());
+        String streamName = RestconfStreamsConstants.CREATE_NOTIFICATION_STREAM + "/";
+        streamName = streamName + moduleName + ":" + notificatinoDefinitionQName.getLocalName();
+        if (outputType.equals("JSON")) {
+            streamName = streamName + "/JSON";
+        }
+
+        if (Notificator.existNotificationListenerFor(streamName)) {
+            final List<NotificationListenerAdapter> notificationListenerFor =
+                    Notificator.getNotificationListenerFor(streamName);
+            return SubscribeToStreamUtil.pickSpecificListenerByOutput(notificationListenerFor, outputType);
+        }
+
+        return Notificator.createNotificationListener(paths, streamName, outputType);
+    }
 }