2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.controllerblueprints.service.load
19 import com.att.eelf.configuration.EELFManager
20 import kotlinx.coroutines.*
21 import org.apache.commons.io.FilenameUtils
22 import org.apache.commons.lang3.text.StrBuilder
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.onap.ccsdk.cds.controllerblueprints.service.domain.ModelType
28 import org.onap.ccsdk.cds.controllerblueprints.service.handler.ModelTypeHandler
29 import org.springframework.stereotype.Service
31 import java.nio.charset.Charset
34 open class ModelTypeLoadService(private val modelTypeHandler: ModelTypeHandler) {
36 private val log = EELFManager.getInstance().getLogger(ModelTypeLoadService::class.java)
37 private val updateBySystem = "System"
39 open suspend fun loadPathsModelType(modelTypePaths: List<String>) {
40 modelTypePaths.forEach { runBlocking { loadPathModelType(it) } }
44 * Load the Model Type file content from the defined path, Load of sequencing should be maintained.
46 open suspend fun loadPathModelType(modelTypePath: String) = runBlocking {
47 log.info(" *************************** loadModelType **********************")
49 val errorBuilder = StrBuilder()
52 val dataTypeFiles = File("$modelTypePath/data_type").listFiles()
54 val deferredResults = mutableListOf<Deferred<Unit>>()
56 for (file in dataTypeFiles) deferredResults += async {
57 loadModelType(file, DataType::class.java, errorBuilder)
60 deferredResults.awaitAll()
64 val artifactTypeFiles = File("$modelTypePath/artifact_type").listFiles()
66 val deferredResults = mutableListOf<Deferred<Unit>>()
68 for (file in artifactTypeFiles) deferredResults += async {
70 ArtifactType::class.java, errorBuilder)
73 deferredResults.awaitAll()
77 val relationshipTypeFiles = File("$modelTypePath/relationship_type").listFiles()
79 val deferredResults = mutableListOf<Deferred<Unit>>()
81 for (file in relationshipTypeFiles) deferredResults += async {
83 RelationshipType::class.java, errorBuilder)
86 deferredResults.awaitAll()
90 val nodeTypeFiles = File("$modelTypePath/node_type").listFiles()
92 val deferredResults = mutableListOf<Deferred<Unit>>()
94 for (file in nodeTypeFiles) deferredResults += async {
96 NodeType::class.java, errorBuilder)
98 deferredResults.awaitAll()
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 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.readText(Charset.defaultCharset())
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}")