Merge "Fix race conditions between config-manager and persister."
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / ControllerContext.xtend
index c1ee611e07c77241b1346f0683387d503111e1e9..882c73d001071dea11ee8bfb1d253887780a0875 100644 (file)
@@ -9,7 +9,6 @@ import java.util.HashMap
 import java.util.List
 import java.util.Map
 import java.util.concurrent.ConcurrentHashMap
-import javax.ws.rs.WebApplicationException
 import javax.ws.rs.core.Response
 import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener
 import org.opendaylight.controller.sal.rest.impl.RestconfProvider
@@ -46,7 +45,7 @@ class ControllerContext implements SchemaServiceListener {
     
 
     private new() {
-        if (INSTANCE != null) {
+        if (INSTANCE !== null) {
             throw new IllegalStateException("Already instantiated");
         }
     }
@@ -56,9 +55,8 @@ class ControllerContext implements SchemaServiceListener {
     }
     
     private def void checkPreconditions() {
-        if (schemas == null) {
-            throw new WebApplicationException(Response.status(Response.Status.SERVICE_UNAVAILABLE)
-                    .entity(RestconfProvider::NOT_INITALIZED_MSG).build())
+        if (schemas === null) {
+            throw new ResponseException(Response.Status.SERVICE_UNAVAILABLE, RestconfProvider::NOT_INITALIZED_MSG)
         }
     }
 
@@ -72,7 +70,7 @@ class ControllerContext implements SchemaServiceListener {
             pathArgs.remove(0)
         }
         val schemaNode = ret.collectPathArguments(pathArgs, restconfInstance.findModule);
-        if (schemaNode == null) {
+        if (schemaNode === null) {
             return null
         }
         new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
@@ -92,7 +90,7 @@ class ControllerContext implements SchemaServiceListener {
 
     private def getLatestModule(SchemaContext schema, String moduleName) {
         checkNotNull(schema)
-        checkArgument(moduleName != null && !moduleName.empty)
+        checkArgument(moduleName !== null && !moduleName.empty)
         val modules = schema.modules.filter[m|m.name == moduleName]
         var latestModule = modules.head
         for (module : modules) {
@@ -130,13 +128,32 @@ class ControllerContext implements SchemaServiceListener {
     private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
         throw new IllegalArgumentException("Conversion of generic path argument is not supported");
     }
+    
+    def findModuleByNamespace(URI namespace) {
+        checkPreconditions
+        var module = uriToModuleName.get(namespace)
+        if (module === null) {
+            val moduleSchemas = schemas.findModuleByNamespace(namespace);
+            if(moduleSchemas === null) throw new IllegalArgumentException()
+            var latestModule = moduleSchemas.head
+            for (m : moduleSchemas) {
+                if (m.revision.after(latestModule.revision)) {
+                    latestModule = m
+                }
+            }
+            if(latestModule === null) throw new IllegalArgumentException()
+            uriToModuleName.put(namespace, latestModule.name)
+            module = latestModule.name;
+        }
+        return module
+    }
 
     def CharSequence toRestconfIdentifier(QName qname) {
         checkPreconditions
         var module = uriToModuleName.get(qname.namespace)
-        if (module == null) {
+        if (module === null) {
             val moduleSchema = schemas.findModuleByNamespaceAndRevision(qname.namespace, qname.revision);
-            if(moduleSchema == null) throw new IllegalArgumentException()
+            if(moduleSchema === null) throw new IllegalArgumentException()
             uriToModuleName.put(qname.namespace, moduleSchema.name)
             module = moduleSchema.name;
         }
@@ -189,13 +206,16 @@ class ControllerContext implements SchemaServiceListener {
     }
 
     private def toUriString(Object object) {
-        if(object == null) return "";
+        if(object === null) return "";
         return URLEncoder.encode(object.toString)
     }
 
     private def DataSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
         DataNodeContainer parentNode) {
         checkNotNull(strings)
+        if (parentNode === null) {
+            return null;
+        }
         if (strings.empty) {
             return parentNode as DataSchemaNode;
         }
@@ -203,14 +223,14 @@ class ControllerContext implements SchemaServiceListener {
 
         val nodeName = nodeRef.toNodeName();
         val targetNode = parentNode.getDataChildByName(nodeName);
-        if (targetNode == null) {
+        if (targetNode === null) {
             val children = parentNode.childNodes
             for (child : children) {
                 if (child instanceof ChoiceNode) {
                     val choice = child as ChoiceNode
                     for (caze : choice.cases) {
                         val result = builder.collectPathArguments(strings, caze as DataNodeContainer);
-                        if (result != null)
+                        if (result !== null)
                             return result
                     }
                 }