Merge "BUG-1281: share filters between instances"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / ControllerContext.java
index 0a67c84d8b2b04c56a31fa2d7e9dd2e3e8821934..695f9f82af486cc2ed742d9ff55c54d9259bbca8 100644 (file)
@@ -15,11 +15,9 @@ import com.google.common.base.Predicate;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
 import com.google.common.collect.BiMap;
-import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashBiMap;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
-
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URLDecoder;
@@ -28,28 +26,25 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
-
 import javax.ws.rs.core.Response.Status;
-
-import org.opendaylight.controller.sal.core.api.mount.MountInstance;
-import org.opendaylight.controller.sal.core.api.mount.MountService;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.controller.sal.rest.api.Draft02;
 import org.opendaylight.controller.sal.rest.impl.RestUtil;
 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
 import org.opendaylight.yangtools.concepts.Codec;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdentifierBuilder;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
@@ -94,13 +89,13 @@ public class ControllerContext implements SchemaContextListener {
             new AtomicReference<>(Collections.<QName, RpcDefinition>emptyMap());
 
     private volatile SchemaContext globalSchema;
-    private volatile MountService mountService;
+    private volatile DOMMountPointService mountService;
 
     public void setGlobalSchema(final SchemaContext globalSchema) {
         this.globalSchema = globalSchema;
     }
 
-    public void setMountService(final MountService mountService) {
+    public void setMountService(final DOMMountPointService mountService) {
         this.mountService = mountService;
     }
 
@@ -145,8 +140,8 @@ public class ControllerContext implements SchemaContextListener {
                     ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
 
-        InstanceIdentifierBuilder builder = InstanceIdentifier.builder();
-        Module latestModule = this.getLatestModule(globalSchema, startModule);
+        InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
+        Module latestModule = globalSchema.findModuleByName(startModule, null);
         InstanceIdWithSchemaNode iiWithSchemaNode = this.collectPathArguments(builder, pathArgs, latestModule, null,
                 toMountPointIdentifier);
 
@@ -178,59 +173,38 @@ public class ControllerContext implements SchemaContextListener {
 
         return list;
     }
-
-    private Module getLatestModule(final SchemaContext schema, final String moduleName) {
-        Preconditions.checkArgument(schema != null);
-        Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
-
-        Predicate<Module> filter = new Predicate<Module>() {
-            @Override
-            public boolean apply(final Module m) {
-                return Objects.equal(m.getName(), moduleName);
-            }
-        };
-
-        Iterable<Module> modules = Iterables.filter(schema.getModules(), filter);
-        return this.filterLatestModule(modules);
-    }
-
-    private Module filterLatestModule(final Iterable<Module> modules) {
-        Module latestModule = modules.iterator().hasNext() ? modules.iterator().next() : null;
-        for (final Module module : modules) {
-            if (module.getRevision().after(latestModule.getRevision())) {
-                latestModule = module;
-            }
-        }
-        return latestModule;
-    }
-
     public Module findModuleByName(final String moduleName) {
         this.checkPreconditions();
         Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
-        return this.getLatestModule(globalSchema, moduleName);
+        return globalSchema.findModuleByName(moduleName, null);
     }
 
-    public Module findModuleByName(final MountInstance mountPoint, final String moduleName) {
+    public Module findModuleByName(final DOMMountPoint mountPoint, final String moduleName) {
         Preconditions.checkArgument(moduleName != null && mountPoint != null);
 
         final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
-        return mountPointSchema == null ? null : this.getLatestModule(mountPointSchema, moduleName);
+        if (mountPointSchema == null) {
+            return null;
+        }
+
+        return mountPointSchema.findModuleByName(moduleName, null);
     }
 
     public Module findModuleByNamespace(final URI namespace) {
         this.checkPreconditions();
         Preconditions.checkArgument(namespace != null);
-
-        final Set<Module> moduleSchemas = globalSchema.findModuleByNamespace(namespace);
-        return moduleSchemas == null ? null : this.filterLatestModule(moduleSchemas);
+        return globalSchema.findModuleByNamespaceAndRevision(namespace, null);
     }
 
-    public Module findModuleByNamespace(final MountInstance mountPoint, final URI namespace) {
+    public Module findModuleByNamespace(final DOMMountPoint mountPoint, final URI namespace) {
         Preconditions.checkArgument(namespace != null && mountPoint != null);
 
         final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
-        Set<Module> moduleSchemas = mountPointSchema == null ? null : mountPointSchema.findModuleByNamespace(namespace);
-        return moduleSchemas == null ? null : this.filterLatestModule(moduleSchemas);
+        if (mountPointSchema == null) {
+            return null;
+        }
+
+        return mountPointSchema.findModuleByNamespaceAndRevision(namespace, null);
     }
 
     public Module findModuleByNameAndRevision(final QName module) {
@@ -240,7 +214,7 @@ public class ControllerContext implements SchemaContextListener {
         return globalSchema.findModuleByName(module.getLocalName(), module.getRevision());
     }
 
-    public Module findModuleByNameAndRevision(final MountInstance mountPoint, final QName module) {
+    public Module findModuleByNameAndRevision(final DOMMountPoint mountPoint, final QName module) {
         this.checkPreconditions();
         Preconditions.checkArgument(module != null && module.getLocalName() != null && module.getRevision() != null
                 && mountPoint != null);
@@ -250,7 +224,7 @@ public class ControllerContext implements SchemaContextListener {
                 module.getRevision());
     }
 
-    public DataNodeContainer getDataNodeContainerFor(final InstanceIdentifier path) {
+    public DataNodeContainer getDataNodeContainerFor(final YangInstanceIdentifier path) {
         this.checkPreconditions();
 
         final Iterable<PathArgument> elements = path.getPathArguments();
@@ -262,7 +236,7 @@ public class ControllerContext implements SchemaContextListener {
         for (final PathArgument element : elements) {
             QName _nodeType = element.getNodeType();
             final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
-            if (potentialNode == null || !this.isListOrContainer(potentialNode)) {
+            if (potentialNode == null || !ControllerContext.isListOrContainer(potentialNode)) {
                 return null;
             }
             node = (DataNodeContainer) potentialNode;
@@ -271,7 +245,7 @@ public class ControllerContext implements SchemaContextListener {
         return node;
     }
 
-    public String toFullRestconfIdentifier(final InstanceIdentifier path) {
+    public String toFullRestconfIdentifier(final YangInstanceIdentifier path) {
         this.checkPreconditions();
 
         final Iterable<PathArgument> elements = path.getPathArguments();
@@ -284,7 +258,7 @@ public class ControllerContext implements SchemaContextListener {
         for (final PathArgument element : elements) {
             QName _nodeType = element.getNodeType();
             final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
-            if (!this.isListOrContainer(potentialNode)) {
+            if (!ControllerContext.isListOrContainer(potentialNode)) {
                 return null;
             }
             node = ((DataNodeContainer) potentialNode);
@@ -309,7 +283,7 @@ public class ControllerContext implements SchemaContextListener {
         return moduleName;
     }
 
-    public String findModuleNameByNamespace(final MountInstance mountPoint, final URI namespace) {
+    public String findModuleNameByNamespace(final DOMMountPoint mountPoint, final URI namespace) {
         final Module module = this.findModuleByNamespace(mountPoint, namespace);
         return module == null ? null : module.getName();
     }
@@ -327,12 +301,12 @@ public class ControllerContext implements SchemaContextListener {
         return namespace;
     }
 
-    public URI findNamespaceByModuleName(final MountInstance mountPoint, final String moduleName) {
+    public URI findNamespaceByModuleName(final DOMMountPoint mountPoint, final String moduleName) {
         final Module module = this.findModuleByName(mountPoint, moduleName);
         return module == null ? null : module.getNamespace();
     }
 
-    public Set<Module> getAllModules(final MountInstance mountPoint) {
+    public Set<Module> getAllModules(final DOMMountPoint mountPoint) {
         this.checkPreconditions();
 
         SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
@@ -366,7 +340,7 @@ public class ControllerContext implements SchemaContextListener {
         return builder.toString();
     }
 
-    public CharSequence toRestconfIdentifier(final MountInstance mountPoint, final QName qname) {
+    public CharSequence toRestconfIdentifier(final DOMMountPoint mountPoint, final QName qname) {
         if (mountPoint == null) {
             return null;
         }
@@ -390,6 +364,13 @@ public class ControllerContext implements SchemaContextListener {
         return findModuleByNameAndRevision(Draft02.RestConfModule.IETF_RESTCONF_QNAME);
     }
 
+    private static final Predicate<GroupingDefinition> ERRORS_GROUPING_FILTER = new Predicate<GroupingDefinition>() {
+        @Override
+        public boolean apply(final GroupingDefinition g) {
+            return Draft02.RestConfModule.ERRORS_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName());
+        }
+    };
+
     public DataSchemaNode getRestconfModuleErrorsSchemaNode() {
         Module restconfModule = getRestconfModule();
         if (restconfModule == null) {
@@ -398,14 +379,7 @@ public class ControllerContext implements SchemaContextListener {
 
         Set<GroupingDefinition> groupings = restconfModule.getGroupings();
 
-        final Predicate<GroupingDefinition> filter = new Predicate<GroupingDefinition>() {
-            @Override
-            public boolean apply(final GroupingDefinition g) {
-                return Objects.equal(g.getQName().getLocalName(), Draft02.RestConfModule.ERRORS_GROUPING_SCHEMA_NODE);
-            }
-        };
-
-        Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, filter);
+        Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, ERRORS_GROUPING_FILTER);
 
         final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
 
@@ -414,6 +388,13 @@ public class ControllerContext implements SchemaContextListener {
         return Iterables.getFirst(instanceDataChildrenByName, null);
     }
 
+    private static final Predicate<GroupingDefinition> GROUPING_FILTER = new Predicate<GroupingDefinition>() {
+        @Override
+        public boolean apply(final GroupingDefinition g) {
+            return Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName());
+        }
+    };
+
     public DataSchemaNode getRestconfModuleRestConfSchemaNode(final Module inRestconfModule, final String schemaNodeName) {
         Module restconfModule = inRestconfModule;
         if (restconfModule == null) {
@@ -425,16 +406,7 @@ public class ControllerContext implements SchemaContextListener {
         }
 
         Set<GroupingDefinition> groupings = restconfModule.getGroupings();
-
-        final Predicate<GroupingDefinition> filter = new Predicate<GroupingDefinition>() {
-            @Override
-            public boolean apply(final GroupingDefinition g) {
-                return Objects.equal(g.getQName().getLocalName(), Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE);
-            }
-        };
-
-        Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, filter);
-
+        Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, GROUPING_FILTER);
         final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
 
         List<DataSchemaNode> instanceDataChildrenByName = this.findInstanceDataChildrenByName(restconfGrouping,
@@ -511,9 +483,9 @@ public class ControllerContext implements SchemaContextListener {
         DataSchemaNode ret = container.getDataChildByName(name);
         if (ret == null) {
             for (final DataSchemaNode node : container.getChildNodes()) {
-                if ((node instanceof ChoiceCaseNode)) {
-                    final ChoiceCaseNode caseNode = ((ChoiceCaseNode) node);
-                    DataSchemaNode childByQName = ControllerContext.childByQName(caseNode, name);
+                if ((node instanceof ChoiceNode)) {
+                    final ChoiceNode choiceNode = ((ChoiceNode) node);
+                    DataSchemaNode childByQName = ControllerContext.childByQName(choiceNode, name);
                     if (childByQName != null) {
                         return childByQName;
                     }
@@ -528,7 +500,7 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     private InstanceIdWithSchemaNode collectPathArguments(final InstanceIdentifierBuilder builder,
-            final List<String> strings, final DataNodeContainer parentNode, final MountInstance mountPoint,
+            final List<String> strings, final DataNodeContainer parentNode, final DOMMountPoint mountPoint,
             final boolean returnJustMountPoint) {
         Preconditions.<List<String>> checkNotNull(strings);
 
@@ -559,13 +531,14 @@ public class ControllerContext implements SchemaContextListener {
                             ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
                 }
 
-                final InstanceIdentifier partialPath = builder.toInstance();
-                final MountInstance mount = mountService.getMountPoint(partialPath);
-                if (mount == null) {
+                final YangInstanceIdentifier partialPath = builder.toInstance();
+                final Optional<DOMMountPoint> mountOpt = mountService.getMountPoint(partialPath);
+                if (!mountOpt.isPresent()) {
                     LOG.debug("Instance identifier to missing mount point: {}", partialPath);
                     throw new RestconfDocumentedException("Mount point does not exist.", ErrorType.PROTOCOL,
                             ErrorTag.UNKNOWN_ELEMENT);
                 }
+                DOMMountPoint mount = mountOpt.get();
 
                 final SchemaContext mountPointSchema = mount.getSchemaContext();
                 if (mountPointSchema == null) {
@@ -574,12 +547,12 @@ public class ControllerContext implements SchemaContextListener {
                 }
 
                 if (returnJustMountPoint) {
-                    InstanceIdentifier instance = InstanceIdentifier.builder().toInstance();
+                    YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance();
                     return new InstanceIdWithSchemaNode(instance, mountPointSchema, mount);
                 }
 
                 if (strings.size() == 1) {
-                    InstanceIdentifier instance = InstanceIdentifier.builder().toInstance();
+                    YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance();
                     return new InstanceIdWithSchemaNode(instance, mountPointSchema, mount);
                 }
 
@@ -590,28 +563,31 @@ public class ControllerContext implements SchemaContextListener {
                             ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
                 }
 
-                final Module moduleBehindMountPoint = this
-                        .getLatestModule(mountPointSchema, moduleNameBehindMountPoint);
+                final Module moduleBehindMountPoint = mountPointSchema.findModuleByName(moduleNameBehindMountPoint, null);
                 if (moduleBehindMountPoint == null) {
                     throw new RestconfDocumentedException("\"" + moduleName
                             + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
                 }
 
                 List<String> subList = strings.subList(1, strings.size());
-                return this.collectPathArguments(InstanceIdentifier.builder(), subList, moduleBehindMountPoint, mount,
+                return this.collectPathArguments(YangInstanceIdentifier.builder(), subList, moduleBehindMountPoint, mount,
                         returnJustMountPoint);
             }
 
             Module module = null;
             if (mountPoint == null) {
-                module = this.getLatestModule(globalSchema, moduleName);
+                module = globalSchema.findModuleByName(moduleName, null);
                 if (module == null) {
                     throw new RestconfDocumentedException("\"" + moduleName + "\" module does not exist.",
                             ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
                 }
             } else {
                 SchemaContext schemaContext = mountPoint.getSchemaContext();
-                module = schemaContext == null ? null : this.getLatestModule(schemaContext, moduleName);
+                if (schemaContext != null) {
+                    module = schemaContext.findModuleByName(moduleName, null);
+                } else {
+                    module = null;
+                }
                 if (module == null) {
                     throw new RestconfDocumentedException("\"" + moduleName
                             + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
@@ -650,7 +626,7 @@ public class ControllerContext implements SchemaContextListener {
             targetNode = potentialSchemaNodes.iterator().next();
         }
 
-        if (!this.isListOrContainer(targetNode)) {
+        if (!ControllerContext.isListOrContainer(targetNode)) {
             throw new RestconfDocumentedException("URI has bad format. Node \"" + head
                     + "\" must be Container or List yang type.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
@@ -722,6 +698,13 @@ public class ControllerContext implements SchemaContextListener {
         return instantiatedDataNodeContainers;
     }
 
+    private static final Function<ChoiceNode, Set<ChoiceCaseNode>> CHOICE_FUNCTION = new Function<ChoiceNode, Set<ChoiceCaseNode>>() {
+        @Override
+        public Set<ChoiceCaseNode> apply(final ChoiceNode node) {
+            return node.getCases();
+        }
+    };
+
     private void collectInstanceDataNodeContainers(final List<DataSchemaNode> potentialSchemaNodes,
             final DataNodeContainer container, final String name) {
 
@@ -742,17 +725,8 @@ public class ControllerContext implements SchemaContextListener {
             }
         }
 
-        Iterable<ChoiceNode> choiceNodes = Iterables.<ChoiceNode> filter(container.getChildNodes(), ChoiceNode.class);
-
-        final Function<ChoiceNode, Set<ChoiceCaseNode>> choiceFunction = new Function<ChoiceNode, Set<ChoiceCaseNode>>() {
-            @Override
-            public Set<ChoiceCaseNode> apply(final ChoiceNode node) {
-                return node.getCases();
-            }
-        };
-
-        Iterable<Set<ChoiceCaseNode>> map = Iterables.<ChoiceNode, Set<ChoiceCaseNode>> transform(choiceNodes,
-                choiceFunction);
+        Iterable<ChoiceNode> choiceNodes = Iterables.filter(container.getChildNodes(), ChoiceNode.class);
+        Iterable<Set<ChoiceCaseNode>> map = Iterables.transform(choiceNodes, CHOICE_FUNCTION);
 
         final Iterable<ChoiceCaseNode> allCases = Iterables.<ChoiceCaseNode> concat(map);
         for (final ChoiceCaseNode caze : allCases) {
@@ -767,7 +741,7 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     private void addKeyValue(final HashMap<QName, Object> map, final DataSchemaNode node, final String uriValue,
-            final MountInstance mountPoint) {
+            final DOMMountPoint mountPoint) {
         Preconditions.<String> checkNotNull(uriValue);
         Preconditions.checkArgument((node instanceof LeafSchemaNode));
 
@@ -824,34 +798,8 @@ public class ControllerContext implements SchemaContextListener {
     private QName toQName(final String name) {
         final String module = toModuleName(name);
         final String node = toNodeName(name);
-        Set<Module> modules = globalSchema.getModules();
-
-        final Comparator<Module> comparator = new Comparator<Module>() {
-            @Override
-            public int compare(final Module o1, final Module o2) {
-                return o1.getRevision().compareTo(o2.getRevision());
-            }
-        };
-
-        List<Module> sorted = new ArrayList<Module>(modules);
-        Collections.<Module> sort(new ArrayList<Module>(modules), comparator);
-
-        final Function<Module, QName> transform = new Function<Module, QName>() {
-            @Override
-            public QName apply(final Module m) {
-                return QName.create(m.getNamespace(), m.getRevision(), m.getName());
-            }
-        };
-
-        final Predicate<QName> findFirst = new Predicate<QName>() {
-            @Override
-            public boolean apply(final QName qn) {
-                return Objects.equal(module, qn.getLocalName());
-            }
-        };
-
-        Optional<QName> namespace = FluentIterable.from(sorted).transform(transform).firstMatch(findFirst);
-        return namespace.isPresent() ? QName.create(namespace.get(), node) : null;
+        final Module m = globalSchema.findModuleByName(module, null);
+        return m == null ? null : QName.create(m.getQNameModule(), node);
     }
 
     private static boolean isListOrContainer(final DataSchemaNode node) {