Merge "Bug 946: Fixed eclipse errors with nagasena projects"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / ControllerContext.xtend
index c2b82eae632290cc60d48df245d70fa1c583e8b8..cb02fc89bfe604fb58e34854ea85f27edb1a403e 100644 (file)
@@ -43,20 +43,22 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
 import org.opendaylight.yangtools.yang.model.api.Module
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition
 import org.opendaylight.yangtools.yang.model.api.SchemaContext
-import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition
 import org.slf4j.LoggerFactory
 
 import static com.google.common.base.Preconditions.*
 import static javax.ws.rs.core.Response.Status.*
 
-class ControllerContext implements SchemaServiceListener {
+class ControllerContext implements SchemaContextListener {
     val static LOG = LoggerFactory.getLogger(ControllerContext)
     val static ControllerContext INSTANCE = new ControllerContext
     val static NULL_VALUE = "null"
     val static MOUNT_MODULE = "yang-ext"
     val static MOUNT_NODE = "mount"
     public val static MOUNT = "yang-ext:mount"
+    val static URI_ENCODING_CHAR_SET = "ISO-8859-1"
+    val static URI_SLASH_PLACEHOLDER = "%2F";
 
     @Property
     var SchemaContext globalSchema;
@@ -98,7 +100,8 @@ class ControllerContext implements SchemaServiceListener {
 
     private def InstanceIdWithSchemaNode toIdentifier(String restconfInstance, boolean toMountPointIdentifier) {
         checkPreconditions
-        val pathArgs = Lists.newArrayList(Splitter.on("/").split(restconfInstance))
+        val encodedPathArgs = Lists.newArrayList(Splitter.on("/").split(restconfInstance))
+        val pathArgs = urlPathArgsDecode(encodedPathArgs)
         pathArgs.omitFirstAndLastEmptyString
         if (pathArgs.empty) {
             return null;
@@ -353,7 +356,7 @@ class ControllerContext implements SchemaServiceListener {
 
     private def toUriString(Object object) {
         if(object === null) return "";
-        return URLEncoder.encode(object.toString)
+        return URLEncoder.encode(object.toString,URI_ENCODING_CHAR_SET)        
     }
     
     private def InstanceIdWithSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
@@ -480,7 +483,7 @@ class ControllerContext implements SchemaServiceListener {
                         + "\" cannot contain \"null\" value as a key."
                     )
                 }
-                keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue);
+                keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue, mountPoint);
                 i = i + 1;
             }
             consumed = consumed + i;
@@ -538,19 +541,25 @@ class ControllerContext implements SchemaServiceListener {
         }
     }
     
-    private def void addKeyValue(HashMap<QName, Object> map, DataSchemaNode node, String uriValue) {
+    private def void addKeyValue(HashMap<QName, Object> map, DataSchemaNode node, String uriValue, MountInstance mountPoint) {
         checkNotNull(uriValue);
         checkArgument(node instanceof LeafSchemaNode);
         val urlDecoded = URLDecoder.decode(uriValue);
         val typedef = (node as LeafSchemaNode).type;
         
-        var decoded = TypeDefinitionAwareCodec.from(typedef)?.deserialize(urlDecoded)
+        var decoded = RestCodec.from(typedef, mountPoint)?.deserialize(urlDecoded)
+        var additionalInfo = ""
         if(decoded === null) {
             var baseType = RestUtil.resolveBaseTypeFrom(typedef)
             if(baseType instanceof IdentityrefTypeDefinition) {
                 decoded = toQName(urlDecoded)
+                additionalInfo = "For key which is of type identityref it should be in format module_name:identity_name."
             }
         }
+        if (decoded === null) {
+            throw new ResponseException(BAD_REQUEST, uriValue + " from URI can't be resolved. "+  additionalInfo )
+        }                
+        
         map.put(node.QName, decoded);
     }
 
@@ -599,11 +608,30 @@ class ControllerContext implements SchemaServiceListener {
     }
 
     override onGlobalContextUpdated(SchemaContext context) {
-        this.globalSchema = context;
-        for (operation : context.operations) {
-            val qname = operation.QName;
-            qnameToRpc.put(qname, operation);
+        if (context !== null) {
+            qnameToRpc.clear
+            this.globalSchema = context;
+            for (operation : context.operations) {
+                val qname = operation.QName;
+                qnameToRpc.put(qname, operation);
+            }
+        }
+    }
+
+
+    def urlPathArgsDecode(List<String> strings) {
+        val List<String> decodedPathArgs = new ArrayList();
+        for (pathArg : strings) {
+            decodedPathArgs.add(URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET))
         }
+        return decodedPathArgs
     }
 
+    def urlPathArgDecode(String pathArg) {
+        if (pathArg !== null) {
+            return URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET)
+        }
+        return null
+    }    
+
 }