Revert "Renaming Files having BluePrint to have Blueprint"
[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.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
39 import java.io.File
40
41 @Service
42 open class ModelTypeLoadService(private val modelTypeHandler: ModelTypeHandler) {
43
44     private val log = LoggerFactory.getLogger(ModelTypeLoadService::class.java)
45     private val updateBySystem = "System"
46
47     open suspend fun loadPathsModelType(modelTypePaths: List<String>) {
48         modelTypePaths.forEach {
49             loadPathModelType(it)
50         }
51     }
52
53     /**
54      * Load the Model Type file content from the defined path, Load of sequencing should be maintained.
55      */
56     open suspend fun loadPathModelType(modelTypePath: String) {
57         log.info(" ****** loadModelType($modelTypePath) ********")
58         try {
59             val errorBuilder = StrBuilder()
60
61             coroutineScope {
62                 val dataTypeFiles = normalizedFile("$modelTypePath", "data_type").listFiles()
63                 val deferred = dataTypeFiles.map {
64                     async {
65                         loadModelType(it, DataType::class.java, errorBuilder)
66                     }
67                 }
68                 deferred.awaitAll()
69             }
70
71             coroutineScope {
72                 val artifactTypeFiles = normalizedFile("$modelTypePath", "artifact_type").listFiles()
73                 val deferred = artifactTypeFiles.map {
74                     async {
75                         loadModelType(it, ArtifactType::class.java, errorBuilder)
76                     }
77                 }
78                 deferred.awaitAll()
79             }
80
81             coroutineScope {
82                 val relationshipTypeFiles = normalizedFile("$modelTypePath", "relationship_type").listFiles()
83                 val deferred = relationshipTypeFiles.map {
84                     async {
85                         loadModelType(it, RelationshipType::class.java, errorBuilder)
86                     }
87                 }
88                 deferred.awaitAll()
89             }
90
91             coroutineScope {
92                 val nodeTypeFiles = normalizedFile("$modelTypePath", "node_type").listFiles()
93                 val deferred = nodeTypeFiles.map {
94                     async {
95                         loadModelType(it, NodeType::class.java, errorBuilder)
96                     }
97                 }
98                 deferred.awaitAll()
99             }
100
101             if (!errorBuilder.isEmpty) {
102                 log.error(errorBuilder.toString())
103             }
104         } catch (e: Exception) {
105             log.error("Failed to loade ModelTypes under($modelTypePath)", e)
106         }
107     }
108
109     private suspend inline fun <reified T> loadModelType(file: File, classType: Class<T>, errorBuilder: StrBuilder) {
110         try {
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}" }
116
117             val modelType = ModelType()
118             val definitionType: String?
119             when (T::class) {
120                 DataType::class -> {
121                     definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE
122                 }
123                 RelationshipType::class -> {
124                     definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE
125                 }
126                 ArtifactType::class -> {
127                     definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE
128                 }
129                 NodeType::class -> {
130                     definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE
131                 }
132                 else -> {
133                     throw BluePrintException("couldn't process model type($classType) definition")
134                 }
135             }
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}")
148         }
149     }
150 }