import java.net.URI;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import org.opendaylight.restconf.utils.patch.Draft16StringModuleInstanceIdentifierCodec;
import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
import org.opendaylight.yangtools.yang.model.api.Module;
/**
* @deprecated This class will be replaced by
- * {@link org.opendaylight.restconf.utils.patch.Draft11StringModuleInstanceIdentifierCodec}
+ * {@link Draft16StringModuleInstanceIdentifierCodec}
*/
@Deprecated
-final class StringModuleInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec {
+public final class StringModuleInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec {
private final DataSchemaContextTree dataContextTree;
private final SchemaContext context;
private final String defaultPrefix;
- StringModuleInstanceIdentifierCodec(SchemaContext context) {
+ public StringModuleInstanceIdentifierCodec(final SchemaContext context) {
this.context = Preconditions.checkNotNull(context);
this.dataContextTree = DataSchemaContextTree.from(context);
this.defaultPrefix = "";
}
- StringModuleInstanceIdentifierCodec(SchemaContext context, @Nonnull String defaultPrefix) {
+ StringModuleInstanceIdentifierCodec(final SchemaContext context, @Nonnull final String defaultPrefix) {
this.context = Preconditions.checkNotNull(context);
this.dataContextTree = DataSchemaContextTree.from(context);
this.defaultPrefix = defaultPrefix;
}
@Override
- protected Module moduleForPrefix(@Nonnull String prefix) {
+ protected Module moduleForPrefix(@Nonnull final String prefix) {
if (prefix.isEmpty() && !this.defaultPrefix.isEmpty()) {
return this.context.findModuleByName(this.defaultPrefix, null);
} else {
@Nullable
@Override
- protected String prefixForNamespace(@Nonnull URI namespace) {
+ protected String prefixForNamespace(@Nonnull final URI namespace) {
final Module module = this.context.findModuleByNamespaceAndRevision(namespace, null);
return module == null ? null : module.getName();
}
import java.util.Map;
import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
import org.opendaylight.netconf.sal.rest.impl.RestUtil;
+import org.opendaylight.netconf.sal.rest.impl.StringModuleInstanceIdentifierCodec;
import org.opendaylight.netconf.sal.restconf.impl.IdentityValuesDTO.IdentityValue;
import org.opendaylight.netconf.sal.restconf.impl.IdentityValuesDTO.Predicate;
import org.opendaylight.yangtools.concepts.Codec;
private final TypeDefinition<?> type;
private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint) {
- type = RestUtil.resolveBaseTypeFrom(typeDefinition);
- if (type instanceof IdentityrefTypeDefinition) {
- identityrefCodec = new IdentityrefCodecImpl(mountPoint);
+ this.type = RestUtil.resolveBaseTypeFrom(typeDefinition);
+ if (this.type instanceof IdentityrefTypeDefinition) {
+ this.identityrefCodec = new IdentityrefCodecImpl(mountPoint);
} else {
- identityrefCodec = null;
+ this.identityrefCodec = null;
}
- if (type instanceof InstanceIdentifierTypeDefinition) {
- instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint);
+ if (this.type instanceof InstanceIdentifierTypeDefinition) {
+ this.instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint);
} else {
- instanceIdentifier = null;
+ this.instanceIdentifier = null;
}
}
@Override
public Object deserialize(final Object input) {
try {
- if (type instanceof IdentityrefTypeDefinition) {
+ if (this.type instanceof IdentityrefTypeDefinition) {
if (input instanceof IdentityValuesDTO) {
- return identityrefCodec.deserialize(input);
+ return this.identityrefCodec.deserialize(input);
}
if(LOG.isDebugEnabled()) {
LOG.debug(
input == null ? "null" : input.getClass(), String.valueOf(input));
}
return null;
- } else if (type instanceof InstanceIdentifierTypeDefinition) {
+ } else if (this.type instanceof InstanceIdentifierTypeDefinition) {
if (input instanceof IdentityValuesDTO) {
- return instanceIdentifier.deserialize(input);
+ return this.instanceIdentifier.deserialize(input);
+ } else {
+ final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
+ ControllerContext.getInstance().getGlobalSchema());
+ return codec.deserialize((String) input);
}
- LOG.info(
- "Value is not instance of InstanceIdentifierTypeDefinition but is {}. Therefore NULL is used as translation of - {}",
- input == null ? "null" : input.getClass(), String.valueOf(input));
- return null;
} else {
final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
- .from(type);
+ .from(this.type);
if (typeAwarecodec != null) {
if (input instanceof IdentityValuesDTO) {
return typeAwarecodec.deserialize(((IdentityValuesDTO) input).getOriginValue());
}
return typeAwarecodec.deserialize(String.valueOf(input));
} else {
- LOG.debug("Codec for type \"" + type.getQName().getLocalName()
+ LOG.debug("Codec for type \"" + this.type.getQName().getLocalName()
+ "\" is not implemented yet.");
return null;
}
@Override
public Object serialize(final Object input) {
try {
- if (type instanceof IdentityrefTypeDefinition) {
- return identityrefCodec.serialize(input);
- } else if (type instanceof LeafrefTypeDefinition) {
+ if (this.type instanceof IdentityrefTypeDefinition) {
+ return this.identityrefCodec.serialize(input);
+ } else if (this.type instanceof LeafrefTypeDefinition) {
return LEAFREF_DEFAULT_CODEC.serialize(input);
- } else if (type instanceof InstanceIdentifierTypeDefinition) {
- return instanceIdentifier.serialize(input);
+ } else if (this.type instanceof InstanceIdentifierTypeDefinition) {
+ return this.instanceIdentifier.serialize(input);
} else {
final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
- .from(type);
+ .from(this.type);
if (typeAwarecodec != null) {
return typeAwarecodec.serialize(input);
} else {
if(LOG.isDebugEnabled()) {
- LOG.debug("Codec for type \"" + type.getQName().getLocalName()
+ LOG.debug("Codec for type \"" + this.type.getQName().getLocalName()
+ "\" is not implemented yet.");
}
return null;
@Override
public QName deserialize(final IdentityValuesDTO data) {
final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
- final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), mountPoint);
+ final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint);
if (module == null) {
LOG.info("Module was not found for namespace {}", valueWithNamespace.getNamespace());
LOG.info("Idenetityref will be translated as NULL for data - {}", String.valueOf(valueWithNamespace));
final IdentityValuesDTO identityValuesDTO = new IdentityValuesDTO();
for (final PathArgument pathArgument : data.getPathArguments()) {
final IdentityValue identityValue = qNameToIdentityValue(pathArgument.getNodeType());
- if (pathArgument instanceof NodeIdentifierWithPredicates && identityValue != null) {
+ if ((pathArgument instanceof NodeIdentifierWithPredicates) && (identityValue != null)) {
final List<Predicate> predicates = keyValuesToPredicateList(((NodeIdentifierWithPredicates) pathArgument)
.getKeyValues());
identityValue.setPredicates(predicates);
- } else if (pathArgument instanceof NodeWithValue && identityValue != null) {
+ } else if ((pathArgument instanceof NodeWithValue) && (identityValue != null)) {
final List<Predicate> predicates = new ArrayList<>();
final String value = String.valueOf(((NodeWithValue) pathArgument).getValue());
predicates.add(new Predicate(null, value));
public YangInstanceIdentifier deserialize(final IdentityValuesDTO data) {
final List<PathArgument> result = new ArrayList<PathArgument>();
final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
- final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), mountPoint);
+ final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint);
if (module == null) {
LOG.info("Module by namespace '{}' of first node in instance-identifier was not found.",
valueWithNamespace.getNamespace());
final List<IdentityValue> identities = data.getValuesWithNamespaces();
for (int i = 0; i < identities.size(); i++) {
final IdentityValue identityValue = identities.get(i);
- URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), mountPoint);
+ URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), this.mountPoint);
final DataSchemaNode node = ControllerContext.findInstanceDataChildByNameAndNamespace(
parentContainer, identityValue.getValue(), validNamespace);
if (node == null) {
final DataNodeContainer listNode = (DataNodeContainer) node;
final Map<QName, Object> predicatesMap = new HashMap<>();
for (final Predicate predicate : identityValue.getPredicates()) {
- validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), mountPoint);
+ validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), this.mountPoint);
final DataSchemaNode listKey = ControllerContext
.findInstanceDataChildByNameAndNamespace(listNode, predicate.getName().getValue(),
validNamespace);
}
}
result.add(pathArgument);
- if (i < identities.size() - 1) { // last element in instance-identifier can be other than
+ if (i < (identities.size() - 1)) { // last element in instance-identifier can be other than
// DataNodeContainer
if (node instanceof DataNodeContainer) {
parentContainer = (DataNodeContainer) node;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-final class Draft16StringModuleInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec {
+public final class Draft16StringModuleInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec {
private final DataSchemaContextTree dataContextTree;
private final SchemaContext context;
private final String defaultPrefix;
- Draft16StringModuleInstanceIdentifierCodec(SchemaContext context) {
+ public Draft16StringModuleInstanceIdentifierCodec(final SchemaContext context) {
this.context = Preconditions.checkNotNull(context);
this.dataContextTree = DataSchemaContextTree.from(context);
this.defaultPrefix = "";
}
- Draft16StringModuleInstanceIdentifierCodec(SchemaContext context, @Nonnull String defaultPrefix) {
+ Draft16StringModuleInstanceIdentifierCodec(final SchemaContext context, @Nonnull final String defaultPrefix) {
this.context = Preconditions.checkNotNull(context);
this.dataContextTree = DataSchemaContextTree.from(context);
this.defaultPrefix = defaultPrefix;
}
@Override
- protected Module moduleForPrefix(@Nonnull String prefix) {
+ protected Module moduleForPrefix(@Nonnull final String prefix) {
if (prefix.isEmpty() && !this.defaultPrefix.isEmpty()) {
return this.context.findModuleByName(this.defaultPrefix, null);
} else {
@Nullable
@Override
- protected String prefixForNamespace(@Nonnull URI namespace) {
+ protected String prefixForNamespace(@Nonnull final URI namespace) {
final Module module = this.context.findModuleByNamespaceAndRevision(namespace, null);
return module == null ? null : module.getName();
}
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.rest.impl;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
+import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
+import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class InstanceIdentifierTypeLeafTest {
+
+ @Test
+ public void stringToInstanceIdentifierTest() throws Exception {
+ final SchemaContext schemaContext = TestRestconfUtils.loadSchemaContext("/instanceidentifier");
+ ControllerContext.getInstance().setGlobalSchema(schemaContext);
+ final InstanceIdentifierContext<?> instanceIdentifier =
+ ControllerContext.getInstance().toInstanceIdentifier(
+ "/iid-value-module:cont-iid/iid-list/%2Fiid-value-module%3Acont-iid%2Fiid-value-module%3A"
+ + "values-iid%5Biid-value-module:value-iid='value'%5D");
+ final YangInstanceIdentifier yiD = instanceIdentifier.getInstanceIdentifier();
+ Assert.assertNotNull(yiD);
+ final PathArgument lastPathArgument = yiD.getLastPathArgument();
+ Assert.assertTrue(lastPathArgument.getNodeType().getNamespace().toString().equals("iid:value:module"));
+ Assert.assertTrue(lastPathArgument.getNodeType().getLocalName().equals("iid-list"));
+
+ final NodeIdentifierWithPredicates list = (NodeIdentifierWithPredicates) lastPathArgument;
+ final YangInstanceIdentifier value = (YangInstanceIdentifier) list.getKeyValues()
+ .get(QName.create(lastPathArgument.getNodeType(), "iid-leaf"));
+ final PathArgument lastPathArgumentOfValue = value.getLastPathArgument();
+ Assert.assertTrue(lastPathArgumentOfValue.getNodeType().getNamespace().toString().equals("iid:value:module"));
+ Assert.assertTrue(lastPathArgumentOfValue.getNodeType().getLocalName().equals("values-iid"));
+
+ final NodeIdentifierWithPredicates valueList = (NodeIdentifierWithPredicates) lastPathArgumentOfValue;
+ final String valueIid = (String) valueList.getKeyValues()
+ .get(QName.create(lastPathArgumentOfValue.getNodeType(), "value-iid"));
+ Assert.assertEquals("value", valueIid);
+ }
+
+}