X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2FRestCodec.java;h=ceac03e3d97eccabca05633652179ce1bb3acb1c;hp=4f643fe34d02461a00a71ceef4f53da24670e75c;hb=f3bb0de0240648e0c4ddf73a304a5ef65bd1df29;hpb=405ea7ce68d22bd3d2501857c5253793b581b086 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 4f643fe34d..ceac03e3d9 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 @@ -12,18 +12,17 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - -import org.opendaylight.controller.sal.core.api.mount.MountInstance; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; import org.opendaylight.controller.sal.rest.impl.RestUtil; import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue; import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.Predicate; import org.opendaylight.yangtools.concepts.Codec; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec; import org.opendaylight.yangtools.yang.data.api.codec.InstanceIdentifierCodec; import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec; @@ -41,13 +40,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class RestCodec { - + private static final Logger logger = LoggerFactory.getLogger(RestCodec.class); private RestCodec() { } - public static final Codec from(TypeDefinition typeDefinition, MountInstance mountPoint) { + public static final Codec from(final TypeDefinition typeDefinition, + final DOMMountPoint mountPoint) { return new ObjectCodec(typeDefinition, mountPoint); } @@ -62,7 +62,7 @@ public class RestCodec { private final TypeDefinition type; - private ObjectCodec(TypeDefinition typeDefinition, MountInstance mountPoint) { + private ObjectCodec(final TypeDefinition typeDefinition, final DOMMountPoint mountPoint) { type = RestUtil.resolveBaseTypeFrom(typeDefinition); if (type instanceof IdentityrefTypeDefinition) { identityrefCodec = new IdentityrefCodecImpl(mountPoint); @@ -78,18 +78,16 @@ public class RestCodec { @SuppressWarnings("unchecked") @Override - public Object deserialize(Object input) { + public Object deserialize(final Object input) { try { if (type instanceof IdentityrefTypeDefinition) { if (input instanceof IdentityValuesDTO) { return identityrefCodec.deserialize(input); } - logger.info( + logger.debug( "Value is not instance of IdentityrefTypeDefinition but is {}. Therefore NULL is used as translation of - {}", input == null ? "null" : input.getClass(), String.valueOf(input)); return null; - } else if (type instanceof LeafrefTypeDefinition) { - return LEAFREF_DEFAULT_CODEC.deserialize(input); } else if (type instanceof InstanceIdentifierTypeDefinition) { if (input instanceof IdentityValuesDTO) { return instanceIdentifier.deserialize(input); @@ -102,6 +100,9 @@ public class RestCodec { TypeDefinitionAwareCodec> typeAwarecodec = TypeDefinitionAwareCodec .from(type); if (typeAwarecodec != null) { + if (input instanceof IdentityValuesDTO) { + return typeAwarecodec.deserialize(((IdentityValuesDTO) input).getOriginValue()); + } return typeAwarecodec.deserialize(String.valueOf(input)); } else { logger.debug("Codec for type \"" + type.getQName().getLocalName() @@ -109,8 +110,7 @@ public class RestCodec { return null; } } - } catch (ClassCastException e) { // TODO remove this catch when - // everyone use codecs + } catch (ClassCastException e) { // TODO remove this catch when everyone use codecs logger.error( "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), e); @@ -120,7 +120,7 @@ public class RestCodec { @SuppressWarnings("unchecked") @Override - public Object serialize(Object input) { + public Object serialize(final Object input) { try { if (type instanceof IdentityrefTypeDefinition) { return identityrefCodec.serialize(input); @@ -139,8 +139,7 @@ public class RestCodec { return null; } } - } catch (ClassCastException e) { // TODO remove this catch when - // everyone use codecs + } catch (ClassCastException e) { // TODO remove this catch when everyone use codecs logger.error( "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), e); @@ -154,19 +153,19 @@ public class RestCodec { private final Logger logger = LoggerFactory.getLogger(IdentityrefCodecImpl.class); - private final MountInstance mountPoint; + private final DOMMountPoint mountPoint; - public IdentityrefCodecImpl(MountInstance mountPoint) { + public IdentityrefCodecImpl(final DOMMountPoint mountPoint) { this.mountPoint = mountPoint; } @Override - public IdentityValuesDTO serialize(QName data) { - return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix()); + public IdentityValuesDTO serialize(final QName data) { + return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix(), null); } @Override - public QName deserialize(IdentityValuesDTO data) { + public QName deserialize(final IdentityValuesDTO data) { IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0); Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), mountPoint); if (module == null) { @@ -174,7 +173,7 @@ public class RestCodec { logger.info("Idenetityref will be translated as NULL for data - {}", String.valueOf(valueWithNamespace)); return null; } - + return QName.create(module.getNamespace(), module.getRevision(), valueWithNamespace.getValue()); } @@ -183,12 +182,12 @@ public class RestCodec { public static class LeafrefCodecImpl implements LeafrefCodec { @Override - public String serialize(Object data) { + public String serialize(final Object data) { return String.valueOf(data); } @Override - public Object deserialize(String data) { + public Object deserialize(final String data) { return data; } @@ -196,22 +195,26 @@ public class RestCodec { public static class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { private final Logger logger = LoggerFactory.getLogger(InstanceIdentifierCodecImpl.class); - private final MountInstance mountPoint; + private final DOMMountPoint mountPoint; - public InstanceIdentifierCodecImpl(MountInstance mountPoint) { + public InstanceIdentifierCodecImpl(final DOMMountPoint mountPoint) { this.mountPoint = mountPoint; } @Override - public IdentityValuesDTO serialize(InstanceIdentifier data) { - List pathArguments = data.getPath(); + public IdentityValuesDTO serialize(final YangInstanceIdentifier data) { IdentityValuesDTO identityValuesDTO = new IdentityValuesDTO(); - for (PathArgument pathArgument : pathArguments) { + for (PathArgument pathArgument : data.getPathArguments()) { IdentityValue identityValue = qNameToIdentityValue(pathArgument.getNodeType()); if (pathArgument instanceof NodeIdentifierWithPredicates && identityValue != null) { List predicates = keyValuesToPredicateList(((NodeIdentifierWithPredicates) pathArgument) .getKeyValues()); identityValue.setPredicates(predicates); + } else if (pathArgument instanceof NodeWithValue && identityValue != null) { + List predicates = new ArrayList<>(); + String value = String.valueOf(((NodeWithValue) pathArgument).getValue()); + predicates.add(new Predicate(null, value)); + identityValue.setPredicates(predicates); } identityValuesDTO.add(identityValue); } @@ -219,13 +222,15 @@ public class RestCodec { } @Override - public InstanceIdentifier deserialize(IdentityValuesDTO data) { + public YangInstanceIdentifier deserialize(final IdentityValuesDTO data) { List result = new ArrayList(); IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0); Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), mountPoint); if (module == null) { - logger.info("Module by namespace '{}' of first node in instance-identiefier was not found.", valueWithNamespace.getNamespace()); - logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(valueWithNamespace.getValue())); + logger.info("Module by namespace '{}' of first node in instance-identifier was not found.", + valueWithNamespace.getNamespace()); + logger.info("Instance-identifier will be translated as NULL for data - {}", + String.valueOf(valueWithNamespace.getValue())); return null; } @@ -234,11 +239,12 @@ public class RestCodec { for (int i = 0; i < identities.size(); i++) { IdentityValue identityValue = identities.get(i); URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), mountPoint); - DataSchemaNode node = ControllerContext.getInstance().findInstanceDataChildByNameAndNamespace( + DataSchemaNode node = ControllerContext.findInstanceDataChildByNameAndNamespace( parentContainer, identityValue.getValue(), validNamespace); if (node == null) { logger.info("'{}' node was not found in {}", identityValue, parentContainer.getChildNodes()); - logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(identityValue.getValue())); + logger.info("Instance-identifier will be translated as NULL for data - {}", + String.valueOf(identityValue.getValue())); return null; } QName qName = node.getQName(); @@ -250,7 +256,8 @@ public class RestCodec { Predicate leafListPredicate = identityValue.getPredicates().get(0); if (!leafListPredicate.isLeafList()) { logger.info("Predicate's data is not type of leaf-list. It should be in format \".='value'\""); - logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(identityValue.getValue())); + logger.info("Instance-identifier will be translated as NULL for data - {}", + String.valueOf(identityValue.getValue())); return null; } pathArgument = new NodeWithValue(qName, leafListPredicate.getValue()); @@ -259,33 +266,37 @@ public class RestCodec { Map predicatesMap = new HashMap<>(); for (Predicate predicate : identityValue.getPredicates()) { validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), mountPoint); - DataSchemaNode listKey = ControllerContext.getInstance().findInstanceDataChildByNameAndNamespace( - listNode, predicate.getName().getValue(), validNamespace); + DataSchemaNode listKey = ControllerContext + .findInstanceDataChildByNameAndNamespace(listNode, predicate.getName().getValue(), + validNamespace); predicatesMap.put(listKey.getQName(), predicate.getValue()); } pathArgument = new NodeIdentifierWithPredicates(qName, predicatesMap); } else { logger.info("Node {} is not List or Leaf-list.", node); - logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(identityValue.getValue())); + logger.info("Instance-identifier will be translated as NULL for data - {}", + String.valueOf(identityValue.getValue())); return null; } } result.add(pathArgument); - if (i < identities.size() - 1) { // last element in instance-identifier can be other than DataNodeContainer + if (i < identities.size() - 1) { // last element in instance-identifier can be other than + // DataNodeContainer if (node instanceof DataNodeContainer) { parentContainer = (DataNodeContainer) node; } else { logger.info("Node {} isn't instance of DataNodeContainer", node); - logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(identityValue.getValue())); + logger.info("Instance-identifier will be translated as NULL for data - {}", + String.valueOf(identityValue.getValue())); return null; } } } - - return result.isEmpty() ? null : new InstanceIdentifier(result); + + return result.isEmpty() ? null : YangInstanceIdentifier.create(result); } - private List keyValuesToPredicateList(Map keyValues) { + private List keyValuesToPredicateList(final Map keyValues) { List result = new ArrayList<>(); for (QName qName : keyValues.keySet()) { Object value = keyValues.get(qName); @@ -294,15 +305,15 @@ public class RestCodec { return result; } - private IdentityValue qNameToIdentityValue(QName qName) { + private IdentityValue qNameToIdentityValue(final QName qName) { if (qName != null) { return new IdentityValue(qName.getNamespace().toString(), qName.getLocalName(), qName.getPrefix()); } return null; } } - - private static Module getModuleByNamespace(String namespace, MountInstance mountPoint) { + + private static Module getModuleByNamespace(final String namespace, final DOMMountPoint mountPoint) { URI validNamespace = resolveValidNamespace(namespace, mountPoint); Module module = null; @@ -317,8 +328,8 @@ public class RestCodec { } return module; } - - private static URI resolveValidNamespace(String namespace, MountInstance mountPoint) { + + private static URI resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint) { URI validNamespace; if (mountPoint != null) { validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(mountPoint, namespace);