5c7ba275de9466d22063c1196d10ea84b17c0b55
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / handler / ModelTypeHandler.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.handler
19
20 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ModelTypeRepository
21 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.ModelTypeValidator
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ModelType
24 import org.slf4j.LoggerFactory
25 import org.springframework.stereotype.Service
26
27 @Service
28 open class ModelTypeHandler(private val modelTypeRepository: ModelTypeRepository) {
29
30     private val log = LoggerFactory.getLogger(ModelTypeHandler::class.java)!!
31
32     /**
33      * This is a getModelTypeByName service
34      *
35      * @param modelTypeName modelTypeName
36      * @return ModelType
37      */
38     suspend fun getModelTypeByName(modelTypeName: String): ModelType {
39         log.info("Searching : $modelTypeName")
40         check(modelTypeName.isNotBlank()) { "Model Name Information is missing." }
41         val modelType = modelTypeRepository.findByModelName(modelTypeName)
42         return if (modelType != null) {
43             modelType
44         } else {
45             throw BluePrintException("couldn't get modelType($modelTypeName)")
46         }
47     }
48
49
50     /**
51      * This is a searchModelTypes service
52      *
53      * @param tags tags
54      * @return List<ModelType>
55     </ModelType> */
56     suspend fun searchModelTypes(tags: String): List<ModelType> {
57         check(tags.isNotBlank()) { "No Search Information provide" }
58         return modelTypeRepository.findByTagsContainingIgnoreCase(tags)
59     }
60
61     /**
62      * This is a saveModel service
63      *
64      * @param modelType modelType
65      * @return ModelType
66      * @throws BluePrintException BluePrintException
67      */
68     @Throws(BluePrintException::class)
69     suspend open fun saveModel(modelType: ModelType): ModelType {
70         lateinit var dbModel: ModelType
71         ModelTypeValidator.validateModelType(modelType)
72         val dbModelType: ModelType? = modelTypeRepository.findByModelName(modelType.modelName)
73         if (dbModelType != null) {
74             dbModel = dbModelType
75             dbModel.description = modelType.description
76             dbModel.definition = modelType.definition
77             dbModel.definitionType = modelType.definitionType
78             dbModel.derivedFrom = modelType.derivedFrom
79             dbModel.tags = modelType.tags
80             dbModel.version = modelType.version
81             dbModel.updatedBy = modelType.updatedBy
82             dbModel = modelTypeRepository.save(dbModel)
83         } else {
84             dbModel = modelTypeRepository.save(modelType)
85         }
86         return dbModel
87     }
88
89
90     /**
91      * This is a deleteByModelName service
92      *
93      * @param modelName modelName
94      */
95     suspend open fun deleteByModelName(modelName: String) {
96         check(modelName.isNotBlank()) { "Model Name Information is missing." }
97         modelTypeRepository.deleteByModelName(modelName)
98
99     }
100
101     /**
102      * This is a getModelTypeByDefinitionType service
103      *
104      * @param definitionType definitionType
105      * @return List<ModelType>
106      */
107     suspend fun getModelTypeByDefinitionType(definitionType: String): List<ModelType> {
108         check(definitionType.isNotBlank()) { "Model definitionType Information is missing." }
109         return modelTypeRepository.findByDefinitionType(definitionType)
110     }
111
112     /**
113      * This is a getModelTypeByDerivedFrom service
114      *
115      * @param derivedFrom derivedFrom
116      * @return List<ModelType>
117      */
118     suspend fun getModelTypeByDerivedFrom(derivedFrom: String): List<ModelType> {
119         check(derivedFrom.isNotBlank()) { "Model derivedFrom Information is missing." }
120         return modelTypeRepository.findByDerivedFrom(derivedFrom)
121     }
122 }