From d8d2798ea4ec48c2987f6ab712c1548d4fde0e3c Mon Sep 17 00:00:00 2001 From: Martin Sunal Date: Mon, 20 Jan 2014 12:39:25 +0100 Subject: [PATCH] Changed codec for Identityref in JSON transformation - codec for Identityref which is used in JSON transformation can use mount point Change-Id: Icdef737216f1d9a46cc7b40135b1b472d732caff Signed-off-by: Martin Sunal --- .../controller/sal/rest/impl/JsonMapper.java | 25 ++++++++---- .../impl/StructuredDataToJsonProvider.java | 2 +- .../sal/restconf/impl/BrokerFacade.xtend | 2 + .../sal/restconf/impl/ControllerContext.xtend | 39 ++++++++++++------- .../sal/restconf/impl/RestCodec.java | 35 ++++++++++++----- .../sal/restconf/impl/RestconfImpl.xtend | 12 +++--- .../sal/restconf/impl/StructuredData.java | 9 ++++- .../impl/test/RestCodecExceptionsTest.java | 4 +- .../sal/restconf/impl/test/TestUtils.java | 2 +- 9 files changed, 87 insertions(+), 43 deletions(-) diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java index 7f7e8606c3..8956f37ce5 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java @@ -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 foundLeafLists = new HashSet<>(); private final Set 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()); } diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/StructuredDataToJsonProvider.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/StructuredDataToJsonProvider.java index 04114fa0ed..b10384c6f5 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/StructuredDataToJsonProvider.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/StructuredDataToJsonProvider.java @@ -52,7 +52,7 @@ public enum StructuredDataToJsonProvider implements MessageBodyWriter { 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 { def commitConfigurationDataDeleteBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) { checkPreconditions val transaction = mountPoint.beginTransaction; + LOG.info("Delete Configuration via Restconf: {}", path) transaction.removeConfigurationData(path) return transaction.commit } diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend index 61237f01a1..d0099bb398 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend @@ -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 diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java index 45f3f7f30b..952b6ce1bb 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java @@ -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 from(TypeDefinition typeDefinition) { - return new ObjectCodec(typeDefinition); + public static final Codec 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 { + 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 { diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend index 5ad6f1eea8..0f53e56b84 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend @@ -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 diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/StructuredData.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/StructuredData.java index 62a9ae0581..38853c47d3 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/StructuredData.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/StructuredData.java @@ -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; + } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java index fcc4c02a6f..36da6d669a 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java @@ -13,7 +13,7 @@ public class RestCodecExceptionsTest { @Test public void serializeExceptionTest() { - Codec codec = RestCodec.from(new BitsType(null)); + Codec 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 codec = RestCodec.from(mockedIidentityrefType); + Codec codec = RestCodec.from(mockedIidentityrefType, null); String serializedValue = (String) codec.deserialize("incorrect value"); // IdentityValuesDTO // object // expected diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java index 5ef66c3b25..bae5a2332d 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java @@ -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(); -- 2.36.6