diff options
author | Dan Timoney <dt5972@att.com> | 2018-09-04 14:49:27 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-09-04 14:49:27 +0000 |
commit | b20dd093cf58a39792395f8ba05bfa00a76f4eec (patch) | |
tree | 55e6982861eb18f67e1b4d8c94ec4f3be9b48941 /ms/controllerblueprints/modules/core | |
parent | b79c738377b562b059c2926256c8b499444f1c1e (diff) | |
parent | 2ae44709f7e3b04d2150e6c15182fae2a39adc45 (diff) |
Merge "Controller Blueprints Microservice"
Diffstat (limited to 'ms/controllerblueprints/modules/core')
4 files changed, 119 insertions, 37 deletions
diff --git a/ms/controllerblueprints/modules/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/PropertyAssignmentService.kt b/ms/controllerblueprints/modules/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/PropertyAssignmentService.kt index 9389a6f3..ea8d4911 100644 --- a/ms/controllerblueprints/modules/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/PropertyAssignmentService.kt +++ b/ms/controllerblueprints/modules/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/PropertyAssignmentService.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.
@@ -21,6 +22,7 @@ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.node.NullNode
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.ResourceResolverUtils
import org.slf4j.Logger
@@ -45,7 +47,7 @@ If Property Assignment is Expression. fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,
assignment: Any): JsonNode {
- var valueNode: JsonNode = NullNode.getInstance()
+ val valueNode: JsonNode
logger.trace("Assignment ({})", assignment)
val expressionData = BluePrintExpressionService.getExpressionData(assignment)
@@ -96,7 +98,7 @@ If Property Assignment is Expression. <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
*/
fun resolveAttributeExpression(nodeTemplateName: String, attributeExpression: AttributeExpression): JsonNode {
- var valueNode: JsonNode = NullNode.getInstance()
+ val valueNode: JsonNode
val attributeName = attributeExpression.attributeName
val subAttributeName: String? = attributeExpression.subAttributeName
@@ -106,15 +108,15 @@ If Property Assignment is Expression. attributeNodeTemplateName = attributeExpression.modelableEntityName
}
- val attributeExpression = bluePrintContext.nodeTemplateByName(attributeNodeTemplateName).attributes?.get(attributeName)
+ val nodeTemplateAttributeExpression = bluePrintContext.nodeTemplateByName(attributeNodeTemplateName).attributes?.get(attributeName)
?: throw BluePrintException(String.format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, attributeName))
var propertyDefinition: AttributeDefinition = bluePrintContext.nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)!!
- logger.info("template name ({}), property Name ({}) resolved value ({})", attributeNodeTemplateName, attributeName, attributeExpression)
+ logger.info("node template name ({}), property Name ({}) resolved value ({})", attributeNodeTemplateName, attributeName, nodeTemplateAttributeExpression)
// Check it it is a nested expression
- valueNode = resolveAssignmentExpression(attributeNodeTemplateName, attributeName, attributeExpression)
+ valueNode = resolveAssignmentExpression(attributeNodeTemplateName, attributeName, nodeTemplateAttributeExpression)
// subPropertyName?.let {
// valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
@@ -127,7 +129,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 {
- var valueNode: JsonNode = NullNode.getInstance()
+ val valueNode: JsonNode
val propertyName = propertyExpression.propertyName
val subPropertyName: String? = propertyExpression.subPropertyName
@@ -137,15 +139,15 @@ If Property Assignment is Expression. propertyNodeTemplateName = propertyExpression.modelableEntityName
}
- val propertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)
- ?: throw BluePrintException(String.format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))
+ val nodeTemplatePropertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)
+ ?: throw BluePrintException(format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))
var propertyDefinition: PropertyDefinition = bluePrintContext.nodeTemplateNodeType(propertyNodeTemplateName).properties?.get(propertyName)!!
- logger.info("template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, propertyExpression)
+ logger.info("node template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
// Check it it is a nested expression
- valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, propertyExpression)
+ valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
// subPropertyName?.let {
// valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
diff --git a/ms/controllerblueprints/modules/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt b/ms/controllerblueprints/modules/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt index 9621382c..697a7100 100644 --- a/ms/controllerblueprints/modules/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt +++ b/ms/controllerblueprints/modules/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.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.
@@ -32,6 +33,7 @@ import org.onap.ccsdk.apps.controllerblueprints.core.format import org.slf4j.LoggerFactory
import java.io.File
import java.nio.charset.Charset
+import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
/**
*
@@ -108,14 +110,60 @@ object JacksonUtils { }
@JvmStatic
+ fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode) : Boolean {
+ if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
+ return checkJsonNodeValueOfPrimitiveType(type, jsonNode)
+ } else if (BluePrintTypes.validCollectionTypes().contains(type)) {
+ return checkJsonNodeValueOfCollectionType(type, jsonNode)
+ }
+ return false;
+ }
+
+ @JvmStatic
+ fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {
+ when (primitiveType) {
+ BluePrintConstants.DATA_TYPE_STRING -> {
+ return jsonNode.isTextual
+ }
+ BluePrintConstants.DATA_TYPE_BOOLEAN -> {
+ return jsonNode.isBoolean
+ }
+ BluePrintConstants.DATA_TYPE_INTEGER -> {
+ return jsonNode.isInt
+ }
+ BluePrintConstants.DATA_TYPE_FLOAT -> {
+ return jsonNode.isDouble
+ }
+ BluePrintConstants.DATA_TYPE_TIMESTAMP -> {
+ return jsonNode.isTextual
+ }
+ else ->
+ return false
+ }
+ }
+
+ @JvmStatic
+ fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {
+ when (type) {
+ BluePrintConstants.DATA_TYPE_LIST ->
+ return jsonNode.isArray
+ BluePrintConstants.DATA_TYPE_MAP ->
+ return jsonNode.isContainerNode
+ else ->
+ return false
+ }
+
+ }
+
+ @JvmStatic
fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {
- if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
+ if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
objectNode.put(key, value as Boolean)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
objectNode.put(key, value as Int)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
objectNode.put(key, value as Float)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {
objectNode.put(key, value as String)
} else {
objectNode.put(key, value as String)
@@ -124,13 +172,13 @@ object JacksonUtils { @JvmStatic
fun populatePrimitiveValues(value: Any, primitiveType: String, objectNode: ArrayNode) {
- if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
+ if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
objectNode.add(value as Boolean)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
objectNode.add(value as Int)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
objectNode.add(value as Float)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {
objectNode.add(value as String)
} else {
objectNode.add(value as String)
@@ -139,11 +187,11 @@ object JacksonUtils { @JvmStatic
fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {
- if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
+ if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
objectNode.put(key, false)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
objectNode.put(key, 0)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
objectNode.put(key, 0.0)
} else {
objectNode.put(key, "")
@@ -152,11 +200,11 @@ object JacksonUtils { @JvmStatic
fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {
- if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
+ if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
arrayNode.add(false)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
arrayNode.add(0)
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
+ } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
arrayNode.add(0.0)
} else {
arrayNode.add("")
@@ -168,13 +216,13 @@ object JacksonUtils { if (nodeValue == null || nodeValue is NullNode) {
objectNode.set(key, nodeValue)
} else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
- if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_BOOLEAN == type) {
+ if (BluePrintConstants.DATA_TYPE_BOOLEAN == type) {
objectNode.put(key, nodeValue.asBoolean())
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_INTEGER == type) {
+ } else if (BluePrintConstants.DATA_TYPE_INTEGER == type) {
objectNode.put(key, nodeValue.asInt())
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_FLOAT == type) {
+ } else if (BluePrintConstants.DATA_TYPE_FLOAT == type) {
objectNode.put(key, nodeValue.floatValue())
- } else if (org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.DATA_TYPE_TIMESTAMP == type) {
+ } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == type) {
objectNode.put(key, nodeValue.asText())
} else {
objectNode.put(key, nodeValue.asText())
diff --git a/ms/controllerblueprints/modules/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtilsTest.kt b/ms/controllerblueprints/modules/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtilsTest.kt index 28f3b397..8d0f968f 100644 --- a/ms/controllerblueprints/modules/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtilsTest.kt +++ b/ms/controllerblueprints/modules/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtilsTest.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.
@@ -16,13 +17,14 @@ package org.onap.ccsdk.apps.controllerblueprints.core.utils
-import com.fasterxml.jackson.databind.JsonNode
import org.junit.Test
+import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
+import kotlin.test.assertTrue
/**
* JacksonUtilsTest
@@ -31,7 +33,7 @@ import kotlin.test.assertNotNull */
class JacksonUtilsTest {
- private val logger: Logger = LoggerFactory.getLogger(this::class.toString())
+ private val log: Logger = LoggerFactory.getLogger(this::class.toString())
val basePath = "load/blueprints"
@@ -49,17 +51,13 @@ class JacksonUtilsTest { @Test
fun testJsonNodeFromClassPathFile() {
val filePath = "data/default-context.json"
- val jsonNode = JacksonUtils.jsonNodeFromClassPathFile(filePath)
- assertNotNull(jsonNode, "Failed to get json node from file")
- assertEquals(true, jsonNode is JsonNode, "failed to get JSON node instance")
+ JacksonUtils.jsonNodeFromClassPathFile(filePath)
}
@Test
fun testJsonNodeFromFile() {
- val filePath = basePath + "/baseconfiguration/Definitions/activation-blueprint.json"
- val jsonNode = JacksonUtils.jsonNodeFromFile(filePath)
- assertNotNull(jsonNode, "Failed to get json node from file")
- assertEquals(true, jsonNode is JsonNode, "failed to get JSON node instance")
+ val filePath = basePath + "/baseconfiguration/Definitions/activation-blueprint.json"
+ JacksonUtils.jsonNodeFromFile(filePath)
}
@Test
@@ -68,4 +66,28 @@ class JacksonUtilsTest { val nodeType = JacksonUtils.getListFromJson(content, String::class.java)
assertNotNull(nodeType, "Failed to get String array from content")
}
+
+
+ @Test
+ fun testJsonValue() {
+ val filePath = "data/alltype-data.json"
+ val rootJson = JacksonUtils.jsonNodeFromClassPathFile(filePath);
+ assertNotNull(rootJson, "Failed to get all type data json node")
+ val intValue = rootJson.get("intValue")
+ assertTrue(JacksonUtils.checkJsonNodeValueOfType(BluePrintConstants.DATA_TYPE_INTEGER, intValue), "Failed to get as int value")
+ val floatValue = rootJson.get("floatValue")
+ assertTrue(JacksonUtils.checkJsonNodeValueOfType(BluePrintConstants.DATA_TYPE_FLOAT, floatValue), "Failed to get as float value")
+ val stringValue = rootJson.get("stringValue")
+ assertTrue(JacksonUtils.checkJsonNodeValueOfType(BluePrintConstants.DATA_TYPE_STRING, stringValue), "Failed to get as string value")
+ val booleanValue = rootJson.get("booleanValue")
+ assertTrue(JacksonUtils.checkJsonNodeValueOfType(BluePrintConstants.DATA_TYPE_BOOLEAN, booleanValue), "Failed to get as boolean value")
+ val arrayStringValue = rootJson.get("arrayStringValue")
+ assertTrue(JacksonUtils.checkJsonNodeValueOfType(BluePrintConstants.DATA_TYPE_LIST, arrayStringValue), "Failed to get as List value")
+ val mapValue = rootJson.get("mapValue")
+ assertTrue(JacksonUtils.checkJsonNodeValueOfType(BluePrintConstants.DATA_TYPE_MAP, mapValue), "Failed to get as Map value")
+
+ assertTrue(!JacksonUtils.checkJsonNodeValueOfType(BluePrintConstants.DATA_TYPE_LIST, stringValue), "Negative type failed")
+
+
+ }
}
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/core/src/test/resources/data/alltype-data.json b/ms/controllerblueprints/modules/core/src/test/resources/data/alltype-data.json new file mode 100644 index 00000000..055b0965 --- /dev/null +++ b/ms/controllerblueprints/modules/core/src/test/resources/data/alltype-data.json @@ -0,0 +1,10 @@ +{ + "intValue" : 1, + "floatValue" : 1.34, + "booleanValue" : true, + "stringValue" : "sample-String", + "timeValue" : "2018-09-29", + "arrayStringValue" : ["one", "two"], + "mapValue" : {"profile_name1":"profile_name1", + "profile_name2":"profile_name2"} +}
\ No newline at end of file |