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.domain.ModelType
21 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ModelTypeRepository
22 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.ModelTypeValidator
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
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)")
50 * This is a searchModelTypes service
53 * @return List<ModelType>
55 suspend fun searchModelTypes(tags: String): List<ModelType> {
56 check(tags.isNotBlank()) { "No Search Information provide" }
57 return modelTypeRepository.findByTagsContainingIgnoreCase(tags)
61 * This is a saveModel service
63 * @param modelType modelType
65 * @throws BluePrintException BluePrintException
67 @Throws(BluePrintException::class)
68 open suspend fun saveModel(modelType: ModelType): ModelType {
69 lateinit var dbModel: ModelType
70 ModelTypeValidator.validateModelType(modelType)
71 val dbModelType: ModelType? = modelTypeRepository.findByModelName(modelType.modelName)
72 if (dbModelType != null) {
74 dbModel.description = modelType.description
75 dbModel.definition = modelType.definition
76 dbModel.definitionType = modelType.definitionType
77 dbModel.derivedFrom = modelType.derivedFrom
78 dbModel.tags = modelType.tags
79 dbModel.version = modelType.version
80 dbModel.updatedBy = modelType.updatedBy
81 dbModel = modelTypeRepository.save(dbModel)
83 dbModel = modelTypeRepository.save(modelType)
89 * This is a deleteByModelName service
91 * @param modelName modelName
93 open suspend fun deleteByModelName(modelName: String) {
94 check(modelName.isNotBlank()) { "Model Name Information is missing." }
95 modelTypeRepository.deleteByModelName(modelName)
99 * This is a getModelTypeByDefinitionType service
101 * @param definitionType definitionType
102 * @return List<ModelType>
104 suspend fun getModelTypeByDefinitionType(definitionType: String): List<ModelType> {
105 check(definitionType.isNotBlank()) { "Model definitionType Information is missing." }
106 return modelTypeRepository.findByDefinitionType(definitionType)
110 * This is a getModelTypeByDerivedFrom service
112 * @param derivedFrom derivedFrom
113 * @return List<ModelType>
115 suspend fun getModelTypeByDerivedFrom(derivedFrom: String): List<ModelType> {
116 check(derivedFrom.isNotBlank()) { "Model derivedFrom Information is missing." }
117 return modelTypeRepository.findByDerivedFrom(derivedFrom)