Added support for /modules, /modules/module, /operations resources
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / ControllerContext.xtend
index 2dfe5062c89af5b2f0151dfee3721d0ba7486521..d8a03e5d4fbf65c813df8576cde6e1bdc9e02efe 100644 (file)
@@ -86,7 +86,15 @@ class ControllerContext implements SchemaServiceListener {
         onGlobalContextUpdated(schemas)
     }
 
-    public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
+    def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
+        return restconfInstance.toIdentifier(false)
+    }
+
+    def InstanceIdWithSchemaNode toMountPointIdentifier(String restconfInstance) {
+        return restconfInstance.toIdentifier(true)
+    }
+
+    private def InstanceIdWithSchemaNode toIdentifier(String restconfInstance, boolean toMountPointIdentifier) {
         checkPreconditions
         val pathArgs = restconfInstance.split("/");
         if (pathArgs.empty) {
@@ -99,8 +107,14 @@ class ControllerContext implements SchemaServiceListener {
         if (startModule === null) {
             throw new ResponseException(BAD_REQUEST, "First node in URI has to be in format \"moduleName:nodeName\"")
         }
-        val iiWithSchemaNode = collectPathArguments(InstanceIdentifier.builder(), pathArgs,
-            globalSchema.getLatestModule(startModule), null);
+        var InstanceIdWithSchemaNode iiWithSchemaNode = null;
+        if (toMountPointIdentifier) {
+            iiWithSchemaNode = collectPathArguments(InstanceIdentifier.builder(), pathArgs,
+            globalSchema.getLatestModule(startModule), null, true);
+        } else {
+            iiWithSchemaNode = collectPathArguments(InstanceIdentifier.builder(), pathArgs,
+            globalSchema.getLatestModule(startModule), null, false);
+        }
         if (iiWithSchemaNode === null) {
             throw new ResponseException(BAD_REQUEST, "URI has bad format")
         }
@@ -150,6 +164,18 @@ class ControllerContext implements SchemaServiceListener {
         return moduleSchemas?.filterLatestModule
     }
 
+    def findModuleByNameAndRevision(QName module) {
+        checkPreconditions
+        checkArgument(module !== null && module.localName !== null && module.revision !== null)
+        return globalSchema.findModuleByName(module.localName, module.revision)
+    }
+
+    def findModuleByNameAndRevision(MountInstance mountPoint, QName module) {
+        checkPreconditions
+        checkArgument(module !== null && module.localName !== null && module.revision !== null && mountPoint !== null)
+        return mountPoint.schemaContext?.findModuleByName(module.localName, module.revision)
+    }
+
     def String toFullRestconfIdentifier(InstanceIdentifier path) {
         checkPreconditions
         val elements = path.path;
@@ -211,6 +237,16 @@ class ControllerContext implements SchemaServiceListener {
         return module?.namespace
     }
 
+    def getAllModules(MountInstance mountPoint) {
+        checkPreconditions
+        return mountPoint?.schemaContext?.modules
+    }
+    
+    def getAllModules() {
+        checkPreconditions
+        return globalSchema.modules
+    }
+
     def CharSequence toRestconfIdentifier(QName qname) {
         checkPreconditions
         var module = uriToModuleName.get(qname.namespace)
@@ -281,7 +317,7 @@ class ControllerContext implements SchemaServiceListener {
     }
     
     private def InstanceIdWithSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
-        DataNodeContainer parentNode, MountInstance mountPoint) {
+        DataNodeContainer parentNode, MountInstance mountPoint, boolean returnJustMountPoint) {
         checkNotNull(strings)
         if (parentNode === null) {
             return null;
@@ -318,6 +354,10 @@ class ControllerContext implements SchemaServiceListener {
                     throw new ResponseException(BAD_REQUEST, "Mount point does not contain any schema with modules.")
                 }
                 
+                if (returnJustMountPoint) {
+                    return new InstanceIdWithSchemaNode(InstanceIdentifier.builder().toInstance, mountPointSchema, mount)
+                }
+                
                 if (strings.size == 1) { // any data node is not behind mount point
                     return new InstanceIdWithSchemaNode(InstanceIdentifier.builder().toInstance, mountPointSchema, mount)
                 }
@@ -335,7 +375,7 @@ class ControllerContext implements SchemaServiceListener {
                 }
                 
                 return collectPathArguments(InstanceIdentifier.builder(), strings.subList(1, strings.size),
-                    moduleBehindMountPoint, mount);
+                    moduleBehindMountPoint, mount, returnJustMountPoint);
             }
             
             var Module module = null;
@@ -412,7 +452,7 @@ class ControllerContext implements SchemaServiceListener {
         }
         if (targetNode instanceof DataNodeContainer) {
             val remaining = strings.subList(consumed, strings.length);
-            val result = builder.collectPathArguments(remaining, targetNode as DataNodeContainer, mountPoint);
+            val result = builder.collectPathArguments(remaining, targetNode as DataNodeContainer, mountPoint, returnJustMountPoint);
             return result
         }