Bug 5897 - PATCH merge operation does nothing 61/42461/11
authorIvan Hrasko <ihrasko@cisco.com>
Mon, 25 Jul 2016 10:43:38 +0000 (12:43 +0200)
committerIvan Hrasko <ihrasko@cisco.com>
Fri, 29 Jul 2016 08:17:33 +0000 (08:17 +0000)
- added merge patch operation processing

Change-Id: Ic3f5908c5f016a0680779ed12b4bc26b7b840d9a
Signed-off-by: Ivan Hrasko <ihrasko@cisco.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/BrokerFacade.java
restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonPATCHBodyReader.java
restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlPATCHBodyReader.java
restconf/sal-rest-connector/src/test/resources/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json [new file with mode: 0644]
restconf/sal-rest-connector/src/test/resources/instanceidentifier/json/jsonPATCHMergeOperationOnList.json [new file with mode: 0644]
restconf/sal-rest-connector/src/test/resources/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml [new file with mode: 0644]
restconf/sal-rest-connector/src/test/resources/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml [new file with mode: 0644]

index 37c90515abab3c33cff7f38ecb2f06a3e881b17b..bb69bacc31568e6108d0ec0293d5f64f43421c44 100644 (file)
@@ -202,6 +202,20 @@ public class BrokerFacade {
                         }
                     }
                     break;
+                case MERGE:
+                    if (errorCounter == 0) {
+                        try {
+                            mergeDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(),
+                                    patchEntity.getNode(), globalSchema);
+                            editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
+                        } catch (RestconfDocumentedException e) {
+                            editErrors = new ArrayList<>();
+                            editErrors.addAll(e.getErrors());
+                            editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
+                            errorCounter++;
+                        }
+                    }
+                    break;
             }
         }
 
@@ -396,6 +410,20 @@ public class BrokerFacade {
         writeTransaction.delete(datastore, path);
     }
 
+    private void mergeDataWithinTransaction(
+            final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
+            final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
+        LOG.trace("Merge {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
+        ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
+
+        // merging is necessary only for lists otherwise we can call put method
+        if (payload instanceof MapNode) {
+            writeTransaction.merge(datastore, path, payload);
+        } else {
+            writeTransaction.put(datastore, path, payload);
+        }
+    }
+
     public void setDomDataBroker(final DOMDataBroker domDataBroker) {
         this.domDataBroker = domDataBroker;
     }
index ab48d5a02f37fdf08e4e80250c355d51ab937615..b999c036cc8ad163906f64bfc510212edeedb3e5 100644 (file)
@@ -126,4 +126,36 @@ public class TestJsonPATCHBodyReader extends AbstractBodyReaderTest {
                 .readFrom(null, null, null, mediaType, null, inputStream);
         checkPATCHContext(returnValue);
     }
+
+    /**
+     * Test of Yang PATCH merge operation on list. Test consists of two edit operations - replace and merge.
+     */
+    @Test
+    public void modulePATCHMergeOperationOnListTest() throws Exception {
+        final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
+        mockBodyReader(uri, jsonPATCHBodyReader, false);
+
+        final InputStream inputStream = TestJsonBodyReader.class
+                .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
+
+        final PATCHContext returnValue = jsonPATCHBodyReader
+                .readFrom(null, null, null, mediaType, null, inputStream);
+        checkPATCHContext(returnValue);
+    }
+
+    /**
+     * Test of Yang PATCH merge operation on container. Test consists of two edit operations - create and merge.
+     */
+    @Test
+    public void modulePATCHMergeOperationOnContainerTest() throws Exception {
+        final String uri = "instance-identifier-patch-module:patch-cont";
+        mockBodyReader(uri, jsonPATCHBodyReader, false);
+
+        final InputStream inputStream = TestJsonBodyReader.class
+                .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
+
+        final PATCHContext returnValue = jsonPATCHBodyReader
+                .readFrom(null, null, null, mediaType, null, inputStream);
+        checkPATCHContext(returnValue);
+    }
 }
index 3eef699171c12ba67ec531c522aa131f9fdf06b0..4768a0c9e8a5375b4568850a2181d2d65a920c61 100644 (file)
@@ -115,4 +115,32 @@ public class TestXmlPATCHBodyReader extends AbstractBodyReaderTest {
                 .readFrom(null, null, null, mediaType, null, inputStream);
         checkPATCHContext(returnValue);
     }
+
+    /**
+     * Test of Yang PATCH merge operation on list. Test consists of two edit operations - replace and merge.
+     */
+    @Test
+    public void moduleDataMergeOperationOnListTest() throws Exception {
+        final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
+        mockBodyReader(uri, xmlPATCHBodyReader, false);
+        final InputStream inputStream = TestXmlBodyReader.class
+                .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
+        final PATCHContext returnValue = xmlPATCHBodyReader
+                .readFrom(null, null, null, mediaType, null, inputStream);
+        checkPATCHContext(returnValue);
+    }
+
+    /**
+     * Test of Yang PATCH merge operation on container. Test consists of two edit operations - create and merge.
+     */
+    @Test
+    public void moduleDataMergeOperationOnContainerTest() throws Exception {
+        final String uri = "instance-identifier-patch-module:patch-cont";
+        mockBodyReader(uri, xmlPATCHBodyReader, false);
+        final InputStream inputStream = TestXmlBodyReader.class
+                .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
+        final PATCHContext returnValue = xmlPATCHBodyReader
+                .readFrom(null, null, null, mediaType, null, inputStream);
+        checkPATCHContext(returnValue);
+    }
 }
diff --git a/restconf/sal-rest-connector/src/test/resources/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json b/restconf/sal-rest-connector/src/test/resources/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json
new file mode 100644 (file)
index 0000000..1483920
--- /dev/null
@@ -0,0 +1,43 @@
+{
+  "ietf-yang-patch:yang-patch" : {
+    "patch-id" : "Test merge operation",
+    "comment" : "This is test patch for merge operation on container",
+    "edit" : [
+      {
+        "edit-id": "edit1",
+        "operation": "create",
+        "target": "/",
+        "value": {
+          "patch-cont": {
+            "my-list1": [
+              {
+                "name": "my-list1 - A",
+                "my-leaf11": "I am leaf11-0",
+                "my-leaf12": "I am leaf12-1"
+              },
+              {
+                "name": "my-list1 - B",
+                "my-leaf11": "I am leaf11-0",
+                "my-leaf12": "I am leaf12-1"
+              }
+            ]
+          }
+        }
+      },
+      {
+        "edit-id": "edit2",
+        "operation": "merge",
+        "target": "/",
+        "value": {
+          "patch-cont": {
+            "my-list1": {
+              "name": "my-list1 - Merged",
+              "my-leaf11": "I am leaf11-0",
+              "my-leaf12": "I am leaf12-1"
+            }
+          }
+        }
+      }
+    ]
+  }
+}
\ No newline at end of file
diff --git a/restconf/sal-rest-connector/src/test/resources/instanceidentifier/json/jsonPATCHMergeOperationOnList.json b/restconf/sal-rest-connector/src/test/resources/instanceidentifier/json/jsonPATCHMergeOperationOnList.json
new file mode 100644 (file)
index 0000000..3b809e0
--- /dev/null
@@ -0,0 +1,32 @@
+{
+  "ietf-yang-patch:yang-patch" : {
+    "patch-id" : "Test merge operation",
+    "comment" : "This is test patch for merge operation on list",
+    "edit" : [
+      {
+        "edit-id": "edit1",
+        "operation": "replace",
+        "target": "/instance-identifier-patch-module:my-list2[instance-identifier-patch-module:name='my-leaf20']",
+        "value": {
+          "my-list2": {
+            "name": "my-leaf20",
+            "my-leaf21": "I am leaf21-0",
+            "my-leaf22": "I am leaf22-0"
+          }
+        }
+      },
+      {
+        "edit-id": "edit2",
+        "operation": "merge",
+        "target": "/instance-identifier-patch-module:my-list2[instance-identifier-patch-module:name='my-leaf21']",
+        "value": {
+          "my-list2": {
+            "name": "my-leaf21",
+            "my-leaf21": "I am leaf21-1",
+            "my-leaf22": "I am leaf22-1"
+          }
+        }
+      }
+    ]
+  }
+}
\ No newline at end of file
diff --git a/restconf/sal-rest-connector/src/test/resources/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml b/restconf/sal-rest-connector/src/test/resources/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml
new file mode 100644 (file)
index 0000000..afa35bd
--- /dev/null
@@ -0,0 +1,44 @@
+<!--
+  ~ 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
+  -->
+<yang-patch xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
+    <patch-id>Test merge operation</patch-id>
+    <comment>This is test patch for merge operation on container</comment>
+    <edit>
+        <edit-id>edit1</edit-id>
+        <operation>create</operation>
+        <target>/</target>
+        <value>
+            <patch-cont xmlns="instance:identifier:patch:module">
+                <my-list1>
+                    <name>my-list1 - A</name>
+                    <my-leaf11>I am leaf11-0</my-leaf11>
+                    <my-leaf12>I am leaf12-1</my-leaf12>
+                </my-list1>
+                <my-list1>
+                    <name>my-list1 - B</name>
+                    <my-leaf11>I am leaf11-0</my-leaf11>
+                    <my-leaf12>I am leaf12-1</my-leaf12>
+                </my-list1>
+            </patch-cont>
+        </value>
+    </edit>
+    <edit>
+        <edit-id>edit2</edit-id>
+        <operation>merge</operation>
+        <target>/</target>
+        <value>
+            <patch-cont xmlns="instance:identifier:patch:module">
+                <my-list1>
+                    <name>my-list1 - Merged</name>
+                    <my-leaf11>I am leaf11-0</my-leaf11>
+                    <my-leaf12>I am leaf12-1</my-leaf12>
+                </my-list1>
+            </patch-cont>
+        </value>
+    </edit>
+</yang-patch>
\ No newline at end of file
diff --git a/restconf/sal-rest-connector/src/test/resources/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml b/restconf/sal-rest-connector/src/test/resources/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml
new file mode 100644 (file)
index 0000000..ad13041
--- /dev/null
@@ -0,0 +1,35 @@
+<!--
+  ~ 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
+  -->
+<yang-patch xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
+    <patch-id>Test merge operation</patch-id>
+    <comment>This is test patch for merge operation on list</comment>
+    <edit>
+        <edit-id>edit1</edit-id>
+        <operation>replace</operation>
+        <target>/my-list2</target>
+        <value>
+            <my-list2 xmlns="instance:identifier:patch:module">
+                <name>my-leaf20</name>
+                <my-leaf21>I am leaf21-0</my-leaf21>
+                <my-leaf22>I am leaf22-0</my-leaf22>
+            </my-list2>
+        </value>
+    </edit>
+    <edit>
+        <edit-id>edit2</edit-id>
+        <operation>merge</operation>
+        <target>/my-list2</target>
+        <value>
+            <my-list2 xmlns="instance:identifier:patch:module">
+                <name>my-leaf21</name>
+                <my-leaf21>I am leaf21-1</my-leaf21>
+                <my-leaf22>I am leaf22-1</my-leaf22>
+            </my-list2>
+        </value>
+    </edit>
+</yang-patch>
\ No newline at end of file