2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler
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
28 open class ModelTypeHandler(private val modelTypeRepository: ModelTypeRepository) {
30 private val log = LoggerFactory.getLogger(ModelTypeHandler::class.java)!!
33 * This is a getModelTypeByName service
35 * @param modelTypeName modelTypeName
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) {
45 throw BluePrintException("couldn't get modelType($modelTypeName)")
51 * This is a searchModelTypes service
54 * @return List<ModelType>
56 suspend fun searchModelTypes(tags: String): List<ModelType> {
57 check(tags.isNotBlank()) { "No Search Information provide" }
58 return modelTypeRepository.findByTagsContainingIgnoreCase(tags)
62 * This is a saveModel service
64 * @param modelType modelType
66 * @throws BluePrintException BluePrintException
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) {
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)
84 dbModel = modelTypeRepository.save(modelType)
91 * This is a deleteByModelName service
93 * @param modelName modelName
95 suspend open fun deleteByModelName(modelName: String) {
96 check(modelName.isNotBlank()) { "Model Name Information is missing." }
97 modelTypeRepository.deleteByModelName(modelName)
102 * This is a getModelTypeByDefinitionType service
104 * @param definitionType definitionType
105 * @return List<ModelType>
107 suspend fun getModelTypeByDefinitionType(definitionType: String): List<ModelType> {
108 check(definitionType.isNotBlank()) { "Model definitionType Information is missing." }
109 return modelTypeRepository.findByDefinitionType(definitionType)
113 * This is a getModelTypeByDerivedFrom service
115 * @param derivedFrom derivedFrom
116 * @return List<ModelType>
118 suspend fun getModelTypeByDerivedFrom(derivedFrom: String): List<ModelType> {
119 check(derivedFrom.isNotBlank()) { "Model derivedFrom Information is missing." }
120 return modelTypeRepository.findByDerivedFrom(derivedFrom)