SetNode patch

Support nested array removal with setnode

Change-Id: Ie70ef2ff7b84d35c9352c7597aef0f636128439b
Issue-ID: CCSDK-643
Signed-off-by: Smokowski, Kevin (ks6305) <kevin.smokowski@att.com>
diff --git a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SetNodeExecutor.java b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SetNodeExecutor.java
old mode 100644
new mode 100755
index 5019b56..8275a8e
--- a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SetNodeExecutor.java
+++ b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SetNodeExecutor.java
@@ -110,17 +110,13 @@
                         if (lhsPrefix.endsWith(".")) {
                             lhsPrefix = lhsPrefix.substring(0, lhsPrefix.length() - 1);
                         }
-                        int lhsPfxLength = lhsPrefix.length();
+
                         HashMap<String, String> parmsToAdd = new HashMap<String, String>();
 
                         for (String sourceVarName : ctx.getAttributeKeySet()) {
-
                             if (sourceVarName.startsWith(rhsRoot)) {
-
                                 String targetVar = lhsPrefix + "." + sourceVarName.substring(rhsRoot.length());
-
-                                LOG.debug("Copying " + sourceVarName + " value to " + targetVar);
-
+                                LOG.debug("Copying {} value to {}", sourceVarName, targetVar);
                                 parmsToAdd.put(targetVar, ctx.getAttribute(sourceVarName));
                             }
                         }
@@ -131,20 +127,26 @@
                         // If RHS is empty, unset attributes in LHS
                         LinkedList<String> parmsToRemove = new LinkedList<String>();
                         String prefix = lhsVarName + ".";
+                        String arrayPrefix = lhsVarName + "[";
                         //Clear length value in case an array exists with this prefix
                         String lengthParamName = lhsVarName + "_length";
-                        parmsToRemove.add(lengthParamName);
-                        LOG.debug("Unsetting " + lengthParamName + " because prefix " + prefix + " is being cleared.");
+                        LOG.debug("Unsetting {} because prefix {} is being cleared.", lengthParamName, prefix);
 
                         for (String curCtxVarname : ctx.getAttributeKeySet()) {
                             String curCtxVarnameMatchingValue = curCtxVarname;
                             //Special handling for reseting array values, strips out brackets and any numbers between the brackets
                             //when testing if a context memory value starts with a prefix
                             if(!prefix.contains("[") && curCtxVarnameMatchingValue.contains("[")) {
-                                curCtxVarnameMatchingValue = curCtxVarname.replaceAll(arrayPattern, "");
+                                curCtxVarnameMatchingValue = curCtxVarname.replaceAll(arrayPattern, "") + ".";
                             }
                             if (curCtxVarnameMatchingValue.startsWith(prefix)) {
-                                LOG.debug("Unsetting " + curCtxVarname + " because matching value " + curCtxVarnameMatchingValue + " starts with the prefix " + prefix);
+                                LOG.debug("Unsetting {} because matching value {} starts with the prefix {}", curCtxVarname, curCtxVarnameMatchingValue, prefix);
+                                parmsToRemove.add(curCtxVarname);
+                            }else if (curCtxVarnameMatchingValue.startsWith(lengthParamName)) {
+                            	LOG.debug("Unsetting {} because matching value {} starts with the lengthParamName {}", curCtxVarname, curCtxVarnameMatchingValue, lengthParamName);
+                                parmsToRemove.add(curCtxVarname);
+                            }else if (curCtxVarnameMatchingValue.startsWith(arrayPrefix)) {
+                            	LOG.debug("Unsetting {} because matching value {} starts with the arrayPrefix {}", curCtxVarname, curCtxVarnameMatchingValue, arrayPrefix);
                                 parmsToRemove.add(curCtxVarname);
                             }
                         }
@@ -158,16 +160,15 @@
                     String ctxValue = ctx.getAttribute(lhsVarName);
                     if ((ctxValue != null) && (ctxValue.length() > 0)) {
                         setValue = false;
-                        LOG.debug("Attribute " + lhsVarName
-                                + " already set and only-if-unset is true, so not overriding");
+                        LOG.debug("Attribute {} already set and only-if-unset is true, so not overriding", lhsVarName);
                     }
                 }
                 if (setValue) {
                     String curValue = SvcLogicExpressionResolver.evaluate(curEnt.getValue(), node, ctx);
 
                     if (LOG.isDebugEnabled()) {
-                        LOG.trace("Parameter value " + curEnt.getValue().asParsedExpr() + " resolves to " + curValue);
-                        LOG.debug("Setting context attribute " + lhsVarName + " to " + curValue);
+                        LOG.trace("Parameter value {} resolves to {}", curEnt.getValue().asParsedExpr(), curValue);
+                        LOG.debug("Setting context attribute {} to {}", lhsVarName, curValue);
                     }
                     ctx.setAttribute(lhsVarName, curValue);
                 }
diff --git a/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/SetNodeExecutorTest.java b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/SetNodeExecutorTest.java
old mode 100644
new mode 100755
index 9ba2c05..7c6f4ce
--- a/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/SetNodeExecutorTest.java
+++ b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/SetNodeExecutorTest.java
@@ -27,6 +27,10 @@
         assertNull(ctx.getAttribute("si.field1"));
         assertNull(ctx.getAttribute("si.field2"));
         assertNull(ctx.getAttribute("si.field3"));
+        assertNull(ctx.getAttribute("si.subarray[0]"));
+        assertNull(ctx.getAttribute("si.subarray[1]"));
+        assertNull(ctx.getAttribute("si.subarray[2]"));
+        assertNull(ctx.getAttribute("si.subarray_length"));
         assertEquals("6", ctx.getAttribute("search1"));
         assertEquals("KeepMe!", ctx.getAttribute("simonSays"));
     }
@@ -37,7 +41,7 @@
         SvcLogicContext ctx = new SvcLogicContext();
 
         SvcLogicParser slp = new SvcLogicParser();
-        LinkedList<SvcLogicGraph> graph = slp.parse("src/test/resources/clearArrayValues.xml");
+        LinkedList<SvcLogicGraph> graph = slp.parse("src/test/resources/clearMultipleArrayValues.xml");
         SvcLogicNode root = graph.getFirst().getRootNode();
         SvcLogicNode nodeOne = root.getOutcomeValue("1");
         SvcLogicNode nodeTwo = root.getOutcomeValue("2");
@@ -56,7 +60,7 @@
         assertEquals("6", ctx.getAttribute("search1"));
         assertEquals("KeepMe!", ctx.getAttribute("simonSays"));
     }
-    
+
     @Test
     public void clearSingleArrayProperties() throws Exception {
         SetNodeExecutor sne = new SetNodeExecutor();
@@ -72,11 +76,83 @@
         sne.execute(nodeTwo, ctx);
 
         assertNull(ctx.getAttribute("si[0].field1"));
+        assertNull(ctx.getAttribute("si[0].subarray[0]"));
+        assertNull(ctx.getAttribute("si[0].subarray[1]"));
+        assertNull(ctx.getAttribute("si[0].subarray[2]"));
+        assertNull(ctx.getAttribute("si[0].subarray_length"));
+        
+        // TODO: This is just setting up elements as null but note reducing the size of Array.
+        assertEquals("3",ctx.getAttribute("si_length"));
+        
         assertEquals("2",ctx.getAttribute("si[1].field2"));
         assertEquals("3", ctx.getAttribute("si[2].field3"));
         assertEquals("6", ctx.getAttribute("search1"));
         assertEquals("KeepMe!", ctx.getAttribute("simonSays"));
     }
+    
+    @Test
+    public void clearSingleSubArrayProperties() throws Exception {
+        SetNodeExecutor sne = new SetNodeExecutor();
+        SvcLogicContext ctx = new SvcLogicContext();
+
+        SvcLogicParser slp = new SvcLogicParser();
+        LinkedList<SvcLogicGraph> graph = slp.parse("src/test/resources/clearSingleSubArrayValues.xml");
+        SvcLogicNode root = graph.getFirst().getRootNode();
+        SvcLogicNode nodeOne = root.getOutcomeValue("1");
+        SvcLogicNode nodeTwo = root.getOutcomeValue("2");
+
+        sne.execute(nodeOne, ctx);
+        sne.execute(nodeTwo, ctx);
+
+        assertEquals("1",ctx.getAttribute("tmp.si[0].field1"));
+        assertEquals("2",ctx.getAttribute("tmp.si[1].field2"));
+        assertEquals("3", ctx.getAttribute("tmp.si[2].field3"));
+        assertEquals("3", ctx.getAttribute("tmp.si_length"));
+        
+        assertEquals("a",ctx.getAttribute("tmp.si[0].subarray[0]"));
+        
+        // TODO: This is setting up element as Empty instead null
+        //assertNull(ctx.getAttribute("tmp.si[0].subarray[1]"));
+        assertEquals("", ctx.getAttribute("tmp.si[0].subarray[1]"));
+        
+        assertEquals("c", ctx.getAttribute("tmp.si[0].subarray[2]"));
+        assertEquals("3", ctx.getAttribute("tmp.si[0].subarray_length"));
+        
+        assertEquals("x",ctx.getAttribute("tmp.si[1].subarray[0]"));
+        assertEquals("y",ctx.getAttribute("tmp.si[1].subarray[1]"));
+        assertEquals("z", ctx.getAttribute("tmp.si[1].subarray[2]"));
+        assertEquals("3", ctx.getAttribute("tmp.si[1].subarray_length"));
+        
+        assertEquals("6", ctx.getAttribute("search1"));
+        assertEquals("KeepMe!", ctx.getAttribute("simonSays"));
+    }
+    
+    @Test
+    public void clearSubArrayProperties() throws Exception {
+        SetNodeExecutor sne = new SetNodeExecutor();
+        SvcLogicContext ctx = new SvcLogicContext();
+
+        SvcLogicParser slp = new SvcLogicParser();
+        LinkedList<SvcLogicGraph> graph = slp.parse("src/test/resources/clearSubArrayValues.xml");
+        SvcLogicNode root = graph.getFirst().getRootNode();
+        SvcLogicNode nodeOne = root.getOutcomeValue("1");
+        SvcLogicNode nodeTwo = root.getOutcomeValue("2");
+
+        sne.execute(nodeOne, ctx);
+        sne.execute(nodeTwo, ctx);
+
+        assertEquals("1", ctx.getAttribute("si[0].field1"));
+        assertEquals("2",ctx.getAttribute("si[1].field2"));
+        assertEquals("3", ctx.getAttribute("si[2].field3"));
+        assertEquals("3", ctx.getAttribute("si_length"));
+        assertNull(ctx.getAttribute("si[0].subarray[0]"));
+        assertNull(ctx.getAttribute("si[0].subarray[1]"));
+        assertNull(ctx.getAttribute("si[0].subarray[2]"));
+        assertNull(ctx.getAttribute("si[0].subarray_length"));
+
+        assertEquals("6", ctx.getAttribute("search1"));
+        assertEquals("KeepMe!", ctx.getAttribute("simonSays"));
+    }
 
     @Test
     public void arrayPattern() {
@@ -107,4 +183,37 @@
         assertEquals("3", ctx.getAttribute("rootTwo.field3"));
     }
 
+    @Test
+    public void clearNestedSubArrayProperties() throws Exception { 
+        SetNodeExecutor sne = new SetNodeExecutor(); 
+        SvcLogicContext ctx = new SvcLogicContext(); 
+ 
+        SvcLogicParser slp = new SvcLogicParser(); 
+        LinkedList<SvcLogicGraph> graph = slp.parse("src/test/resources/clearNestedSubArrayValues.xml"); 
+        SvcLogicNode root = graph.getFirst().getRootNode(); 
+        SvcLogicNode nodeOne = root.getOutcomeValue("1"); 
+        SvcLogicNode nodeTwo = root.getOutcomeValue("2"); 
+ 
+        sne.execute(nodeOne, ctx); 
+        sne.execute(nodeTwo, ctx); 
+ 
+        assertEquals("1", ctx.getAttribute("tmp.si[0].field1")); 
+        assertEquals("2",ctx.getAttribute("tmp.si[1].field2")); 
+        assertEquals("3", ctx.getAttribute("tmp.si[2].field3")); 
+        assertEquals("3", ctx.getAttribute("tmp.si_length")); 
+ 
+        assertNull(ctx.getAttribute("tmp.si[0].subarray[0]")); 
+        assertNull(ctx.getAttribute("tmp.si[0].subarray[1]")); 
+        assertNull(ctx.getAttribute("tmp.si[0].subarray[2]")); 
+        assertNull(ctx.getAttribute("tmp.si[0].subarray_length")); 
+        
+        assertEquals("x", ctx.getAttribute("tmp.si[1].subarray[0]")); 
+        assertEquals("y",ctx.getAttribute("tmp.si[1].subarray[1]")); 
+        assertEquals("z", ctx.getAttribute("tmp.si[1].subarray[2]")); 
+        assertEquals("3", ctx.getAttribute("tmp.si[1].subarray_length")); 
+ 
+        assertEquals("6", ctx.getAttribute("search1")); 
+        assertEquals("KeepMe!", ctx.getAttribute("simonSays")); 
+    }
+
 }
diff --git a/sli/provider/src/test/resources/clearArrayValues.xml b/sli/provider/src/test/resources/clearMultipleArrayValues.xml
similarity index 100%
rename from sli/provider/src/test/resources/clearArrayValues.xml
rename to sli/provider/src/test/resources/clearMultipleArrayValues.xml
diff --git a/sli/provider/src/test/resources/clearNestedSubArrayValues.xml b/sli/provider/src/test/resources/clearNestedSubArrayValues.xml
new file mode 100644
index 0000000..a80b3e5
--- /dev/null
+++ b/sli/provider/src/test/resources/clearNestedSubArrayValues.xml
@@ -0,0 +1,27 @@
+<service-logic 
+    xmlns='http://www.onap.org/sdnc/svclogic' 
+    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.onap.org/sdnc/svclogic ./svclogic.xsd' module='TEST-DG' version='1.0.0'> 
+    <method rpc='test-dg' mode='sync'> 
+        <block> 
+            <set> 
+                <parameter name='tmp.si[0].field1' value='1' /> 
+                <parameter name='tmp.si[1].field2' value='2' /> 
+                <parameter name='tmp.si[2].field3' value='3' /> 
+                <parameter name='tmp.si_length' value='3' /> 
+                <parameter name='tmp.si[0].subarray[0]' value='a' /> 
+                <parameter name='tmp.si[0].subarray[1]' value='b' /> 
+                <parameter name='tmp.si[0].subarray[2]' value='c' /> 
+                <parameter name='tmp.si[0].subarray_length' value='3' /> 
+                <parameter name='tmp.si[1].subarray[0]' value='x' /> 
+                <parameter name='tmp.si[1].subarray[1]' value='y' /> 
+                <parameter name='tmp.si[1].subarray[2]' value='z' /> 
+                <parameter name='tmp.si[1].subarray_length' value='3' /> 
+                <parameter name='search1' value='6' /> 
+                <parameter name='simonSays' value='KeepMe!' /> 
+            </set> 
+            <set> 
+                <parameter name='tmp.si[0].subarray.' value='' /> 
+            </set> 
+        </block> 
+    </method> 
+</service-logic>
\ No newline at end of file
diff --git a/sli/provider/src/test/resources/clearSingleArrayValues.xml b/sli/provider/src/test/resources/clearSingleArrayValues.xml
index 3e4e5d9..56781a0 100644
--- a/sli/provider/src/test/resources/clearSingleArrayValues.xml
+++ b/sli/provider/src/test/resources/clearSingleArrayValues.xml
@@ -7,6 +7,11 @@
                 <parameter name='si[0].field1' value='1' />

                 <parameter name='si[1].field2' value='2' />

                 <parameter name='si[2].field3' value='3' />

+                <parameter name='si_length' value='3' />

+                <parameter name='si[0].subarray[0]' value='a' /> 

+                <parameter name='si[0].subarray[1]' value='b' /> 

+                <parameter name='si[0].subarray[2]' value='c' /> 

+                <parameter name='si[0].subarray_length' value='3' />

                 <parameter name='search1' value='6' />

                 <parameter name='simonSays' value='KeepMe!' />

             </set>

diff --git a/sli/provider/src/test/resources/clearSingleSubArrayValues.xml b/sli/provider/src/test/resources/clearSingleSubArrayValues.xml
new file mode 100644
index 0000000..22f14f3
--- /dev/null
+++ b/sli/provider/src/test/resources/clearSingleSubArrayValues.xml
@@ -0,0 +1,27 @@
+<service-logic

+    xmlns='http://www.onap.org/sdnc/svclogic'

+    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.onap.org/sdnc/svclogic ./svclogic.xsd' module='TEST-DG' version='1.0.0'>

+    <method rpc='test-dg' mode='sync'>

+        <block>

+            <set>

+                <parameter name='tmp.si[0].field1' value='1' /> 

+                <parameter name='tmp.si[1].field2' value='2' /> 

+                <parameter name='tmp.si[2].field3' value='3' /> 

+                <parameter name='tmp.si_length' value='3' /> 

+                <parameter name='tmp.si[0].subarray[0]' value='a' /> 

+                <parameter name='tmp.si[0].subarray[1]' value='b' /> 

+                <parameter name='tmp.si[0].subarray[2]' value='c' /> 

+                <parameter name='tmp.si[0].subarray_length' value='3' /> 

+                <parameter name='tmp.si[1].subarray[0]' value='x' /> 

+                <parameter name='tmp.si[1].subarray[1]' value='y' /> 

+                <parameter name='tmp.si[1].subarray[2]' value='z' /> 

+                <parameter name='tmp.si[1].subarray_length' value='3' /> 

+                <parameter name='search1' value='6' /> 

+                <parameter name='simonSays' value='KeepMe!' /> 

+            </set>

+            <set>

+                <parameter name='tmp.si[0].subarray[1]' value='' />

+            </set>

+        </block>

+    </method>

+</service-logic>
\ No newline at end of file
diff --git a/sli/provider/src/test/resources/clearSubArrayValues.xml b/sli/provider/src/test/resources/clearSubArrayValues.xml
new file mode 100644
index 0000000..cb25f38
--- /dev/null
+++ b/sli/provider/src/test/resources/clearSubArrayValues.xml
@@ -0,0 +1,24 @@
+<service-logic
+    xmlns='http://www.onap.org/sdnc/svclogic'
+    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.onap.org/sdnc/svclogic ./svclogic.xsd' module='TEST-DG' version='1.0.0'>
+    <method rpc='test-dg' mode='sync'>
+        <block>
+            <set>
+                <parameter name='si[0].field1' value='1' />
+                <parameter name='si[1].field2' value='2' />
+                <parameter name='si[2].field3' value='3' />
+                <parameter name='si_length' value='3' />
+                <parameter name='si[0].subarray[0]' value='a' />
+                <parameter name='si[0].subarray[1]' value='b' />
+                <parameter name='si[0].subarray[2]' value='c' />
+                <parameter name='si[0].subarray_length' value='3' />
+                <parameter name='search1' value='6' />
+                <parameter name='simonSays' value='KeepMe!' />
+            </set>
+            <set>
+                <parameter name='si[0].subarray.' value='' />
+                <!-- <parameter name='si.subarray.' value='' /> -->
+            </set>
+        </block>
+    </method>
+</service-logic>
\ No newline at end of file
diff --git a/sli/provider/src/test/resources/clearValues.xml b/sli/provider/src/test/resources/clearValues.xml
index 615c856..dc7f5c8 100644
--- a/sli/provider/src/test/resources/clearValues.xml
+++ b/sli/provider/src/test/resources/clearValues.xml
@@ -7,6 +7,10 @@
                 <parameter name='si.field1' value='1' />

                 <parameter name='si.field2' value='2' />

                 <parameter name='si.field3' value='3' />

+                <parameter name='si.subarray[0]' value='a' />

+                <parameter name='si.subarray[1]' value='b' />

+                <parameter name='si.subarray[2]' value='c' />

+                <parameter name='si.subarray_length' value='3' />

                 <parameter name='search1' value='6' />

                 <parameter name='simonSays' value='KeepMe!' />

             </set>