Merge "Changed codec for Identityref in JSON transformation"
authorEd Warnicke <eaw@cisco.com>
Wed, 22 Jan 2014 02:45:31 +0000 (02:45 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 22 Jan 2014 02:45:31 +0000 (02:45 +0000)
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 7f7e860..8956f37 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 04114fa..b10384c 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 c575058..9c0c3ac 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 61237f0..d0099bb 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 45f3f7f..952b6ce 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 5ad6f1e..0f53e56 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 62a9ae0..38853c4 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 fcc4c02..36da6d6 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 5ef66c3..bae5a23 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();