From: Jozef Gloncak Date: Tue, 5 Aug 2014 08:59:55 +0000 (+0200) Subject: BUG 1162 - problem to post list data X-Git-Tag: release/helium~317 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F84%2F9684%2F3;p=controller.git BUG 1162 - problem to post list data If last path arguments of instance identifier are list with keys then in POST operation it is necessary to append it to instance identifier as nodeWithKey. This code was part of RestconfImpl but it was probably deleted accidentaly. Change-Id: I117cbcc39dcb97e8bc5ecc852ce2e9e7da7070cf Signed-off-by: Jozef Gloncak --- diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java index 73ca02c505..fac6c80564 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java @@ -632,7 +632,8 @@ public class RestconfImpl implements RestconfService { NormalizedNode data = null; YangInstanceIdentifier normalizedII; if (mountPoint != null) { - normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData.getInstanceIdentifier()); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); data = broker.readConfigurationData(mountPoint, normalizedII); } else { normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); @@ -695,7 +696,8 @@ public class RestconfImpl implements RestconfService { NormalizedNode data = null; YangInstanceIdentifier normalizedII; if (mountPoint != null) { - normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData.getInstanceIdentifier()); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); data = broker.readOperationalData(mountPoint, normalizedII); } else { normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); @@ -730,7 +732,8 @@ public class RestconfImpl implements RestconfService { try { if (mountPoint != null) { - normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData.getInstanceIdentifier()); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); broker.commitConfigurationDataPut(mountPoint, normalizedII, datastoreNormalizedNode).get(); } else { normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); @@ -842,7 +845,8 @@ public class RestconfImpl implements RestconfService { try { if (mountPoint != null) { - normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData.getInstanceIdentifier()); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); broker.commitConfigurationDataPost(mountPoint, normalizedII, datastoreNormalizedData); } else { normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); @@ -886,7 +890,8 @@ public class RestconfImpl implements RestconfService { try { if (mountPoint != null) { - normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData.getInstanceIdentifier()); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); broker.commitConfigurationDataPost(mountPoint, normalizedII, datastoreNormalizedData); } else { @@ -908,7 +913,8 @@ public class RestconfImpl implements RestconfService { try { if (mountPoint != null) { - normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData.getInstanceIdentifier()); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); broker.commitConfigurationDataDelete(mountPoint, normalizedII); } else { normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); @@ -1082,7 +1088,12 @@ public class RestconfImpl implements RestconfService { iiBuilder = YangInstanceIdentifier.builder(iiOriginal); } - iiBuilder.node(schemaOfData.getQName()); + if ((schemaOfData instanceof ListSchemaNode)) { + HashMap keys = this.resolveKeysFromData(((ListSchemaNode) schemaOfData), data); + iiBuilder.nodeWithKey(schemaOfData.getQName(), keys); + } else { + iiBuilder.node(schemaOfData.getQName()); + } YangInstanceIdentifier instance = iiBuilder.toInstance(); DOMMountPoint mountPoint = null; @@ -1093,6 +1104,34 @@ public class RestconfImpl implements RestconfService { return new InstanceIdWithSchemaNode(instance, schemaOfData, mountPoint); } + private HashMap resolveKeysFromData(final ListSchemaNode listNode, final CompositeNode dataNode) { + final HashMap keyValues = new HashMap(); + List _keyDefinition = listNode.getKeyDefinition(); + for (final QName key : _keyDefinition) { + SimpleNode head = null; + String localName = key.getLocalName(); + List> simpleNodesByName = dataNode.getSimpleNodesByName(localName); + if (simpleNodesByName != null) { + head = Iterables.getFirst(simpleNodesByName, null); + } + + Object dataNodeKeyValueObject = null; + if (head != null) { + dataNodeKeyValueObject = head.getValue(); + } + + if (dataNodeKeyValueObject == null) { + throw new RestconfDocumentedException("Data contains list \"" + dataNode.getNodeType().getLocalName() + + "\" which does not contain key: \"" + key.getLocalName() + "\"", ErrorType.PROTOCOL, + ErrorTag.INVALID_VALUE); + } + + keyValues.put(key, dataNodeKeyValueObject); + } + + return keyValues; + } + private boolean endsWithMountPoint(final String identifier) { return identifier.endsWith(ControllerContext.MOUNT) || identifier.endsWith(ControllerContext.MOUNT + "/"); } @@ -1431,7 +1470,8 @@ public class RestconfImpl implements RestconfService { "It wasn't possible to correctly interpret data.")); } - private NormalizedNode compositeNodeToDatastoreNormalizedNode(final CompositeNode compNode, final DataSchemaNode schema) { + private NormalizedNode compositeNodeToDatastoreNormalizedNode(final CompositeNode compNode, + final DataSchemaNode schema) { List> lst = new ArrayList>(); lst.add(compNode); if (schema instanceof ContainerSchemaNode) { @@ -1448,7 +1488,8 @@ public class RestconfImpl implements RestconfService { "It wasn't possible to translate specified data to datastore readable form.")); } - private InstanceIdWithSchemaNode normalizeInstanceIdentifierWithSchemaNode(final InstanceIdWithSchemaNode iiWithSchemaNode) { + private InstanceIdWithSchemaNode normalizeInstanceIdentifierWithSchemaNode( + final InstanceIdWithSchemaNode iiWithSchemaNode) { return normalizeInstanceIdentifierWithSchemaNode(iiWithSchemaNode, false); } @@ -1459,8 +1500,8 @@ public class RestconfImpl implements RestconfService { iiWithSchemaNode.getMountPoint()); } - private YangInstanceIdentifier instanceIdentifierToReadableFormForNormalizeNode(final YangInstanceIdentifier instIdentifier, - final boolean unwrapLastListNode) { + private YangInstanceIdentifier instanceIdentifierToReadableFormForNormalizeNode( + final YangInstanceIdentifier instIdentifier, final boolean unwrapLastListNode) { Preconditions.checkNotNull(instIdentifier, "Instance identifier can't be null"); final List result = new ArrayList(); final Iterator iter = instIdentifier.getPathArguments().iterator();