9f43740aaa882007311e1ec1fc30aa7c1cbdfeeb
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / load / ModelTypeLoadService.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.load
19
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.*
30 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
31 import org.onap.ccsdk.cds.controllerblueprints.core.readNBText
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.slf4j.LoggerFactory
34 import org.springframework.stereotype.Service
35 import java.io.File
36
37 @Service
38 open class ModelTypeLoadService(private val modelTypeHandler: ModelTypeHandler) {
39
40     private val log = LoggerFactory.getLogger(ModelTypeLoadService::class.java)
41     private val updateBySystem = "System"
42
43     open suspend fun loadPathsModelType(modelTypePaths: List<String>) {
44         modelTypePaths.forEach {
45             loadPathModelType(it)
46         }
47     }
48
49     /**
50      * Load the Model Type file content from the defined path, Load of sequencing should be maintained.
51      */
52     open suspend fun loadPathModelType(modelTypePath: String) {
53         log.info(" ****** loadModelType($modelTypePath) ********")
54         try {
55             val errorBuilder = StrBuilder()
56
57             coroutineScope {
58                 val dataTypeFiles = normalizedFile("$modelTypePath", "data_type").listFiles()
59                 val deferred = dataTypeFiles.map {
60                     async {
61                         loadModelType(it, DataType::class.java, errorBuilder)
62                     }
63                 }
64                 deferred.awaitAll()
65             }
66
67             coroutineScope {
68                 val artifactTypeFiles = normalizedFile("$modelTypePath", "artifact_type").listFiles()
69                 val deferred = artifactTypeFiles.map {
70                     async {
71                         loadModelType(it, ArtifactType::class.java, errorBuilder)
72                     }
73                 }
74                 deferred.awaitAll()
75             }
76
77             coroutineScope {
78                 val relationshipTypeFiles = normalizedFile("$modelTypePath", "relationship_type").listFiles()
79                 val deferred = relationshipTypeFiles.map {
80                     async {
81                         loadModelType(it, RelationshipType::class.java, errorBuilder)
82                     }
83                 }
84                 deferred.awaitAll()
85             }
86
87             coroutineScope {
88                 val nodeTypeFiles = normalizedFile("$modelTypePath", "node_type").listFiles()
89                 val deferred = nodeTypeFiles.map {
90                     async {
91                         loadModelType(it, NodeType::class.java, errorBuilder)
92                     }
93                 }
94                 deferred.awaitAll()
95             }
96
97             if (!errorBuilder.isEmpty) {
98                 log.error(errorBuilder.toString())
99             }
100         } catch (e: Exception) {
101             log.error("Failed to loade ModelTypes under($modelTypePath)", e)
102         }
103     }
104
105     private suspend inline fun <reified T> loadModelType(file: File, classType: Class<T>, errorBuilder: StrBuilder) {
106         try {
107             log.trace("Loading ${classType.name} (${file.name})")
108             val dataKey = FilenameUtils.getBaseName(file.name)
109             val definitionContent = file.readNBText()
110             val definition = JacksonUtils.readValue(definitionContent, classType) as EntityType
111             //checkNotNull(definition) { "failed to get data type from file : ${file.name}" }
112
113             val modelType = ModelType()
114             val definitionType: String?
115             when (T::class) {
116                 DataType::class -> {
117                     definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE
118                 }
119                 RelationshipType::class -> {
120                     definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE
121                 }
122                 ArtifactType::class -> {
123                     definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE
124                 }
125                 NodeType::class -> {
126                     definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE
127                 }
128                 else -> {
129                     throw BluePrintException("couldn't process model type($classType) definition")
130                 }
131             }
132             modelType.definitionType = definitionType
133             modelType.derivedFrom = definition.derivedFrom
134             modelType.description = definition.description!!
135             modelType.definition = JacksonUtils.jsonNode(definitionContent)
136             modelType.modelName = dataKey
137             modelType.version = definition.version
138             modelType.updatedBy = updateBySystem
139             modelType.tags = (dataKey + "," + definition.derivedFrom + "," + definitionType)
140             modelTypeHandler.saveModel(modelType)
141             log.trace("${classType.name}(${file.name}) loaded successfully ")
142         } catch (e: Exception) {
143             errorBuilder.appendln("Couldn't load ${classType.name}(${file.name}: ${e.message}")
144         }
145     }
146 }