- codec for Identityref which is used in JSON transformation can use mount point
Change-Id: Icdef737216f1d9a46cc7b40135b1b472d732caff
Signed-off-by: Martin Sunal <msunal@cisco.com>
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;
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) {
// 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() + ":"
}
} 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());
}
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();
}
def commitConfigurationDataDelete(InstanceIdentifier path) {
checkPreconditions
val transaction = dataService.beginTransaction;
+ LOG.info("Delete Configuration via Restconf: {}", path)
transaction.removeConfigurationData(path)
return transaction.commit
}
def commitConfigurationDataDeleteBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
checkPreconditions
val transaction = mountPoint.beginTransaction;
+ LOG.info("Delete Configuration via Restconf: {}", path)
transaction.removeConfigurationData(path)
return transaction.commit
}
def findModuleByNamespace(URI namespace) {
checkPreconditions
+ checkArgument(namespace !== null)
val moduleSchemas = globalSchema.findModuleByNamespace(namespace)
return moduleSchemas?.filterLatestModule
}
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
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;
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")
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")
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 {
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 {
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());
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> {
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) {
} else {
data = broker.readOperationalData(iiWithData.getInstanceIdentifier);
}
- return new StructuredData(data, iiWithData.schemaNode)
+ return new StructuredData(data, iiWithData.schemaNode, iiWithData.mountPoint)
}
override readConfigurationData(String identifier) {
} else {
data = broker.readConfigurationData(iiWithData.getInstanceIdentifier);
}
- return new StructuredData(data, iiWithData.schemaNode)
+ return new StructuredData(data, iiWithData.schemaNode, iiWithData.mountPoint)
}
override readOperationalData(String identifier) {
} 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) {
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) {
} // 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
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.*;
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() {
public DataSchemaNode getSchema() {
return schema;
}
+
+ public MountInstance getMountPoint() {
+ return mountPoint;
+ }
}
@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);
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
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();