aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main
diff options
context:
space:
mode:
authorBrinda Santh <bs2796@att.com>2019-12-03 16:15:20 -0500
committerBrinda Santh <bs2796@att.com>2019-12-06 13:37:24 -0500
commitea84b36447601d0d6a633ae708b72c6aaae6dc67 (patch)
tree732ae48ab262fd2ff8ddda1748728d9764eb0d79 /ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main
parent7466e4277c9fe098a61ae126d1892b3cd30d6f89 (diff)
Refractor controller blueprint modules
Move controller blueprints modules to blueprints processor and change the maven group name. Fix test cba and model type paths Fix dependencies issues. Issue-ID: CCSDK-1663 Signed-off-by: Brinda Santh <bs2796@att.com> Change-Id: I3654e6f04811470327acba90e8a452b66e3e189b
Diffstat (limited to 'ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main')
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDefinition.kt108
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDictionaryConstants.kt38
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/factory/ResourceSourceMappingFactory.kt46
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/service/ResourceAssignmentValidationService.kt142
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtils.kt119
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/utils/ResourceDictionaryUtils.kt99
6 files changed, 552 insertions, 0 deletions
diff --git a/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDefinition.kt b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDefinition.kt
new file mode 100644
index 000000000..10930ffab
--- /dev/null
+++ b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDefinition.kt
@@ -0,0 +1,108 @@
+/*
+ * Copyright © 2018 IBM.
+ * Modifications 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.cds.controllerblueprints.resource.dict
+
+import com.fasterxml.jackson.annotation.JsonFormat
+import com.fasterxml.jackson.annotation.JsonProperty
+import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
+import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
+import java.io.Serializable
+import java.util.Date
+
+open class ResourceDefinition {
+
+ @JsonProperty(value = "name", required = true)
+ lateinit var name: String
+
+ @JsonProperty(value = "property", required = true)
+ lateinit var property: PropertyDefinition
+
+ var tags: String? = null
+
+ @JsonProperty(value = "updated-by")
+ lateinit var updatedBy: String
+
+ @JsonProperty(value = "sources", required = true)
+ lateinit var sources: MutableMap<String, NodeTemplate>
+}
+
+open class ResourceAssignment {
+
+ @JsonProperty(value = "name", required = true)
+ lateinit var name: String
+
+ @JsonProperty(value = "property")
+ var property: PropertyDefinition? = null
+
+ @JsonProperty("input-param")
+ var inputParameter: Boolean = false
+
+ @JsonProperty("dictionary-name")
+ var dictionaryName: String? = null
+
+ @JsonProperty("dictionary-source")
+ var dictionarySource: String? = null
+
+ /** Modified Source definition, Capability Source will use for script reference changes,
+ * Rest Source will use for extra headers etc **/
+ @JsonProperty("dictionary-source-definition")
+ var dictionarySourceDefinition: NodeTemplate? = null
+
+ /** Duplicate field : Shall be used directly from Dictionary Source definition dependencies. **/
+ @JsonProperty("dependencies")
+ var dependencies: MutableList<String>? = null
+
+ @JsonProperty("version")
+ var version: Int = 0
+
+ @JsonProperty("status")
+ var status: String? = null
+
+ @JsonProperty("message")
+ var message: String? = null
+
+ @JsonProperty("updated-date")
+ @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
+ var updatedDate: Date? = null
+
+ @JsonProperty("updated-by")
+ var updatedBy: String? = null
+
+ override fun toString(): String {
+ return """
+ [
+ name = $name
+ status = $status
+ required = ${property?.required}
+ dependencies = $dependencies
+ dictionaryName = $dictionaryName
+ dictionarySource = $dictionarySource
+ ]
+ """.trimIndent()
+ }
+}
+
+/**
+ * Interface for Source Definitions (ex Input Source,
+ * Default Source, Database Source, Rest Sources, etc)
+ */
+interface ResourceSource : Serializable
+
+open class ResourceSourceMapping {
+ lateinit var resourceSourceMappings: MutableMap<String, String>
+}
diff --git a/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDictionaryConstants.kt b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDictionaryConstants.kt
new file mode 100644
index 000000000..366a4dcd4
--- /dev/null
+++ b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDictionaryConstants.kt
@@ -0,0 +1,38 @@
+/*
+ * 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.cds.controllerblueprints.resource.dict
+
+/**
+ * ResourceDictionaryConstants
+ *
+ * @author Brinda Santh
+ */
+object ResourceDictionaryConstants {
+
+ const val SOURCE_INPUT = "input"
+ const val SOURCE_DEFAULT = "default"
+ const val PROCESSOR_DB = "processor-db"
+
+ const val MODEL_DIR_RESOURCE_DEFINITION: String = "resource_dictionary"
+
+ const val PROPERTY_TYPE = "type"
+ const val PROPERTY_INPUT_KEY_MAPPING = "input-key-mapping"
+ const val PROPERTY_OUTPUT_KEY_MAPPING = "output-key-mapping"
+ const val PROPERTY_KEY_DEPENDENCIES = "key-dependencies"
+
+ const val PATH_RESOURCE_DEFINITION_TYPE = "resources_definition_types"
+}
diff --git a/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/factory/ResourceSourceMappingFactory.kt b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/factory/ResourceSourceMappingFactory.kt
new file mode 100644
index 000000000..f8dea9871
--- /dev/null
+++ b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/factory/ResourceSourceMappingFactory.kt
@@ -0,0 +1,46 @@
+/*
+ * 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.cds.controllerblueprints.resource.dict.factory
+
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
+import org.onap.ccsdk.cds.controllerblueprints.core.format
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceSourceMapping
+
+/**
+ * ResourceSourceMappingFactory.
+ *
+ * @author Brinda Santh
+ */
+object ResourceSourceMappingFactory {
+
+ private val resourceSourceMappings: MutableMap<String, String> = hashMapOf()
+
+ fun registerSourceMapping(sourceInstance: String, nodeTypeName: String) {
+ resourceSourceMappings[sourceInstance] = nodeTypeName
+ }
+
+ fun getRegisterSourceMapping(sourceInstance: String): String {
+ return resourceSourceMappings[sourceInstance]
+ ?: throw BluePrintException(format("failed to get source({}) mapping", sourceInstance))
+ }
+
+ fun getRegisterSourceMapping(): ResourceSourceMapping {
+ val resourceSourceMapping = ResourceSourceMapping()
+ resourceSourceMapping.resourceSourceMappings = resourceSourceMappings
+ return resourceSourceMapping
+ }
+}
diff --git a/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/service/ResourceAssignmentValidationService.kt b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/service/ResourceAssignmentValidationService.kt
new file mode 100644
index 000000000..a5171d25c
--- /dev/null
+++ b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/service/ResourceAssignmentValidationService.kt
@@ -0,0 +1,142 @@
+/*
+ * 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.
+ * 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.cds.controllerblueprints.resource.dict.service
+
+import org.apache.commons.collections.CollectionUtils
+import org.apache.commons.lang3.StringUtils
+import org.apache.commons.lang3.text.StrBuilder
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
+import org.onap.ccsdk.cds.controllerblueprints.core.utils.TopologicalSortingUtils
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
+import org.slf4j.LoggerFactory
+import java.io.Serializable
+
+/**
+ * ResourceAssignmentValidationService.
+ *
+ * @author Brinda Santh
+ */
+interface ResourceAssignmentValidationService : Serializable {
+
+ @Throws(BluePrintException::class)
+ fun validate(resourceAssignments: List<ResourceAssignment>): Boolean
+}
+
+/**
+ * ResourceAssignmentValidationServiceImpl.
+ *
+ * @author Brinda Santh
+ */
+open class ResourceAssignmentValidationServiceImpl : ResourceAssignmentValidationService {
+
+ private val log = LoggerFactory.getLogger(ResourceAssignmentValidationServiceImpl::class.java)
+
+ open var resourceAssignmentMap: Map<String, ResourceAssignment> = hashMapOf()
+ open val validationMessage = StrBuilder()
+
+ override fun validate(resourceAssignments: List<ResourceAssignment>): Boolean {
+ try {
+ validateTemplateNDictionaryKeys(resourceAssignments)
+ validateCyclicDependency(resourceAssignments)
+ if (StringUtils.isNotBlank(validationMessage)) {
+ throw BluePrintException("Resource Assignment Validation Failure")
+ }
+ } catch (e: Exception) {
+ throw BluePrintException("Resource Assignment Validation :" + validationMessage.toString(), e)
+ }
+ return true
+ }
+
+ open fun validateTemplateNDictionaryKeys(resourceAssignments: List<ResourceAssignment>) {
+
+ resourceAssignmentMap = resourceAssignments.map { it.name to it }.toMap()
+
+ // Check the Resource Assignment has Duplicate Key Names
+ val duplicateKeyNames = resourceAssignments.groupBy { it.name }
+ .filter { it.value.size > 1 }
+ .map { it.key }
+
+ if (duplicateKeyNames.isNotEmpty()) {
+ validationMessage.appendln(String.format("Duplicate Assignment Template Keys (%s) is Present", duplicateKeyNames))
+ }
+
+ // Collect all the dependencies as a single list
+ val dependenciesNames = resourceAssignments.mapNotNull { it.dependencies }.flatten()
+
+ // Check all the dependencies keys have Resource Assignment mappings.
+ val notPresentDictionaries = dependenciesNames.filter { !resourceAssignmentMap.containsKey(it) }.distinct()
+ if (notPresentDictionaries.isNotEmpty()) {
+ validationMessage.appendln(String.format("No assignments for Dictionary Keys (%s)", notPresentDictionaries))
+ }
+
+ if (StringUtils.isNotBlank(validationMessage)) {
+ throw BluePrintException("Resource Assignment Validation Failure")
+ }
+ }
+
+ open fun validateCyclicDependency(resourceAssignments: List<ResourceAssignment>) {
+ val startResourceAssignment = ResourceAssignment()
+ startResourceAssignment.name = "*"
+
+ val topologySorting = TopologicalSortingUtils<ResourceAssignment>()
+
+ resourceAssignmentMap.map { it.value }.map { resourceAssignment ->
+ if (CollectionUtils.isNotEmpty(resourceAssignment.dependencies)) {
+ resourceAssignment.dependencies!!.map {
+ log.trace("Topological Graph link from {} to {}", it, resourceAssignment.name)
+ topologySorting.add(resourceAssignmentMap[it]!!, resourceAssignment)
+ }
+ } else {
+ topologySorting.add(startResourceAssignment, resourceAssignment)
+ }
+ }
+
+ if (!topologySorting.isDag) {
+ val graph = getTopologicalGraph(topologySorting)
+ validationMessage.appendln("Cyclic Dependency :$graph")
+ }
+ }
+
+ open fun getTopologicalGraph(topologySorting: TopologicalSortingUtils<ResourceAssignment>): String {
+ val s = StringBuilder()
+ val neighbors = topologySorting.getNeighbors()
+
+ neighbors.forEach { v, vs ->
+ if (v.name == "*") {
+ s.append("\n * -> [")
+ for (resourceAssignment in vs) {
+ s.append(
+ "(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name +
+ "),"
+ )
+ }
+ s.append("]")
+ } else {
+ s.append("\n (" + v.dictionaryName + ":" + v.name + ") -> [")
+ for (resourceAssignment in vs) {
+ s.append(
+ "(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name +
+ "),"
+ )
+ }
+ s.append("]")
+ }
+ }
+ return s.toString()
+ }
+}
diff --git a/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtils.kt b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtils.kt
new file mode 100644
index 000000000..9a5523113
--- /dev/null
+++ b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtils.kt
@@ -0,0 +1,119 @@
+/*
+ * 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.cds.controllerblueprints.resource.dict.utils
+
+import org.apache.commons.collections.CollectionUtils
+import org.onap.ccsdk.cds.controllerblueprints.core.asListOfString
+import org.onap.ccsdk.cds.controllerblueprints.core.utils.TopologicalSortingUtils
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
+import org.slf4j.LoggerFactory
+import java.util.ArrayList
+
+/**
+ * BulkResourceSequencingUtils.
+ *
+ * @author Brinda Santh
+ */
+object BulkResourceSequencingUtils {
+
+ private val log = LoggerFactory.getLogger(BulkResourceSequencingUtils::class.java)
+
+ @JvmStatic
+ fun process(resourceAssignments: MutableList<ResourceAssignment>): List<List<ResourceAssignment>> {
+ val resourceAssignmentMap: MutableMap<String, ResourceAssignment> = hashMapOf()
+ val sequenceBatchResourceAssignment = ArrayList<List<ResourceAssignment>>()
+ log.trace("Assignments ({})", resourceAssignments)
+ // Prepare Map
+ resourceAssignments.forEach { resourceAssignment ->
+ log.trace("Processing Key ({})", resourceAssignment.name)
+ resourceAssignmentMap.put(resourceAssignment.name, resourceAssignment)
+ }
+
+ val startResourceAssignment = ResourceAssignment()
+ startResourceAssignment.name = "*"
+
+ // Preepare Sorting Map
+ val topologySorting = TopologicalSortingUtils<ResourceAssignment>()
+ resourceAssignmentMap.forEach { _, resourceAssignment ->
+ // Get the dependencies from the assignment sources, if not get from the Resource Assignment dependencies
+ if (resourceAssignment.dictionarySourceDefinition != null) {
+ val dependencies = resourceAssignment.dictionarySourceDefinition?.properties?.get("key-dependencies")?.asListOfString()
+ dependencies?.forEach { dependency ->
+ topologySorting.add(resourceAssignmentMap[dependency]!!, resourceAssignment)
+ }
+ } else if (CollectionUtils.isNotEmpty(resourceAssignment.dependencies)) {
+ for (dependency in resourceAssignment.dependencies!!) {
+ topologySorting.add(resourceAssignmentMap[dependency]!!, resourceAssignment)
+ }
+ } else {
+ topologySorting.add(startResourceAssignment, resourceAssignment)
+ }
+ }
+
+ val sequencedResourceAssignments: MutableList<ResourceAssignment> = topologySorting.topSort()!! as MutableList<ResourceAssignment>
+ log.trace("Sorted Sequenced Assignments ({})", sequencedResourceAssignments)
+
+ var batchResourceAssignment: MutableList<ResourceAssignment>? = null
+ var batchAssignmentName: MutableList<String>? = null
+
+ // Prepare Sorting
+ sequencedResourceAssignments.forEachIndexed { index, resourceAssignment ->
+
+ var previousResourceAssignment: ResourceAssignment? = null
+
+ if (index > 0) {
+ previousResourceAssignment = sequencedResourceAssignments[index - 1]
+ }
+
+ var dependencyPresence = false
+ if (batchAssignmentName != null && resourceAssignment.dependencies != null) {
+ dependencyPresence = CollectionUtils.containsAny(batchAssignmentName, resourceAssignment.dependencies)
+ }
+
+ log.trace(
+ "({}) -> Checking ({}), with ({}), result ({})", resourceAssignment.name,
+ batchAssignmentName, resourceAssignment.dependencies, dependencyPresence
+ )
+
+ if (previousResourceAssignment != null && resourceAssignment.dictionarySource != null &&
+ resourceAssignment.dictionarySource!!.equals(previousResourceAssignment.dictionarySource, true) &&
+ !dependencyPresence
+ ) {
+ batchResourceAssignment!!.add(resourceAssignment)
+ batchAssignmentName!!.add(resourceAssignment.name)
+ } else {
+ if (batchResourceAssignment != null) {
+ sequenceBatchResourceAssignment.add(batchResourceAssignment!!)
+ log.trace("Created old Set ({})", batchAssignmentName)
+ }
+ batchResourceAssignment = arrayListOf()
+ batchResourceAssignment!!.add(resourceAssignment)
+
+ batchAssignmentName = arrayListOf()
+ batchAssignmentName!!.add(resourceAssignment.name)
+ }
+
+ if (index == sequencedResourceAssignments.size - 1) {
+ log.trace("Created old Set ({})", batchAssignmentName)
+ sequenceBatchResourceAssignment.add(batchResourceAssignment!!)
+ }
+ }
+ log.info("Batched Sequence : ({})", sequenceBatchResourceAssignment)
+
+ return sequenceBatchResourceAssignment
+ }
+}
diff --git a/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/utils/ResourceDictionaryUtils.kt b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/utils/ResourceDictionaryUtils.kt
new file mode 100644
index 000000000..f26c5098f
--- /dev/null
+++ b/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/utils/ResourceDictionaryUtils.kt
@@ -0,0 +1,99 @@
+/*
+ * 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.cds.controllerblueprints.resource.dict.utils
+
+import com.fasterxml.jackson.databind.JsonNode
+import com.fasterxml.jackson.databind.node.NullNode
+import org.apache.commons.collections.MapUtils
+import org.apache.commons.lang3.StringUtils
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
+import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
+import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
+import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants
+import org.slf4j.LoggerFactory
+import java.io.File
+
+object ResourceDictionaryUtils {
+ private val log = LoggerFactory.getLogger(ResourceDictionaryUtils::class.java)
+
+ @JvmStatic
+ fun populateSourceMapping(
+ resourceAssignment: ResourceAssignment,
+ resourceDefinition: ResourceDefinition
+ ) {
+
+ if (StringUtils.isBlank(resourceAssignment.dictionarySource)) {
+
+ if (MapUtils.isNotEmpty(resourceDefinition.sources)) {
+ val source = findFirstSource(resourceDefinition.sources)
+
+ // Populate and Assign First Source
+ if (StringUtils.isNotBlank(source)) {
+ // Set Dictionary Source
+ resourceAssignment.dictionarySource = source
+ } else {
+ resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
+ }
+ log.info("auto map resourceAssignment : {}", resourceAssignment)
+ } else {
+ resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
+ }
+ }
+ }
+
+ @JvmStatic
+ fun findFirstSource(sources: Map<String, NodeTemplate>): String? {
+ var source: String? = null
+ if (MapUtils.isNotEmpty(sources)) {
+ source = sources.keys.stream().findFirst().get()
+ }
+ return source
+ }
+
+ @JvmStatic
+ fun assignInputs(data: JsonNode, context: MutableMap<String, Any>) {
+ log.trace("assignInputs from input JSON ({})", data.toString())
+ data.fields().forEach { field ->
+ val valueNode: JsonNode = data.at("/".plus(field.key)) ?: NullNode.getInstance()
+
+ val path = BluePrintConstants.PATH_INPUTS.plus(BluePrintConstants.PATH_DIVIDER).plus(field.key)
+ log.trace("setting path ({}), values ({})", path, valueNode)
+ context[path] = valueNode
+ }
+ }
+
+ fun getResourceAssignmentFromFile(filePath: String): List<ResourceAssignment> {
+ return JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
+ ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
+ }
+
+ fun writeResourceDefinitionTypes(basePath: String, resourceDefinitions: List<ResourceDefinition>) {
+ val resourceDefinitionMap = resourceDefinitions.map { it.name to it }.toMap()
+ writeResourceDefinitionTypes(basePath, resourceDefinitionMap)
+ }
+
+ fun writeResourceDefinitionTypes(basePath: String, resourceDefinitionMap: Map<String, ResourceDefinition>) {
+ val typePath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
+ .plus(File.separator).plus("${ResourceDictionaryConstants.PATH_RESOURCE_DEFINITION_TYPE}.json")
+ val resourceDefinitionContent = JacksonUtils.getJson(resourceDefinitionMap.toSortedMap(), true)
+ BluePrintFileUtils.writeDefinitionFile(typePath, resourceDefinitionContent)
+ }
+}