// it could be identityref Built-In Type
URI namespace = getNamespaceFor(value);
if (namespace != null) {
- return new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null);
+ return new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null,value);
}
// it is not "prefix:value" but just "value"
return value;
if (xPathParts.length < 2) { // must be at least "/pr:node"
return null;
}
- IdentityValuesDTO identityValuesDTO = new IdentityValuesDTO();
+ IdentityValuesDTO identityValuesDTO = new IdentityValuesDTO(value);
for (int i = 1; i < xPathParts.length; i++) {
String xPathPartTrimmed = xPathParts[i].trim();
if (namespaceAndValue.length == 2) {
String namespace = startElement.getNamespaceContext().getNamespaceURI(namespaceAndValue[0]);
if (namespace != null && !namespace.isEmpty()) {
- return new IdentityValuesDTO(namespace, namespaceAndValue[1], namespaceAndValue[0]);
+ return new IdentityValuesDTO(namespace, namespaceAndValue[1], namespaceAndValue[0],value);
}
}
// it is not "prefix:value" but just "value"
public final class IdentityValuesDTO {
private final List<IdentityValue> elementData = new ArrayList<>();
+ private final String originValue;
- public IdentityValuesDTO(String namespace, String value, String prefix) {
+ public IdentityValuesDTO(String namespace, String value, String prefix,String originValue) {
elementData.add(new IdentityValue(namespace, value, prefix));
+ this.originValue = originValue;
+ }
+
+ public IdentityValuesDTO(String originValue) {
+ this.originValue = originValue;
}
public IdentityValuesDTO() {
-
+ originValue = null;
}
public void add(String namespace, String value, String prefix) {
public String toString() {
return elementData.toString();
}
+
+ public String getOriginValue() {
+ return originValue;
+ }
public static final class IdentityValue {
input == null ? "null" : input.getClass(), String.valueOf(input));
return null;
} else if (type instanceof LeafrefTypeDefinition) {
+ if (input instanceof IdentityValuesDTO) {
+ return LEAFREF_DEFAULT_CODEC.deserialize(((IdentityValuesDTO)input).getOriginValue());
+ }
return LEAFREF_DEFAULT_CODEC.deserialize(input);
} else if (type instanceof InstanceIdentifierTypeDefinition) {
if (input instanceof IdentityValuesDTO) {
TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> 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()
@Override
public IdentityValuesDTO serialize(QName data) {
- return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix());
+ return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix(),null);
}
@Override
if (schema.typeDefinition instanceof IdentityrefTypeDefinition) {
if (value instanceof String) {
- inputValue = new IdentityValuesDTO(nodeBuilder.namespace.toString, value as String, null)
+ inputValue = new IdentityValuesDTO(nodeBuilder.namespace.toString, value as String, null,value as String);
} // else value is already instance of IdentityValuesDTO
}
--- /dev/null
+/*
+ * Copyright (c) 2014 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.restconf.impl.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+
+import javax.ws.rs.WebApplicationException;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
+import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+
+public class XmlAndJsonToCnSnLeafRefTest extends YangAndXmlAndDataSchemaLoader {
+
+ @BeforeClass
+ public static void initialize() {
+ dataLoad("/leafref/yang", 2, "leafref-module", "cont");
+ }
+
+ @Test
+ public void loadXmlToCnSn() throws WebApplicationException, IOException, URISyntaxException {
+ CompositeNode cnSn = TestUtils.readInputToCnSn("/leafref/xml/xmldata.xml", XmlToCompositeNodeProvider.INSTANCE);
+ TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
+ verifyContPredicate(cnSn, "/ns:cont/ns:lf1", "/cont/lf1", "/ns:cont/ns:lf1", "../lf1");
+ }
+
+ @Test
+ public void loadJsonToCnSn() throws WebApplicationException, IOException, URISyntaxException {
+ CompositeNode cnSn = TestUtils.readInputToCnSn("/leafref/json/jsondata.json",
+ JsonToCompositeNodeProvider.INSTANCE);
+ TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
+ verifyContPredicate(cnSn, "/leafref-module:cont/leafref-module:lf1", "/leafref-module:cont/leafref-module:lf1",
+ "/referenced-module:cont/referenced-module:lf1", "/leafref-module:cont/leafref-module:lf1");
+ }
+
+ private void verifyContPredicate(CompositeNode cnSn, String... values) throws URISyntaxException {
+ Object lf2Value = null;
+ Object lf3Value = null;
+ Object lf4Value = null;
+ Object lf5Value = null;
+
+ for (Node<?> node : cnSn.getValue()) {
+ if (node.getNodeType().getLocalName().equals("lf2")) {
+ lf2Value = ((SimpleNode<?>) node).getValue();
+ } else if (node.getNodeType().getLocalName().equals("lf3")) {
+ lf3Value = ((SimpleNode<?>) node).getValue();
+ } else if (node.getNodeType().getLocalName().equals("lf4")) {
+ lf4Value = ((SimpleNode<?>) node).getValue();
+ } else if (node.getNodeType().getLocalName().equals("lf5")) {
+ lf5Value = ((SimpleNode<?>) node).getValue();
+ }
+ }
+ assertEquals(values[0], lf2Value);
+ assertEquals(values[1], lf3Value);
+ assertEquals(values[2], lf4Value);
+ assertEquals(values[3], lf5Value);
+ }
+
+}
--- /dev/null
+{
+ "leafref-module:cont" : {
+ "lf4" : "/referenced-module:cont/referenced-module:lf1",
+ "lf2" : "/leafref-module:cont/leafref-module:lf1",
+ "lf3" : "/leafref-module:cont/leafref-module:lf1",
+ "lf5" : "/leafref-module:cont/leafref-module:lf1"
+ }
+}
\ No newline at end of file
--- /dev/null
+<cont xmlns="leafref:module">
+ <lf4 xmlns:ns="referenced:module">/ns:cont/ns:lf1</lf4>
+ <lf2 xmlns:ns="leafref:module">/ns:cont/ns:lf1</lf2>
+ <lf3 xmlns:ns="leafref:module">/cont/lf1</lf3>
+ <lf5 xmlns:ns="leafref:module">../lf1</lf5>
+</cont>
--- /dev/null
+module leafref-module {
+ namespace "leafref:module";
+
+
+ prefix "lfrfmodule";
+
+ import referenced-module { prefix refmod; revision-date 2014-04-17;}
+
+
+ revision 2014-04-17 {
+ }
+
+
+ container cont {
+ leaf lf1 {
+ type instance-identifier;
+ }
+
+ leaf lf2 {
+ type leafref {
+ path "../lf1";
+ }
+ }
+
+ leaf lf3 {
+ type leafref {
+ path "/refmod:cont/refmod:lf1";
+ }
+ }
+
+ leaf lf4 {
+ type leafref {
+ path "/cont/lf1";
+ }
+ }
+
+ leaf lf5 {
+ type leafref {
+ path "../lf1";
+ }
+ }
+
+ }
+}
\ No newline at end of file
--- /dev/null
+module referenced-module {
+ namespace "referenced:module";
+
+ prefix "refmodule";
+ revision 2014-04-17 {
+ }
+
+ container cont {
+ leaf lf1 {
+ type instance-identifier;
+ }
+ }
+}
\ No newline at end of file