aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/load/ModelTypeLoadService.kt
blob: cb7953363b582794874a707bb24d93a9fb6b9b73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2019 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.blueprintsprocessor.designer.api.load

import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import org.apache.commons.io.FilenameUtils
import org.apache.commons.lang3.text.StrBuilder
import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ModelType
import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.ModelTypeHandler
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
import org.onap.ccsdk.cds.controllerblueprints.core.data.EntityType
import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
import org.onap.ccsdk.cds.controllerblueprints.core.readNBText
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.io.File

@Service
open class ModelTypeLoadService(private val modelTypeHandler: ModelTypeHandler) {

    private val log = LoggerFactory.getLogger(ModelTypeLoadService::class.java)
    private val updateBySystem = "System"

    open suspend fun loadPathsModelType(modelTypePaths: List<String>) {
        modelTypePaths.forEach {
            loadPathModelType(it)
        }
    }

    /**
     * Load the Model Type file content from the defined path, Load of sequencing should be maintained.
     */
    open suspend fun loadPathModelType(modelTypePath: String) {
        log.info(" ****** loadModelType($modelTypePath) ********")
        try {
            val errorBuilder = StrBuilder()

            coroutineScope {
                val dataTypeFiles = normalizedFile("$modelTypePath", "data_type").listFiles()
                val deferred = dataTypeFiles.map {
                    async {
                        loadModelType(it, DataType::class.java, errorBuilder)
                    }
                }
                deferred.awaitAll()
            }

            coroutineScope {
                val artifactTypeFiles = normalizedFile("$modelTypePath", "artifact_type").listFiles()
                val deferred = artifactTypeFiles.map {
                    async {
                        loadModelType(it, ArtifactType::class.java, errorBuilder)
                    }
                }
                deferred.awaitAll()
            }

            coroutineScope {
                val relationshipTypeFiles = normalizedFile("$modelTypePath", "relationship_type").listFiles()
                val deferred = relationshipTypeFiles.map {
                    async {
                        loadModelType(it, RelationshipType::class.java, errorBuilder)
                    }
                }
                deferred.awaitAll()
            }

            coroutineScope {
                val nodeTypeFiles = normalizedFile("$modelTypePath", "node_type").listFiles()
                val deferred = nodeTypeFiles.map {
                    async {
                        loadModelType(it, NodeType::class.java, errorBuilder)
                    }
                }
                deferred.awaitAll()
            }

            if (!errorBuilder.isEmpty) {
                log.error(errorBuilder.toString())
            }
        } catch (e: Exception) {
            log.error("Failed to loade ModelTypes under($modelTypePath)", e)
        }
    }

    private suspend inline fun <reified T> loadModelType(file: File, classType: Class<T>, errorBuilder: StrBuilder) {
        try {
            log.trace("Loading ${classType.name} (${file.name})")
            val dataKey = FilenameUtils.getBaseName(file.name)
            val definitionContent = file.readNBText()
            val definition = JacksonUtils.readValue(definitionContent, classType) as EntityType
            // checkNotNull(definition) { "failed to get data type from file : ${file.name}" }

            val modelType = ModelType()
            val definitionType: String?
            when (T::class) {
                DataType::class -> {
                    definitionType = BlueprintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE
                }
                RelationshipType::class -> {
                    definitionType = BlueprintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE
                }
                ArtifactType::class -> {
                    definitionType = BlueprintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE
                }
                NodeType::class -> {
                    definitionType = BlueprintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE
                }
                else -> {
                    throw BlueprintException("couldn't process model type($classType) definition")
                }
            }
            modelType.definitionType = definitionType
            modelType.derivedFrom = definition.derivedFrom
            modelType.description = definition.description!!
            modelType.definition = JacksonUtils.jsonNode(definitionContent)
            modelType.modelName = dataKey
            modelType.version = definition.version
            modelType.updatedBy = updateBySystem
            modelType.tags = (dataKey + "," + definition.derivedFrom + "," + definitionType)
            modelTypeHandler.saveModel(modelType)
            log.trace("${classType.name}(${file.name}) loaded successfully ")
        } catch (e: Exception) {
            errorBuilder.appendln("Couldn't load ${classType.name}(${file.name}: ${e.message}")
        }
    }
}