Changed codec for Identityref in JSON transformation 38/4438/1
authorMartin Sunal <msunal@cisco.com>
Mon, 20 Jan 2014 11:39:25 +0000 (12:39 +0100)
committerMartin Sunal <msunal@cisco.com>
Mon, 20 Jan 2014 11:45:15 +0000 (12:45 +0100)
- codec for Identityref which is used in JSON transformation can use mount point

Change-Id: Icdef737216f1d9a46cc7b40135b1b472d732caff
Signed-off-by: Martin Sunal <msunal@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/StructuredDataToJsonProvider.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/BrokerFacade.xtend
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/StructuredData.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java

index 7f7e8606c3228d8b2cbebff176a81a5e2fbf9017..8956f37ce531e535157f7c8366ab9fd0db96ef0b 100644 (file)
@@ -10,6 +10,7 @@ import java.util.Set;
 
 import javax.activation.UnsupportedDataTypeException;
 
+import org.opendaylight.controller.sal.core.api.mount.MountInstance;
 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO;
 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue;
@@ -43,13 +44,15 @@ class JsonMapper {
 
     private final Set<LeafListSchemaNode> foundLeafLists = new HashSet<>();
     private final Set<ListSchemaNode> foundLists = new HashSet<>();
+    private MountInstance mountPoint;
     private final Logger logger = LoggerFactory.getLogger(JsonMapper.class);
 
-    public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException {
+    public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema, MountInstance mountPoint) throws IOException {
         Preconditions.checkNotNull(writer);
         Preconditions.checkNotNull(data);
         Preconditions.checkNotNull(schema);
-
+        this.mountPoint = mountPoint;
+        
         writer.beginObject();
 
         if (schema instanceof ContainerSchemaNode) {
@@ -192,10 +195,16 @@ class JsonMapper {
         // TODO check InstanceIdentifierTypeDefinition
         if (baseType instanceof IdentityrefTypeDefinition) {
             if (node.getValue() instanceof QName) {
-                IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(baseType).serialize(node.getValue());
+                IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(baseType, mountPoint).serialize(node.getValue());
                 IdentityValue valueFromDTO = valueDTO.getValuesWithNamespaces().get(0);
-                String moduleName = ControllerContext.getInstance().findModuleNameByNamespace(
-                        URI.create(valueFromDTO.getNamespace()));
+                String moduleName;
+                if (mountPoint != null) {
+                    moduleName = ControllerContext.getInstance().findModuleNameByNamespace(mountPoint,
+                            URI.create(valueFromDTO.getNamespace()));
+                } else {
+                    moduleName = ControllerContext.getInstance().findModuleNameByNamespace(
+                            URI.create(valueFromDTO.getNamespace()));
+                }
                 writer.value(moduleName + ":" + valueFromDTO.getValue());
             } else {
                 logger.debug("Value of " + baseType.getQName().getNamespace() + ":"
@@ -205,13 +214,13 @@ class JsonMapper {
             }
         } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition
                 || baseType instanceof UnsignedIntegerTypeDefinition) {
-            writer.value(new NumberForJsonWriter((String) RestCodec.from(baseType).serialize(node.getValue())));
+            writer.value(new NumberForJsonWriter((String) RestCodec.from(baseType, mountPoint).serialize(node.getValue())));
         } else if (baseType instanceof BooleanTypeDefinition) {
-            writer.value(Boolean.parseBoolean((String) RestCodec.from(baseType).serialize(node.getValue())));
+            writer.value(Boolean.parseBoolean((String) RestCodec.from(baseType, mountPoint).serialize(node.getValue())));
         } else if (baseType instanceof EmptyTypeDefinition) {
             writeEmptyDataTypeToJson(writer);
         } else {
-            String value = String.valueOf(RestCodec.from(baseType).serialize(node.getValue()));
+            String value = String.valueOf(RestCodec.from(baseType, mountPoint).serialize(node.getValue()));
             if (value == null) {
                 value = String.valueOf(node.getValue());
             }
index 04114fa0edd3d536cb9eb408663ab3404789d3a3..b10384c6f5910f71c3a56909419c65d3b5daba63 100644 (file)
@@ -52,7 +52,7 @@ public enum StructuredDataToJsonProvider implements MessageBodyWriter<Structured
         JsonWriter writer = new JsonWriter(new OutputStreamWriter(entityStream, "UTF-8"));
         writer.setIndent("    ");
         JsonMapper jsonMapper = new JsonMapper();
-        jsonMapper.write(writer, data, (DataNodeContainer) t.getSchema());
+        jsonMapper.write(writer, data, (DataNodeContainer) t.getSchema(), t.getMountPoint());
         writer.flush();
     }
 
index c57505829c5bde05dffbfda95ad8d21c3ac1ba26..9c0c3ac14c9eabb9757ac526b505822e573a0824 100644 (file)
@@ -113,6 +113,7 @@ class BrokerFacade implements DataReader<InstanceIdentifier, CompositeNode> {
     def commitConfigurationDataDelete(InstanceIdentifier path) {
         checkPreconditions
         val transaction = dataService.beginTransaction;
+        LOG.info("Delete Configuration via Restconf: {}", path)
         transaction.removeConfigurationData(path)
         return transaction.commit
     }
@@ -120,6 +121,7 @@ class BrokerFacade implements DataReader<InstanceIdentifier, CompositeNode> {
     def commitConfigurationDataDeleteBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
         checkPreconditions
         val transaction = mountPoint.beginTransaction;
+        LOG.info("Delete Configuration via Restconf: {}", path)
         transaction.removeConfigurationData(path)
         return transaction.commit
     }
index 61237f01a16933ef742041c787874ec9ee8fc957..d0099bb3980f50ecd5fb59813a1ea0c795a62117 100644 (file)
@@ -129,6 +129,7 @@ class ControllerContext implements SchemaServiceListener {
     
     def findModuleByNamespace(URI namespace) {
         checkPreconditions
+        checkArgument(namespace !== null)
         val moduleSchemas = globalSchema.findModuleByNamespace(namespace)
         return moduleSchemas?.filterLatestModule
     }
@@ -170,28 +171,36 @@ class ControllerContext implements SchemaServiceListener {
 
     def findModuleNameByNamespace(URI namespace) {
         checkPreconditions
-        var module = uriToModuleName.get(namespace)
-        if (module === null) {
-            val moduleSchemas = globalSchema.findModuleByNamespace(namespace);
-            if(moduleSchemas === null) return null
-            var latestModule = moduleSchemas.filterLatestModule
-            if(latestModule === null) return null
-            uriToModuleName.put(namespace, latestModule.name)
-            module = latestModule.name;
+        var moduleName = uriToModuleName.get(namespace)
+        if (moduleName === null) {
+            val module = findModuleByNamespace(namespace)
+            if (module === null) return null
+            moduleName = module.name
+            uriToModuleName.put(namespace, moduleName)
         }
-        return module
+        return moduleName
+    }
+    
+    def findModuleNameByNamespace(MountInstance mountPoint, URI namespace) {
+        val module = mountPoint.findModuleByNamespace(namespace);
+        return module?.name
     }
 
-    def findNamespaceByModuleName(String module) {
-        var namespace = moduleNameToUri.get(module)
+    def findNamespaceByModuleName(String moduleName) {
+        var namespace = moduleNameToUri.get(moduleName)
         if (namespace === null) {
-            var latestModule = globalSchema.getLatestModule(module)
-            if(latestModule === null) return null
-            namespace = latestModule.namespace
-            uriToModuleName.put(namespace, latestModule.name)
+            var module = findModuleByName(moduleName)
+            if(module === null) return null
+            namespace = module.namespace
+            uriToModuleName.put(namespace, moduleName)
         }
         return namespace
     }
+    
+    def findNamespaceByModuleName(MountInstance mountPoint, String moduleName) {
+        val module = mountPoint.findModuleByName(moduleName)
+        return module?.namespace
+    }
 
     def CharSequence toRestconfIdentifier(QName qname) {
         checkPreconditions
index 45f3f7f30bd3de9b29117e27b50767ae7e5c5166..952b6ce1bb0345823e6794bf378a92865c3bf5cf 100644 (file)
@@ -2,6 +2,7 @@ package org.opendaylight.controller.sal.restconf.impl;
 
 import java.net.URI;
 
+import org.opendaylight.controller.sal.core.api.mount.MountInstance;
 import org.opendaylight.controller.sal.rest.impl.RestUtil;
 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue;
 import org.opendaylight.yangtools.concepts.Codec;
@@ -20,8 +21,8 @@ public class RestCodec {
     private RestCodec() {
     }
 
-    public static final Codec<Object, Object> from(TypeDefinition<?> typeDefinition) {
-        return new ObjectCodec(typeDefinition);
+    public static final Codec<Object, Object> from(TypeDefinition<?> typeDefinition, MountInstance mountPoint) {
+        return new ObjectCodec(typeDefinition, mountPoint);
     }
 
     @SuppressWarnings("rawtypes")
@@ -29,13 +30,18 @@ public class RestCodec {
 
         private final Logger logger = LoggerFactory.getLogger(RestCodec.class);
 
-        public static final Codec IDENTITYREF_DEFAULT_CODEC = new IdentityrefCodecImpl();
         public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
+        private final Codec identityrefCodec;
 
-        private TypeDefinition<?> type;
+        private final TypeDefinition<?> type;
 
-        private ObjectCodec(TypeDefinition<?> typeDefinition) {
+        private ObjectCodec(TypeDefinition<?> typeDefinition, MountInstance mountPoint) {
             type = RestUtil.resolveBaseTypeFrom(typeDefinition);
+            if (type instanceof IdentityrefTypeDefinition) {
+                identityrefCodec = new IdentityrefCodecImpl(mountPoint);
+            } else {
+                identityrefCodec = null;
+            }
         }
 
         @SuppressWarnings("unchecked")
@@ -43,7 +49,7 @@ public class RestCodec {
         public Object deserialize(Object input) {
             try {
                 if (type instanceof IdentityrefTypeDefinition) {
-                    return IDENTITYREF_DEFAULT_CODEC.deserialize(input);
+                    return identityrefCodec.deserialize(input);
                 } else if (type instanceof LeafrefTypeDefinition) {
                     return LEAFREF_DEFAULT_CODEC.deserialize(input);
                 } else {
@@ -71,7 +77,7 @@ public class RestCodec {
         public Object serialize(Object input) {
             try {
                 if (type instanceof IdentityrefTypeDefinition) {
-                    return IDENTITYREF_DEFAULT_CODEC.serialize(input);
+                    return identityrefCodec.serialize(input);
                 } else if (type instanceof LeafrefTypeDefinition) {
                     return LEAFREF_DEFAULT_CODEC.serialize(input);
                 } else {
@@ -98,6 +104,12 @@ public class RestCodec {
 
     public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
 
+        private final MountInstance mountPoint;
+        
+        public IdentityrefCodecImpl(MountInstance mountPoint) {
+            this.mountPoint = mountPoint;
+        }
+        
         @Override
         public IdentityValuesDTO serialize(QName data) {
             return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix());
@@ -107,13 +119,18 @@ public class RestCodec {
         public QName deserialize(IdentityValuesDTO data) {
             IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
             String namespace = valueWithNamespace.getNamespace();
-            URI validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(namespace);
+            URI validNamespace;
+            if (mountPoint != null) {
+                validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(mountPoint, namespace);
+            } else {
+                validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(namespace);
+            }
             if (validNamespace == null) {
                 validNamespace = URI.create(namespace);
             }
             return QName.create(validNamespace, null, valueWithNamespace.getValue());
         }
-
+        
     }
 
     public static class LeafrefCodecImpl implements LeafrefCodec<String> {
index 5ad6f1eea88d1ec33fc1cc67c155c601d7f30880..0f53e56b8493560b2ff82ca773c04b4cce158ebf 100644 (file)
@@ -92,7 +92,7 @@ class RestconfImpl implements RestconfService {
         if (rpcResult.result === null) {
             return null
         }
-        return new StructuredData(rpcResult.result, rpc.output)
+        return new StructuredData(rpcResult.result, rpc.output, null)
     }
 
     override readData(String identifier) {
@@ -103,7 +103,7 @@ class RestconfImpl implements RestconfService {
         } else {
             data = broker.readOperationalData(iiWithData.getInstanceIdentifier);
         }
-        return new StructuredData(data, iiWithData.schemaNode)
+        return new StructuredData(data, iiWithData.schemaNode, iiWithData.mountPoint)
     }
 
     override readConfigurationData(String identifier) {
@@ -114,7 +114,7 @@ class RestconfImpl implements RestconfService {
         } else {
             data = broker.readConfigurationData(iiWithData.getInstanceIdentifier);
         }
-        return new StructuredData(data, iiWithData.schemaNode)
+        return new StructuredData(data, iiWithData.schemaNode, iiWithData.mountPoint)
     }
 
     override readOperationalData(String identifier) {
@@ -125,7 +125,7 @@ class RestconfImpl implements RestconfService {
         } else {
             data = broker.readOperationalData(iiWithData.getInstanceIdentifier);
         }
-        return new StructuredData(data, iiWithData.schemaNode)
+        return new StructuredData(data, iiWithData.schemaNode, iiWithData.mountPoint)
     }
 
     override updateConfigurationDataLegacy(String identifier, CompositeNode payload) {
@@ -322,7 +322,7 @@ class RestconfImpl implements RestconfService {
         if (mountPoint === null) {
             moduleName = controllerContext.findModuleNameByNamespace(validQName.namespace);
         } else {
-            moduleName = mountPoint.findModuleByNamespace(validQName.namespace)?.name
+            moduleName = controllerContext.findModuleNameByNamespace(mountPoint, validQName.namespace)
         }
         if (nodeBuilder.namespace === null || nodeBuilder.namespace == validQName.namespace ||
             nodeBuilder.namespace.toString == moduleName) {
@@ -367,7 +367,7 @@ class RestconfImpl implements RestconfService {
                 } // else value is instance of ValuesDTO
             }
             
-            val outputValue = RestCodec.from(schema.typeDefinition)?.deserialize(inputValue);
+            val outputValue = RestCodec.from(schema.typeDefinition, mountPoint)?.deserialize(inputValue);
             simpleNode.setValue(outputValue)
         } else if (nodeBuilder instanceof EmptyNodeWrapper) {
             val emptyNodeBuilder = nodeBuilder as EmptyNodeWrapper
index 62a9ae05814b3a2d7a7d259c0811d484c580f99e..38853c47d30100cf6b5a1e00e89355eb1d7a8de6 100644 (file)
@@ -1,5 +1,6 @@
 package org.opendaylight.controller.sal.restconf.impl;
 
+import org.opendaylight.controller.sal.core.api.mount.MountInstance;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.model.api.*;
 
@@ -7,10 +8,12 @@ public class StructuredData {
 
     private final CompositeNode data;
     private final DataSchemaNode schema;
+    private final MountInstance mountPoint;
 
-    public StructuredData(CompositeNode data, DataSchemaNode schema) {
+    public StructuredData(CompositeNode data, DataSchemaNode schema, MountInstance mountPoint) {
         this.data = data;
         this.schema = schema;
+        this.mountPoint = mountPoint;
     }
 
     public CompositeNode getData() {
@@ -20,4 +23,8 @@ public class StructuredData {
     public DataSchemaNode getSchema() {
         return schema;
     }
+
+    public MountInstance getMountPoint() {
+        return mountPoint;
+    }
 }
index fcc4c02a6f5e05401ec5e18ab0dc0902747d31bd..36da6d669a237b963f8b550e2b5fe6762dd83b97 100644 (file)
@@ -13,7 +13,7 @@ public class RestCodecExceptionsTest {
 
     @Test
     public void serializeExceptionTest() {
-        Codec<Object, Object> codec = RestCodec.from(new BitsType(null));
+        Codec<Object, Object> codec = RestCodec.from(new BitsType(null), null);
         String serializedValue = (String) codec.serialize("incorrect value"); // set
                                                                               // expected
         assertEquals("incorrect value", serializedValue);
@@ -23,7 +23,7 @@ public class RestCodecExceptionsTest {
     public void deserializeExceptionTest() {
         IdentityrefTypeDefinition mockedIidentityrefType = mock(IdentityrefTypeDefinition.class);
 
-        Codec<Object, Object> codec = RestCodec.from(mockedIidentityrefType);
+        Codec<Object, Object> codec = RestCodec.from(mockedIidentityrefType, null);
         String serializedValue = (String) codec.deserialize("incorrect value"); // IdentityValuesDTO
                                                                                 // object
         // expected
index 5ef66c3b257da4af2811a9e40b7c5d1908b4f474..bae5a2332d051643d147266c17b02a73180afebf 100644 (file)
@@ -273,7 +273,7 @@ public final class TestUtils {
 
         ControllerContext.getInstance().setSchemas(loadSchemaContext(modules));
 
-        messageBodyWriter.writeTo(new StructuredData(compositeNode, dataSchemaNode), null, null, null, null, null,
+        messageBodyWriter.writeTo(new StructuredData(compositeNode, dataSchemaNode, null), null, null, null, null, null,
                 byteArrayOS);
 
         return byteArrayOS.toString();