From f5bc1ee541e01326d11956ff1bf0849944abfcc6 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh" Date: Tue, 12 Feb 2019 19:16:47 -0500 Subject: Add Sub Attribute parsing capabilit Change-Id: Ie353f8e5b86f7472a4790c32705f4b8c3d5e5826 Issue-ID: CCSDK-1061 Signed-off-by: Muthuramalingam, Brinda Santh --- .../core/BluePrintConstants.kt | 1 + .../controllerblueprints/core/BluePrintTypes.kt | 2 + .../core/service/PropertyAssignmentService.kt | 34 ++++++------ .../core/utils/JsonParserUtils.kt | 60 ++++++++++++++++++++++ .../core/service/BluePrintRuntimeServiceTest.kt | 10 +++- .../core/utils/JsonParserUtilsTest.kt | 33 ++++++++++++ .../src/test/resources/data/default-context.json | 6 +-- 7 files changed, 124 insertions(+), 22 deletions(-) create mode 100644 ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtils.kt create mode 100644 ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtilsTest.kt (limited to 'ms/controllerblueprints/modules/blueprint-core') diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintConstants.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintConstants.kt index a2101251e..0c8209f49 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintConstants.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintConstants.kt @@ -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" diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintTypes.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintTypes.kt index 4509cccf3..0889fddca 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintTypes.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/BluePrintTypes.kt @@ -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 } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/PropertyAssignmentService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/PropertyAssignmentService.kt index 81b7acb56..cb75e2c2f 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/PropertyAssignmentService.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/PropertyAssignmentService.kt @@ -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: [ , , , + get_attribute: [ , , , , ..., ] */ 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. , ..., ] */ 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 index 000000000..4bdaaa56d --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtils.kt @@ -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 { + return JsonPath.using(PATH_CONFIGURATION).parse(jsonContent).read(expression) + } + + fun paths(jsonNode: JsonNode, expression: String): List { + 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 diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeServiceTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeServiceTest.kt index d0bd3cf35..92e9247a2 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeServiceTest.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeServiceTest.kt @@ -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 index 000000000..1f0039999 --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JsonParserUtilsTest.kt @@ -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 diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/resources/data/default-context.json b/ms/controllerblueprints/modules/blueprint-core/src/test/resources/data/default-context.json index 3968626b8..9f733f0fd 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/test/resources/data/default-context.json +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/resources/data/default-context.json @@ -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 -- cgit 1.2.3-korg