Add Sub Attribute parsing capabilit 52/78352/2
authorMuthuramalingam, Brinda Santh <brindasanth@in.ibm.com>
Wed, 13 Feb 2019 00:16:47 +0000 (19:16 -0500)
committerBrinda Santh Muthuramalingam <brindasanth@in.ibm.com>
Wed, 13 Feb 2019 22:18:14 +0000 (22:18 +0000)
Change-Id: Ie353f8e5b86f7472a4790c32705f4b8c3d5e5826
Issue-ID: CCSDK-1061
Signed-off-by: Muthuramalingam, Brinda Santh <brindasanth@in.ibm.com>
.gitignore
components/model-catalog/blueprint-model/test-blueprint/baseconfiguration/Definitions/activation-blueprint.json
ms/blueprintsprocessor/functions/restconf-executor/src/test/resources/logback-test.xml
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintConstants.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintTypes.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/PropertyAssignmentService.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtils.kt [new file with mode: 0644]
ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeServiceTest.kt
ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtilsTest.kt [new file with mode: 0644]
ms/controllerblueprints/modules/blueprint-core/src/test/resources/data/default-context.json

index 993ab74..f116f6b 100644 (file)
@@ -21,7 +21,8 @@
 **/*.jsonld\r
 **/.checkstyle\r
 **/.gitignore\r
-\r
+
+**/*.log\r
 **/*py.class\r
 **/.DS_Store\r
 \r
index 637b589..84c78c6 100644 (file)
             "properties": {
               "login-key": "sample-key",
               "login-account": "sample-account",
-              "target-ip-address": "localhost",
+              "target-ip-address": {
+                "get_attribute": [
+                  "resource-assignment",
+                  "",
+                  "assignment-params",
+                  "$.ipAddress"
+                ]
+              },
               "port-number": 830,
               "connection-time-out": 30
             }
index 44ec746..56ea7bb 100644 (file)
@@ -1,17 +1,17 @@
 <!--
   ~ Copyright © 2017-2018 AT&T Intellectual Property.
   ~
-  ~ Licensed under the Apache License, Version 2.0 (the "License");
-  ~ you may not use this file except in compliance with the License.
-  ~ You may obtain a copy of the License at
+  ~  Licensed under the Apache License, Version 2.0 (the "License");
+  ~  you may not use this file except in compliance with the License.
+  ~  You may obtain a copy of the License at
   ~
-  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~      http://www.apache.org/licenses/LICENSE-2.0
   ~
-  ~ Unless required by applicable law or agreed to in writing, software
-  ~ distributed under the License is distributed on an "AS IS" BASIS,
-  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  ~ See the License for the specific language governing permissions and
-  ~ limitations under the License.
+  ~  Unless required by applicable law or agreed to in writing, software
+  ~  distributed under the License is distributed on an "AS IS" BASIS,
+  ~  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~  See the License for the specific language governing permissions and
+  ~  limitations under the License.
   -->
 
 <configuration>
index a210125..0c8209f 100644 (file)
@@ -44,6 +44,7 @@ object BluePrintConstants {
     const val DATA_TYPE_NULL: String = "null"
     const val DATA_TYPE_LIST: String = "list"
     const val DATA_TYPE_MAP: String = "map"
+    const val DATA_TYPE_JSON: String = "json"
 
     const val USER_SYSTEM: String = "System"
 
index 4509ccc..0889fdd 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright © 2017-2018 AT&T Intellectual Property.
+ * Modifications Copyright © 2018 IBM.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -109,6 +110,7 @@ object BluePrintTypes {
         validTypes.add(BluePrintConstants.DATA_TYPE_NULL)
         validTypes.add(BluePrintConstants.DATA_TYPE_LIST)
         validTypes.add(BluePrintConstants.DATA_TYPE_MAP)
+        validTypes.add(BluePrintConstants.DATA_TYPE_JSON)
         return validTypes
     }
 
index 81b7acb..cb75e2c 100644 (file)
@@ -27,6 +27,7 @@ import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
 import org.onap.ccsdk.apps.controllerblueprints.core.format
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
+import org.onap.ccsdk.apps.controllerblueprints.core.utils.JsonParserUtils
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.ResourceResolverUtils
 
 /**
@@ -95,11 +96,11 @@ If Property Assignment is Expression.
     }
 
     /*
-    get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
+    get_attribute: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
     <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
  */
     fun resolveAttributeExpression(nodeTemplateName: String, attributeExpression: AttributeExpression): JsonNode {
-        val valueNode: JsonNode
+        var valueNode: JsonNode
 
         val attributeName = attributeExpression.attributeName
         val subAttributeName: String? = attributeExpression.subAttributeName
@@ -114,24 +115,20 @@ If Property Assignment is Expression.
                 if (!attributeExpression.modelableEntityName.equals("SELF", true)) {
                     attributeNodeTemplateName = attributeExpression.modelableEntityName
                 }
-                /* Enable in ONAP Dublin Release
-                val nodeTemplateAttributeExpression = bluePrintContext.nodeTemplateByName(attributeNodeTemplateName).attributes?.get(attributeName)
-                        ?: throw BluePrintException(format("failed to get attribute definitions for node template " +
-                                "({})'s property name ({}) ", nodeTemplateName, attributeName))
-
-                var attributeDefinition: AttributeDefinition = bluePrintContext.nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)!!
 
-                log.info("node template name ({}), property Name ({}) resolved value ({})", attributeNodeTemplateName, attributeName, nodeTemplateAttributeExpression)
-                */
+                var attributeDefinition: AttributeDefinition = bluePrintContext
+                        .nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)
+                        ?: throw BluePrintException("failed to get attribute definitions for node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
 
                 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeNodeTemplateName, attributeName)
-                        ?: throw BluePrintException(format("failed to get node template ({})'s attribute ({}) ", nodeTemplateName, attributeName))
+                        ?: throw BluePrintException("failed to get node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
             }
 
         }
-//        subPropertyName?.let {
-//            valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
-//        }
+        if (subAttributeName != null) {
+            if (valueNode.isObject || valueNode.isArray)
+                valueNode = JsonParserUtils.parse(valueNode, subAttributeName)
+        }
         return valueNode
     }
 
@@ -140,7 +137,7 @@ If Property Assignment is Expression.
         <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
      */
     fun resolvePropertyExpression(nodeTemplateName: String, propertyExpression: PropertyExpression): JsonNode {
-        val valueNode: JsonNode
+        var valueNode: JsonNode
 
         val propertyName = propertyExpression.propertyName
         val subPropertyName: String? = propertyExpression.subPropertyName
@@ -160,9 +157,10 @@ If Property Assignment is Expression.
         // Check it it is a nested expression
         valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
 
-//        subPropertyName?.let {
-//            valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
-//        }
+        if (subPropertyName != null) {
+            if (valueNode.isObject || valueNode.isArray)
+                valueNode = JsonParserUtils.parse(valueNode, subPropertyName)
+        }
         return valueNode
     }
 
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtils.kt
new file mode 100644 (file)
index 0000000..4bdaaa5
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ *  Copyright © 2018 IBM.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.onap.ccsdk.apps.controllerblueprints.core.utils
+
+
+import com.fasterxml.jackson.databind.JsonNode
+import com.jayway.jsonpath.Configuration
+import com.jayway.jsonpath.JsonPath
+import com.jayway.jsonpath.Option
+import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider
+import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider
+
+class JsonParserUtils {
+    companion object {
+
+        //TODO("Optimise this")
+        val JACKSON_JSON_NODE_CONFIGURATION = Configuration.builder()
+                .mappingProvider(JacksonMappingProvider()).jsonProvider(JacksonJsonNodeJsonProvider()).build()
+
+        val PATH_CONFIGURATION = Configuration.builder().options(Option.AS_PATH_LIST).build()
+
+        fun paths(jsonContent: String, expression: String): List<String> {
+            return JsonPath.using(PATH_CONFIGURATION).parse(jsonContent).read(expression)
+        }
+
+        fun paths(jsonNode: JsonNode, expression: String): List<String> {
+            return paths(jsonNode.toString(), expression)
+        }
+
+        fun parse(jsonContent: String, expression: String): JsonNode {
+            return JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).read(expression)
+        }
+
+        fun parse(jsonNode: JsonNode, expression: String): JsonNode {
+            return parse(jsonNode.toString(), expression)
+        }
+
+        fun parseNSet(jsonContent: String, expression: String, value: JsonNode): JsonNode {
+            return JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).set(expression, value).json()
+        }
+
+        fun parseNSet(jsonNode: JsonNode, expression: String, valueNode: JsonNode): JsonNode {
+            return parseNSet(jsonNode.toString(), expression, valueNode)
+        }
+    }
+}
\ No newline at end of file
index d0bd3cf..92e9247 100644 (file)
@@ -65,10 +65,18 @@ class BluePrintRuntimeServiceTest {
         BluePrintRuntimeUtils.assignInputsFromClassPathFile(bluePrintRuntimeService.bluePrintContext(),
                 "data/default-context.json", executionContext)
 
+        val assignmentParams = "{\n" +
+                "            \"ipAddress\": \"127.0.0.1\",\n" +
+                "            \"hostName\": \"vnf-host\"\n" +
+                "          }"
+
+        bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params",
+                JacksonUtils.jsonNode(assignmentParams))
+
         val capProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties("sample-netconf-device",
                 "netconf")
         assertNotNull(capProperties, "Failed to populate capability property values")
-        assertEquals(capProperties["target-ip-address"], JacksonUtils.jsonNodeFromObject("localhost"), "Failed to populate parameter target-ip-address")
+        assertEquals(capProperties["target-ip-address"], "127.0.0.1".asJsonPrimitive(), "Failed to populate parameter target-ip-address")
         assertEquals(capProperties["port-number"], JacksonUtils.jsonNodeFromObject(830), "Failed to populate parameter port-number")
     }
 
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtilsTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtilsTest.kt
new file mode 100644 (file)
index 0000000..1f00399
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ *  Copyright © 2018 IBM.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.onap.ccsdk.apps.controllerblueprints.core.utils
+
+import org.junit.Test
+import org.onap.ccsdk.apps.controllerblueprints.core.asJsonPrimitive
+import kotlin.test.assertEquals
+
+class JsonParserUtilsTest {
+
+    @Test
+    fun `test parse Node`() {
+        val dataNode = JacksonUtils.jsonNodeFromClassPathFile("data/default-context.json")
+
+        val parsedNode = JsonParserUtils.parse(dataNode, "$.request-id")
+
+        assertEquals(parsedNode, "12345".asJsonPrimitive(), "failed to parse json request-id")
+    }
+}
\ No newline at end of file
index 3968626..9f733f0 100644 (file)
@@ -1,7 +1,7 @@
 {
-  "request-id" : "12345",
-  "hostname" : "localhost",
+  "request-id": "12345",
+  "hostname": "localhost",
   "template_name": "baseconfiguration",
   "template_version": "1.0.0",
-  "action-name" : "sample-action"
+  "action-name": "sample-action"
 }
\ No newline at end of file