From 0e86613ab9cbe5c6b87746bb7bff09fc3a324d0b Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh" Date: Wed, 20 Mar 2019 18:27:53 -0400 Subject: Improve publish api Change-Id: I245fc765bf4e7916c36126d7a9597e9db80a1713 Issue-ID: CCSDK-1137 Signed-off-by: Muthuramalingam, Brinda Santh --- .../core/BluePrintConstants.kt | 6 +- .../core/FileExtensionFunctions.kt | 15 ++- .../core/interfaces/BluePrintEnhancer.kt | 5 +- .../core/service/BluePrintImportService.kt | 7 +- .../core/service/BluePrintParserService.kt | 62 ---------- .../core/service/PropertyAssignmentService.kt | 2 +- .../core/utils/BluePrintMetadataUtils.kt | 19 +-- .../core/utils/JacksonReactorUtils.kt | 129 +++++++++------------ .../core/utils/JacksonUtils.kt | 23 ++-- .../core/utils/ResourceResolverUtils.kt | 36 +++--- .../core/utils/ServiceTemplateUtils.kt | 28 +++-- .../core/utils/JacksonReactorUtilsTest.kt | 43 +++++++ 12 files changed, 172 insertions(+), 203 deletions(-) delete mode 100644 ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintParserService.kt create mode 100644 ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonReactorUtilsTest.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 e3545dff3..4b5789121 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 @@ -33,6 +33,9 @@ object BluePrintConstants { const val STATUS_PROCESSING: String = "processing" const val STATUS_FAILURE: String = "failure" + const val FLAG_Y: String = "Y" + const val FLAG_N: String = "N" + const val TYPE_DEFAULT: String = "default" const val DATA_TYPE_STRING: String = "string" @@ -98,8 +101,6 @@ object BluePrintConstants { const val MODEL_TYPE_NODE_DG = "tosca.nodes.DG" const val MODEL_TYPE_NODE_COMPONENT = "tosca.nodes.Component" const val MODEL_TYPE_NODE_VNF = "tosca.nodes.Vnf" - @Deprecated("Artifacts will be attached to Node Template") - const val MODEL_TYPE_NODE_ARTIFACT = "tosca.nodes.Artifact" const val MODEL_TYPE_NODE_RESOURCE_SOURCE = "tosca.nodes.ResourceSource" const val MODEL_TYPE_NODES_COMPONENT_JAVA: String = "tosca.nodes.component.Java" @@ -141,6 +142,7 @@ object BluePrintConstants { const val EXPRESSION_GET_NODE_OF_TYPE: String = "get_nodes_of_type" const val PROPERTY_BLUEPRINT_PROCESS_ID: String = "blueprint-process-id" + const val PROPERTY_BLUEPRINT_VALID: String = "blueprint-valid" const val PROPERTY_BLUEPRINT_BASE_PATH: String = "blueprint-basePath" const val PROPERTY_BLUEPRINT_RUNTIME: String = "blueprint-runtime" const val PROPERTY_BLUEPRINT_INPUTS_DATA: String = "blueprint-inputs-data" diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/FileExtensionFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/FileExtensionFunctions.kt index a433a31b1..11553ba6b 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/FileExtensionFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/FileExtensionFunctions.kt @@ -20,6 +20,7 @@ import org.apache.commons.io.FileUtils import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils import java.io.File import java.io.InputStream +import java.nio.file.Path import java.nio.file.Paths fun InputStream.toFile(path: String): File { @@ -65,10 +66,18 @@ fun File.deCompress(targetFile: File): File { } fun deleteDir(path: String) { - Paths.get(path).toFile().deleteRecursively() + normalizedFile(path).deleteRecursively() } +fun normalizedFile(path: String, vararg more: String?): File { + return Paths.get(path, *more).toFile().normalize() +} + +fun normalizedPath(path: String, vararg more: String?): Path { + return Paths.get(path, *more).normalize().toAbsolutePath() +} -fun normalizedFile(path: String): File { - return Paths.get(path).toFile().normalize() +fun normalizedPathName(path: String, vararg more: String?): String { + return normalizedPath(path, *more).toUri().path } + diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/interfaces/BluePrintEnhancer.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/interfaces/BluePrintEnhancer.kt index f01f12620..0aa01e8f6 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/interfaces/BluePrintEnhancer.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/interfaces/BluePrintEnhancer.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,10 +48,10 @@ interface BluePrintAttributeDefinitionEnhancer : BluePrintEnhancer = hashMapOf() - fun getImportResolvedServiceTemplate(): ServiceTemplate { + suspend fun getImportResolvedServiceTemplate(): ServiceTemplate { // Populate Imported Service Templates traverseSchema(PARENT_SERVICE_TEMPLATE, parentServiceTemplate) @@ -48,7 +49,7 @@ class BluePrintImportService(private val parentServiceTemplate: ServiceTemplate, return parentServiceTemplate } - private fun traverseSchema(key: String, serviceTemplate: ServiceTemplate) { + private suspend fun traverseSchema(key: String, serviceTemplate: ServiceTemplate) { if (key != PARENT_SERVICE_TEMPLATE) { importServiceTemplateMap[key] = serviceTemplate } @@ -63,7 +64,7 @@ class BluePrintImportService(private val parentServiceTemplate: ServiceTemplate, } } - private fun resolveImportDefinition(importDefinition: ImportDefinition): ServiceTemplate { + private suspend fun resolveImportDefinition(importDefinition: ImportDefinition): ServiceTemplate { var serviceTemplate: ServiceTemplate? = null val file: String = importDefinition.file val decodedSystemId: String = URLDecoder.decode(file, Charset.defaultCharset().toString()) diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintParserService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintParserService.kt deleted file mode 100644 index b1d67ece9..000000000 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintParserService.kt +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 - * - * 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.service - -import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate -import org.onap.ccsdk.apps.controllerblueprints.core.utils.ServiceTemplateUtils -import com.att.eelf.configuration.EELFLogger -import com.att.eelf.configuration.EELFManager -import java.io.File -import java.io.Serializable - -/** - * - * - * @author Brinda Santh - */ -interface BluePrintParserService : Serializable { - fun readBlueprint(content: String) : BluePrintContext - fun readBlueprintFile(fileName: String) : BluePrintContext - /** - * Read Blueprint from CSAR structure Directory - */ - fun readBlueprintFile(fileName: String, basePath : String) : BluePrintContext -} - -class BluePrintParserDefaultService : BluePrintParserService { - - private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString()) - - var basePath : String = javaClass.classLoader.getResource(".").path - - override fun readBlueprint(content: String): BluePrintContext { - return BluePrintContext(ServiceTemplateUtils.getServiceTemplateFromContent(content)) - } - - override fun readBlueprintFile(fileName: String): BluePrintContext { - return readBlueprintFile(fileName, basePath ) - } - - override fun readBlueprintFile(fileName: String, basePath : String): BluePrintContext { - val rootFilePath: String = StringBuilder().append(basePath).append(File.separator).append(fileName).toString() - val rootServiceTemplate : ServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath) - // TODO("Nested Lookup Implementation based on Import files") - return BluePrintContext(rootServiceTemplate) - } - - -} 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 7905b8fb4..62a7b09ea 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 @@ -219,7 +219,7 @@ If Property Assignment is Expression. if (artifactDefinition.repository != null) { TODO() } else if (artifactDefinition.file != null) { - return ResourceResolverUtils.getFileContent(artifactDefinition.file!!, bluePrintBasePath) + return ResourceResolverUtils.getFileContent(artifactDefinition.file, bluePrintBasePath) } return "" } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintMetadataUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintMetadataUtils.kt index ce11d97eb..8ba2e2755 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintMetadataUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintMetadataUtils.kt @@ -21,16 +21,17 @@ package org.onap.ccsdk.apps.controllerblueprints.core.utils import com.att.eelf.configuration.EELFLogger import com.att.eelf.configuration.EELFManager import com.fasterxml.jackson.databind.JsonNode +import kotlinx.coroutines.runBlocking import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.apps.controllerblueprints.core.asJsonPrimitive import org.onap.ccsdk.apps.controllerblueprints.core.data.ToscaMetaData +import org.onap.ccsdk.apps.controllerblueprints.core.normalizedFile import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintImportService import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService import org.onap.ccsdk.apps.controllerblueprints.core.service.DefaultBluePrintRuntimeService import java.io.File import java.nio.charset.Charset -import java.nio.file.Paths import java.util.* class BluePrintMetadataUtils { @@ -73,7 +74,7 @@ class BluePrintMetadataUtils { fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData { val toscaMetaData = ToscaMetaData() - val lines = Paths.get(metaFilePath).toFile().readLines(Charset.defaultCharset()) + val lines = normalizedFile(metaFilePath).readLines(Charset.defaultCharset()) lines.forEach { line -> if (line.contains(":")) { val keyValue = line.split(":") @@ -104,7 +105,8 @@ class BluePrintMetadataUtils { return bluePrintRuntimeService } - fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService> { + suspend fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String) + : BluePrintRuntimeService> { val bluePrintContext: BluePrintContext = getBaseEnhancementBluePrintContext(blueprintBasePath) @@ -115,7 +117,8 @@ class BluePrintMetadataUtils { return bluePrintRuntimeService } - fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap): BluePrintRuntimeService> { + fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap): + BluePrintRuntimeService> { val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath) val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext) executionContext.forEach { @@ -126,16 +129,16 @@ class BluePrintMetadataUtils { return bluePrintRuntimeService } - fun getBluePrintContext(blueprintBasePath: String): BluePrintContext { + fun getBluePrintContext(blueprintBasePath: String): BluePrintContext = runBlocking { val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath) log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})") - return readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath) + readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath) } - private fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext { + private suspend fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext { val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath) // Clean Type files BluePrintFileUtils.deleteBluePrintTypes(blueprintBasePath) @@ -151,7 +154,7 @@ class BluePrintMetadataUtils { return blueprintContext } - private fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext { + private suspend fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext { val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions) val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath) // Recursively Import Template files diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonReactorUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonReactorUtils.kt index 0522e1f5e..45d8fd4e8 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonReactorUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonReactorUtils.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,91 +20,73 @@ package org.onap.ccsdk.apps.controllerblueprints.core.utils import com.att.eelf.configuration.EELFLogger import com.att.eelf.configuration.EELFManager import com.fasterxml.jackson.databind.JsonNode -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import reactor.core.publisher.Mono -import reactor.core.publisher.toMono +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.apache.commons.io.IOUtils +import org.onap.ccsdk.apps.controllerblueprints.core.normalizedFile +import java.io.File +import java.nio.charset.Charset -@Deprecated("Reactor will be replaced by coroutines by default") -object JacksonReactorUtils { - private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString()) +class JacksonReactorUtils { + companion object { - @JvmStatic - fun getContent(fileName: String): Mono { - return JacksonUtils.getContent(fileName).toMono() - } + private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString()) - @JvmStatic - fun getClassPathFileContent(fileName: String): Mono { - return JacksonUtils.getClassPathFileContent(fileName).toMono() - } + suspend fun getContent(fileName: String): String { + //log.info("Reading File($fileName)") + return getContent(normalizedFile(fileName)) + } - @JvmStatic - fun readValue(content: String, valueType: Class): Mono { - return Mono.just(jacksonObjectMapper().readValue(content, valueType)) - } + suspend fun getContent(file: File): String = withContext(Dispatchers.IO) { + // log.info("Reading File(${file.absolutePath})") + file.readText(Charsets.UTF_8) + } - @JvmStatic - fun jsonNode(content: String): Mono { - return Mono.just(jacksonObjectMapper().readTree(content)) - } + suspend fun getClassPathFileContent(fileName: String): String = withContext(Dispatchers.IO) { + //log.trace("Reading Classpath File($fileName)") + IOUtils.toString(JacksonUtils::class.java.classLoader + .getResourceAsStream(fileName), Charset.defaultCharset()) + } - @JvmStatic - fun getJson(any: kotlin.Any, pretty: Boolean = false): Mono { - return Mono.just(JacksonUtils.getJson(any, pretty)) - } + suspend fun readValueFromFile(fileName: String, valueType: Class): T? { + val content: String = getContent(fileName) + return JacksonUtils.readValue(content, valueType) + } - @JvmStatic - fun getListFromJson(content: String, valueType: Class): Mono> { - val objectMapper = jacksonObjectMapper() - val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType) - return objectMapper.readValue>(content, javaType).toMono() - } + suspend fun readValueFromClassPathFile(fileName: String, valueType: Class): T? { + val content: String = getClassPathFileContent(fileName) + return JacksonUtils.readValue(content, valueType) + } - @JvmStatic - fun readValueFromFile(fileName: String, valueType: Class): Mono { - return getContent(fileName) - .flatMap { content -> - readValue(content, valueType) - } - } + suspend fun jsonNodeFromClassPathFile(fileName: String): JsonNode { + val content: String = getClassPathFileContent(fileName) + return JacksonUtils.jsonNode(content) + } - @JvmStatic - fun readValueFromClassPathFile(fileName: String, valueType: Class): Mono { - return getClassPathFileContent(fileName) - .flatMap { content -> - readValue(content, valueType) - } - } + suspend fun jsonNodeFromFile(fileName: String): JsonNode { + val content: String = getContent(fileName) + return JacksonUtils.jsonNode(content) + } - @JvmStatic - fun jsonNodeFromFile(fileName: String): Mono { - return getContent(fileName) - .flatMap { content -> - jsonNode(content) - } - } + suspend fun getListFromFile(fileName: String, valueType: Class): List { + val content: String = getContent(fileName) + return JacksonUtils.getListFromJson(content, valueType) + } - @JvmStatic - fun jsonNodeFromClassPathFile(fileName: String): Mono { - return getClassPathFileContent(fileName) - .flatMap { content -> - jsonNode(content) - } - } + suspend fun getListFromClassPathFile(fileName: String, valueType: Class): List { + val content: String = getClassPathFileContent(fileName) + return JacksonUtils.getListFromJson(content, valueType) + } - @JvmStatic - fun getListFromFile(fileName: String, valueType: Class): Mono> { - return getContent(fileName) - .flatMap { content -> - getListFromJson(content, valueType) - } - } + suspend fun getMapFromFile(file: File, valueType: Class): MutableMap { + val content: String = getContent(file) + return JacksonUtils.getMapFromJson(content, valueType) + } + + suspend fun getMapFromClassPathFile(fileName: String, valueType: Class): MutableMap { + val content: String = getClassPathFileContent(fileName) + return JacksonUtils.getMapFromJson(content, valueType) + } - @JvmStatic - fun getListFromClassPathFile(fileName: String, valueType: Class): Mono> { - return getClassPathFileContent(fileName) - .flatMap { content -> - getListFromJson(content, valueType) - } } } \ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt index e0341b8a4..7c515984c 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt @@ -21,24 +21,14 @@ import com.att.eelf.configuration.EELFManager import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.SerializationFeature -import com.fasterxml.jackson.databind.node.ArrayNode -import com.fasterxml.jackson.databind.node.BooleanNode -import com.fasterxml.jackson.databind.node.DoubleNode -import com.fasterxml.jackson.databind.node.FloatNode -import com.fasterxml.jackson.databind.node.IntNode -import com.fasterxml.jackson.databind.node.NullNode -import com.fasterxml.jackson.databind.node.ObjectNode -import com.fasterxml.jackson.databind.node.TextNode +import com.fasterxml.jackson.databind.node.* import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import org.apache.commons.io.IOUtils -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes +import org.onap.ccsdk.apps.controllerblueprints.core.* import java.io.File import java.nio.charset.Charset @@ -51,7 +41,7 @@ class JacksonUtils { companion object { private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString()) inline fun readValue(content: String): T = - jacksonObjectMapper().readValue(content, T::class.java) + jacksonObjectMapper().readValue(content, T::class.java) fun readValue(content: String, valueType: Class): T? { return jacksonObjectMapper().readValue(content, valueType) @@ -73,7 +63,8 @@ class JacksonUtils { } } - fun getContent(fileName: String): String = getContent(File(fileName)) + + fun getContent(fileName: String): String = getContent(normalizedFile(fileName)) fun getContent(file: File): String = runBlocking { async { @@ -89,7 +80,7 @@ class JacksonUtils { return runBlocking { withContext(Dispatchers.Default) { IOUtils.toString(JacksonUtils::class.java.classLoader - .getResourceAsStream(fileName), Charset.defaultCharset()) + .getResourceAsStream(fileName), Charset.defaultCharset()) } } } @@ -189,7 +180,7 @@ class JacksonUtils { fun getInstanceFromMap(properties: MutableMap, classType: Class): T { return readValue(getJson(properties), classType) - ?: throw BluePrintProcessorException("failed to transform content ($properties) to type ($classType)") + ?: throw BluePrintProcessorException("failed to transform content ($properties) to type ($classType)") } fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean { diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/ResourceResolverUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/ResourceResolverUtils.kt index 965e965c6..762e47fbf 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/ResourceResolverUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/ResourceResolverUtils.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +17,13 @@ package org.onap.ccsdk.apps.controllerblueprints.core.utils -import org.apache.commons.io.FileUtils -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException -import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmpty import com.att.eelf.configuration.EELFLogger import com.att.eelf.configuration.EELFManager +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmpty import java.io.File import java.net.URL -import java.nio.charset.Charset + /** * * @@ -32,30 +32,30 @@ import java.nio.charset.Charset object ResourceResolverUtils { private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString()) - @JvmStatic - fun getFileContent(filename : String, basePath : String?): String { + fun getFileContent(filename: String, basePath: String?): String { log.trace("file ({}), basePath ({}) ", filename, basePath) - try{ - var resolvedFileName : String = filename - if(filename.startsWith("http", true) - || filename.startsWith("https", true)){ - val givenUrl : String = URL(filename).toString() - val systemUrl : String = File(".").toURI().toURL().toString() + try { + var resolvedFileName: String = filename + if (filename.startsWith("http", true) + || filename.startsWith("https", true)) { + val givenUrl: String = URL(filename).toString() + val systemUrl: String = File(".").toURI().toURL().toString() log.trace("givenUrl ({}), systemUrl ({}) ", givenUrl, systemUrl) - if(givenUrl.startsWith(systemUrl)){ + if (givenUrl.startsWith(systemUrl)) { } - }else{ - if(!filename.startsWith("/")){ + } else { + if (!filename.startsWith("/")) { if (checkNotEmpty(basePath)) { resolvedFileName = basePath.plus(File.separator).plus(filename) - }else{ + } else { resolvedFileName = javaClass.classLoader.getResource(".").path.plus(filename) } } } - return FileUtils.readFileToString(File(resolvedFileName), Charset.defaultCharset()) - }catch (e : Exception){ + //FIXME("Convert into reactive") + return JacksonUtils.getContent(resolvedFileName) + } catch (e: Exception) { throw BluePrintException(e, "failed to file (%s), basePath (%s) ", filename, basePath) } } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/ServiceTemplateUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/ServiceTemplateUtils.kt index 933161323..28916772e 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/ServiceTemplateUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/ServiceTemplateUtils.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +17,8 @@ package org.onap.ccsdk.apps.controllerblueprints.core.utils -import org.apache.commons.io.FileUtils import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate import org.onap.ccsdk.apps.controllerblueprints.core.data.TopologyTemplate -import java.io.File -import java.nio.charset.Charset /** * @@ -29,14 +27,11 @@ import java.nio.charset.Charset */ object ServiceTemplateUtils { - @JvmStatic - fun getServiceTemplate(fileName: String): ServiceTemplate { - val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset()) + suspend fun getServiceTemplate(fileName: String): ServiceTemplate { + val content: String = JacksonReactorUtils.getContent(fileName) return getServiceTemplateFromContent(content) } - - @JvmStatic fun getServiceTemplateFromContent(content: String): ServiceTemplate { return JacksonUtils.readValue(content) } @@ -80,31 +75,34 @@ object ServiceTemplateUtils { parentServiceTemplate.topologyTemplate = parentServiceTemplate.topologyTemplate ?: TopologyTemplate() toMerge.topologyTemplate?.inputs?.let { - parentServiceTemplate.topologyTemplate?.inputs = parentServiceTemplate.topologyTemplate?.inputs ?: hashMapOf() + parentServiceTemplate.topologyTemplate?.inputs = parentServiceTemplate.topologyTemplate?.inputs + ?: hashMapOf() parentServiceTemplate.topologyTemplate?.inputs?.putAll(parentServiceTemplate.topologyTemplate?.inputs as MutableMap) } toMerge.topologyTemplate?.nodeTemplates?.let { - parentServiceTemplate.topologyTemplate?.nodeTemplates = parentServiceTemplate.topologyTemplate?.nodeTemplates ?: hashMapOf() + parentServiceTemplate.topologyTemplate?.nodeTemplates = parentServiceTemplate.topologyTemplate?.nodeTemplates + ?: hashMapOf() parentServiceTemplate.topologyTemplate?.nodeTemplates?.putAll(parentServiceTemplate.topologyTemplate?.nodeTemplates as MutableMap) } toMerge.topologyTemplate?.relationshipTemplates?.let { - parentServiceTemplate.topologyTemplate?.relationshipTemplates = parentServiceTemplate.topologyTemplate?.relationshipTemplates ?: hashMapOf() + parentServiceTemplate.topologyTemplate?.relationshipTemplates = parentServiceTemplate.topologyTemplate?.relationshipTemplates + ?: hashMapOf() parentServiceTemplate.topologyTemplate?.relationshipTemplates?.putAll(parentServiceTemplate.topologyTemplate?.relationshipTemplates as MutableMap) } toMerge.topologyTemplate?.policies?.let { - parentServiceTemplate.topologyTemplate?.policies = parentServiceTemplate.topologyTemplate?.policies ?: hashMapOf() + parentServiceTemplate.topologyTemplate?.policies = parentServiceTemplate.topologyTemplate?.policies + ?: hashMapOf() parentServiceTemplate.topologyTemplate?.policies?.putAll(parentServiceTemplate.topologyTemplate?.policies as MutableMap) } toMerge.topologyTemplate?.workflows?.let { - parentServiceTemplate.topologyTemplate?.workflows = parentServiceTemplate.topologyTemplate?.workflows ?: hashMapOf() + parentServiceTemplate.topologyTemplate?.workflows = parentServiceTemplate.topologyTemplate?.workflows + ?: hashMapOf() parentServiceTemplate.topologyTemplate?.workflows?.putAll(parentServiceTemplate.topologyTemplate?.workflows as MutableMap) } return parentServiceTemplate } - - } \ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonReactorUtilsTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonReactorUtilsTest.kt new file mode 100644 index 000000000..9cf8d62af --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonReactorUtilsTest.kt @@ -0,0 +1,43 @@ +/* + * Copyright © 2019 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.att.eelf.configuration.EELFLogger +import com.att.eelf.configuration.EELFManager +import kotlinx.coroutines.runBlocking +import org.junit.Test + +class JacksonReactorUtilsTest { + + private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString()) + + @Test + fun testJsonNodeFromClassPathFile() { + runBlocking { + val filePath = "data/default-context.json" + JacksonReactorUtils.jsonNodeFromClassPathFile(filePath) + } + } + + @Test + fun testJsonNodeFromFile() { + runBlocking { + val filePath = "src/test/resources/data/default-context.json" + JacksonReactorUtils.jsonNodeFromFile(filePath) + } + } +} \ No newline at end of file -- cgit 1.2.3-korg