2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.load
20 import kotlinx.coroutines.async
21 import kotlinx.coroutines.awaitAll
22 import kotlinx.coroutines.coroutineScope
23 import org.apache.commons.io.FilenameUtils
24 import org.apache.commons.lang3.text.StrBuilder
25 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ModelType
26 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.ModelTypeHandler
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.EntityType
32 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
33 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
34 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
35 import org.onap.ccsdk.cds.controllerblueprints.core.readNBText
36 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
37 import org.slf4j.LoggerFactory
38 import org.springframework.stereotype.Service
42 open class ModelTypeLoadService(private val modelTypeHandler: ModelTypeHandler) {
44 private val log = LoggerFactory.getLogger(ModelTypeLoadService::class.java)
45 private val updateBySystem = "System"
47 open suspend fun loadPathsModelType(modelTypePaths: List<String>) {
48 modelTypePaths.forEach {
54 * Load the Model Type file content from the defined path, Load of sequencing should be maintained.
56 open suspend fun loadPathModelType(modelTypePath: String) {
57 log.info(" ****** loadModelType($modelTypePath) ********")
59 val errorBuilder = StrBuilder()
62 val dataTypeFiles = normalizedFile("$modelTypePath", "data_type").listFiles()
63 val deferred = dataTypeFiles.map {
65 loadModelType(it, DataType::class.java, errorBuilder)
72 val artifactTypeFiles = normalizedFile("$modelTypePath", "artifact_type").listFiles()
73 val deferred = artifactTypeFiles.map {
75 loadModelType(it, ArtifactType::class.java, errorBuilder)
82 val relationshipTypeFiles = normalizedFile("$modelTypePath", "relationship_type").listFiles()
83 val deferred = relationshipTypeFiles.map {
85 loadModelType(it, RelationshipType::class.java, errorBuilder)
92 val nodeTypeFiles = normalizedFile("$modelTypePath", "node_type").listFiles()
93 val deferred = nodeTypeFiles.map {
95 loadModelType(it, NodeType::class.java, errorBuilder)
101 if (!errorBuilder.isEmpty) {
102 log.error(errorBuilder.toString())
104 } catch (e: Exception) {
105 log.error("Failed to loade ModelTypes under($modelTypePath)", e)
109 private suspend inline fun <reified T> loadModelType(file: File, classType: Class<T>, errorBuilder: StrBuilder) {
111 log.trace("Loading ${classType.name} (${file.name})")
112 val dataKey = FilenameUtils.getBaseName(file.name)
113 val definitionContent = file.readNBText()
114 val definition = JacksonUtils.readValue(definitionContent, classType) as EntityType
115 // checkNotNull(definition) { "failed to get data type from file : ${file.name}" }
117 val modelType = ModelType()
118 val definitionType: String?
121 definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE
123 RelationshipType::class -> {
124 definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE
126 ArtifactType::class -> {
127 definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE
130 definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE
133 throw BluePrintException("couldn't process model type($classType) definition")
136 modelType.definitionType = definitionType
137 modelType.derivedFrom = definition.derivedFrom
138 modelType.description = definition.description!!
139 modelType.definition = JacksonUtils.jsonNode(definitionContent)
140 modelType.modelName = dataKey
141 modelType.version = definition.version
142 modelType.updatedBy = updateBySystem
143 modelType.tags = (dataKey + "," + definition.derivedFrom + "," + definitionType)
144 modelTypeHandler.saveModel(modelType)
145 log.trace("${classType.name}(${file.name}) loaded successfully ")
146 } catch (e: Exception) {
147 errorBuilder.appendln("Couldn't load ${classType.name}(${file.name}: ${e.message}")