Bug 3104 - Sal Rest Connector: Data already exists for path when adding
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonNormalizedNodeBodyReader.java
index 5d63c9c01162733a2b0734f17f5e0890a3346370..bacd38720cc1d83196acc652dbe5f0c4cdfc5e92 100644 (file)
@@ -14,6 +14,8 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
@@ -27,9 +29,11 @@ import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
@@ -88,23 +92,35 @@ public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsPr
             final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
             jsonParser.parse(reader);
 
-            NormalizedNode<?, ?> partialResult = resultHolder.getResult();
-            final NormalizedNode<?, ?> result;
+            NormalizedNode<?, ?> result = resultHolder.getResult();
+            List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
+            InstanceIdentifierContext<SchemaNode> newIIContext;
 
-            // FIXME: Also II should be updated unwrap result from augmentation and choice nodes on PUT
-            if (!isPost()) {
-                while (partialResult instanceof AugmentationNode || partialResult instanceof ChoiceNode) {
-                    final Object childNode = ((DataContainerNode) partialResult).getValue().iterator().next();
-                    partialResult = (NormalizedNode<?, ?>) childNode;
+            if (isPost()) {
+                while (result instanceof AugmentationNode || result instanceof ChoiceNode) {
+                    final Object childNode = ((DataContainerNode) result).getValue().iterator().next();
+                    iiToDataList.add(result.getIdentifier());
+                    result = (NormalizedNode<?, ?>) childNode;
+                }
+                if (result instanceof MapEntryNode) {
+                    iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getNodeType()));
+                    iiToDataList.add(result.getIdentifier());
+                } else {
+                    iiToDataList.add(result.getIdentifier());
                 }
-            }
-
-            if (partialResult instanceof MapNode && !isPost()) {
-                result = Iterables.getOnlyElement(((MapNode) partialResult).getValue());
             } else {
-                result = partialResult;
+                if (result instanceof MapNode) {
+                    result = Iterables.getOnlyElement(((MapNode) result).getValue());
+                }
             }
-            return new NormalizedNodeContext(path,result);
+
+            YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(
+                            path.getInstanceIdentifier().getPathArguments(), iiToDataList));
+
+            newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(),
+                    path.getSchemaContext());
+
+            return new NormalizedNodeContext(newIIContext, result);
         } catch (final RestconfDocumentedException e) {
             throw e;
         } catch (final ResultAlreadySetException e) {