Bug 3216 - Restconf GET operations functionality are not supported yet
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / RestconfImpl.java
index 5e9ab7b9d0a0b818a577ddbcec502f10e6196bca..c329f525a952d162eb96c5ba960dc002ecf3f9cc 100644 (file)
@@ -10,12 +10,14 @@ package org.opendaylight.controller.sal.restconf.impl;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
 import com.google.common.base.Throwables;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
@@ -24,6 +26,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
@@ -48,6 +51,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.controller.md.sal.rest.common.RestconfValidationUtils;
 import org.opendaylight.controller.sal.rest.api.Draft02;
 import org.opendaylight.controller.sal.rest.api.RestconfService;
 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
@@ -64,6 +68,7 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum
 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
@@ -87,7 +92,13 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.util.EmptyType;
 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
+import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -320,12 +331,67 @@ public class RestconfImpl implements RestconfService {
         return operationsFromModulesToNormalizedContext(modules, mountPoint);
     }
 
+    private static final Predicate<GroupingBuilder> GROUPING_FILTER = new Predicate<GroupingBuilder>() {
+        @Override
+        public boolean apply(final GroupingBuilder g) {
+            return Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName());
+        }
+    };
+
     private NormalizedNodeContext operationsFromModulesToNormalizedContext(final Set<Module> modules,
             final DOMMountPoint mountPoint) {
 
-        // FIXME find best way to change restconf-netconf yang schema for provide this functionality
-        final String errMsg = "We are not able support view operations functionality yet.";
-        throw new RestconfDocumentedException(errMsg, ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
+        final Module restconfModule = getRestconfModule();
+        final ModuleBuilder restConfModuleBuilder = new ModuleBuilder(restconfModule);
+        final Set<GroupingBuilder> gropingBuilders = restConfModuleBuilder.getGroupingBuilders();
+        final Iterable<GroupingBuilder> filteredGroups = Iterables.filter(gropingBuilders, GROUPING_FILTER);
+        final GroupingBuilder restconfGroupingBuilder = Iterables.getFirst(filteredGroups, null);
+        final ContainerSchemaNodeBuilder restContainerSchemaNodeBuilder = (ContainerSchemaNodeBuilder) restconfGroupingBuilder
+                .getDataChildByName(Draft02.RestConfModule.RESTCONF_CONTAINER_SCHEMA_NODE);
+        final ContainerSchemaNodeBuilder containerSchemaNodeBuilder = (ContainerSchemaNodeBuilder) restContainerSchemaNodeBuilder
+                .getDataChildByName(Draft02.RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE);
+
+        final ContainerSchemaNodeBuilder fakeOperationsSchemaNodeBuilder = containerSchemaNodeBuilder;
+        final SchemaPath fakeSchemaPath = fakeOperationsSchemaNodeBuilder.getPath().createChild(QName.create("dummy"));
+
+        final List<LeafNode<Object>> operationsAsData = new ArrayList<>();
+
+        for (final Module module : modules) {
+            final Set<RpcDefinition> rpcs = module.getRpcs();
+            for (final RpcDefinition rpc : rpcs) {
+                final QName rpcQName = rpc.getQName();
+                final String name = module.getName();
+
+                final QName qName = QName.create(restconfModule.getQNameModule(), rpcQName.getLocalName());
+                final LeafSchemaNodeBuilder leafSchemaNodeBuilder = new LeafSchemaNodeBuilder(name, 0, qName, fakeSchemaPath);
+                final LeafSchemaNodeBuilder fakeRpcSchemaNodeBuilder = leafSchemaNodeBuilder;
+                fakeRpcSchemaNodeBuilder.setAugmenting(true);
+
+                final EmptyType instance = EmptyType.getInstance();
+                fakeRpcSchemaNodeBuilder.setType(instance);
+                final LeafSchemaNode fakeRpcSchemaNode = fakeRpcSchemaNodeBuilder.build();
+                fakeOperationsSchemaNodeBuilder.addChildNode(fakeRpcSchemaNode);
+
+                final LeafNode<Object> leaf = Builders.leafBuilder(fakeRpcSchemaNode).build();
+                operationsAsData.add(leaf);
+            }
+        }
+
+        final ContainerSchemaNode operContainerSchemaNode = fakeOperationsSchemaNodeBuilder.build();
+        final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> operContainerNode = Builders.containerBuilder(operContainerSchemaNode);
+
+        for (final LeafNode<Object> oper : operationsAsData) {
+            operContainerNode.withChild(oper);
+        }
+
+        final Set<Module> fakeRpcModules = Collections.singleton(restConfModuleBuilder.build());
+
+        final YangParserImpl yangParser = new YangParserImpl();
+        final SchemaContext fakeSchemaCx = yangParser.resolveSchemaContext(fakeRpcModules);
+
+        final InstanceIdentifierContext<?> fakeIICx = new InstanceIdentifierContext<>(null, operContainerSchemaNode, mountPoint, fakeSchemaCx);
+
+        return new NormalizedNodeContext(fakeIICx, operContainerNode.build());
     }
 
     private Module getRestconfModule() {
@@ -671,7 +737,7 @@ public class RestconfImpl implements RestconfService {
 
         validateInput(iiWithData.getSchemaNode(), payload);
         validateTopLevelNodeName(payload, iiWithData.getInstanceIdentifier());
-        validateListKeysEqualityInPayloadAndUri(iiWithData, payload.getData());
+        validateListKeysEqualityInPayloadAndUri(payload);
 
         final DOMMountPoint mountPoint = iiWithData.getMountPoint();
         final YangInstanceIdentifier normalizedII = iiWithData.getInstanceIdentifier();
@@ -747,41 +813,38 @@ public class RestconfImpl implements RestconfService {
      *             if key values or key count in payload and URI isn't equal
      *
      */
-    private void validateListKeysEqualityInPayloadAndUri(final InstanceIdentifierContext<?> iiWithData,
-            final NormalizedNode<?, ?> payload) {
-        if (iiWithData.getSchemaNode() instanceof ListSchemaNode) {
-            final List<QName> keyDefinitions = ((ListSchemaNode) iiWithData.getSchemaNode()).getKeyDefinition();
-            final PathArgument lastPathArgument = iiWithData.getInstanceIdentifier().getLastPathArgument();
-            if (lastPathArgument instanceof NodeIdentifierWithPredicates) {
-                final Map<QName, Object> uriKeyValues = ((NodeIdentifierWithPredicates) lastPathArgument)
-                        .getKeyValues();
-                isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions);
+    private static void validateListKeysEqualityInPayloadAndUri(final NormalizedNodeContext payload) {
+        Preconditions.checkArgument(payload != null);
+        final InstanceIdentifierContext<?> iiWithData = payload.getInstanceIdentifierContext();
+        final PathArgument lastPathArgument = iiWithData.getInstanceIdentifier().getLastPathArgument();
+        final SchemaNode schemaNode = iiWithData.getSchemaNode();
+        final NormalizedNode<?, ?> data = payload.getData();
+        if (schemaNode instanceof ListSchemaNode) {
+            final List<QName> keyDefinitions = ((ListSchemaNode) schemaNode).getKeyDefinition();
+            if (lastPathArgument instanceof NodeIdentifierWithPredicates && data instanceof MapEntryNode) {
+                final Map<QName, Object> uriKeyValues = ((NodeIdentifierWithPredicates) lastPathArgument).getKeyValues();
+                isEqualUriAndPayloadKeyValues(uriKeyValues, (MapEntryNode) data, keyDefinitions);
             }
         }
     }
 
-    private void isEqualUriAndPayloadKeyValues(final Map<QName, Object> uriKeyValues, final NormalizedNode<?, ?> payload,
-            final List<QName> keyDefinitions) {
+    private static void isEqualUriAndPayloadKeyValues(final Map<QName, Object> uriKeyValues,
+            final MapEntryNode payload, final List<QName> keyDefinitions) {
+
+        final Map<QName, Object> mutableCopyUriKeyValues = Maps.newHashMap(uriKeyValues);
         for (final QName keyDefinition : keyDefinitions) {
-            final Object uriKeyValue = uriKeyValues.get(keyDefinition);
+            final Object uriKeyValue = mutableCopyUriKeyValues.remove(keyDefinition);
             // should be caught during parsing URI to InstanceIdentifier
-            if (uriKeyValue == null) {
-                final String errMsg = "Missing key " + keyDefinition + " in URI.";
-                throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
+            RestconfValidationUtils.checkDocumentedError(uriKeyValue != null, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
+                    "Missing key " + keyDefinition + " in URI.");
+
+            final Object dataKeyValue = payload.getIdentifier().getKeyValues().get(keyDefinition);
+
+            if ( ! uriKeyValue.equals(dataKeyValue)) {
+                final String errMsg = "The value '" + uriKeyValue + "' for key '" + keyDefinition.getLocalName() +
+                        "' specified in the URI doesn't match the value '" + dataKeyValue + "' specified in the message body. ";
+                throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
             }
-            // TODO thing about the possibility to fix
-//            final List<SimpleNode<?>> payloadKeyValues = payload.getSimpleNodesByName(keyDefinition.getLocalName());
-//            if (payloadKeyValues.isEmpty()) {
-//                final String errMsg = "Missing key " + keyDefinition.getLocalName() + " in the message body.";
-//                throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
-//            }
-//
-//            final Object payloadKeyValue = payloadKeyValues.iterator().next().getValue();
-//            if (!uriKeyValue.equals(payloadKeyValue)) {
-//                final String errMsg = "The value '" + uriKeyValue + "' for key '" + keyDefinition.getLocalName() +
-//                        "' specified in the URI doesn't match the value '" + payloadKeyValue + "' specified in the message body. ";
-//                throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
-//            }
         }
     }