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.DesignerApiDomains
21 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ModelType
22 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ModelTypeRepository
23 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.ModelTypeValidator
24 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
25 import org.onap.ccsdk.cds.controllerblueprints.core.httpProcessorException
26 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
27 import org.slf4j.LoggerFactory
28 import org.springframework.stereotype.Service
31 open class ModelTypeHandler(private val modelTypeRepository: ModelTypeRepository) {
33 private val log = LoggerFactory.getLogger(ModelTypeHandler::class.java)!!
36 * This is a getModelTypeByName service
38 * @param modelTypeName modelTypeName
41 suspend fun getModelTypeByName(modelTypeName: String): ModelType {
42 log.info("Searching : $modelTypeName")
43 check(modelTypeName.isNotBlank()) { "Model Name Information is missing." }
44 val modelType = modelTypeRepository.findByModelName(modelTypeName)
45 return if (modelType != null) {
48 throw httpProcessorException(
49 ErrorCatalogCodes.RESOURCE_NOT_FOUND, DesignerApiDomains.DESIGNER_API,
50 "couldn't get modelType($modelTypeName)"
56 * This is a searchModelTypes service
59 * @return List<ModelType>
61 suspend fun searchModelTypes(tags: String): List<ModelType> {
62 check(tags.isNotBlank()) { "No Search Information provide" }
63 return modelTypeRepository.findByTagsContainingIgnoreCase(tags)
67 * This is a saveModel service
69 * @param modelType modelType
71 * @throws BlueprintException BlueprintException
73 @Throws(BlueprintException::class)
74 open suspend fun saveModel(modelType: ModelType): ModelType {
75 lateinit var dbModel: ModelType
76 ModelTypeValidator.validateModelType(modelType)
77 val dbModelType: ModelType? = modelTypeRepository.findByModelName(modelType.modelName)
78 if (dbModelType != null) {
80 dbModel.description = modelType.description
81 dbModel.definition = modelType.definition
82 dbModel.definitionType = modelType.definitionType
83 dbModel.derivedFrom = modelType.derivedFrom
84 dbModel.tags = modelType.tags
85 dbModel.version = modelType.version
86 dbModel.updatedBy = modelType.updatedBy
87 dbModel = modelTypeRepository.save(dbModel)
89 dbModel = modelTypeRepository.save(modelType)
95 * This is a deleteByModelName service
97 * @param modelName modelName
99 open suspend fun deleteByModelName(modelName: String) {
100 check(modelName.isNotBlank()) { "Model Name Information is missing." }
101 modelTypeRepository.deleteByModelName(modelName)
105 * This is a getModelTypeByDefinitionType service
107 * @param definitionType definitionType
108 * @return List<ModelType>
110 suspend fun getModelTypeByDefinitionType(definitionType: String): List<ModelType> {
111 check(definitionType.isNotBlank()) { "Model definitionType Information is missing." }
112 return modelTypeRepository.findByDefinitionType(definitionType)
116 * This is a getModelTypeByDerivedFrom service
118 * @param derivedFrom derivedFrom
119 * @return List<ModelType>
121 suspend fun getModelTypeByDerivedFrom(derivedFrom: String): List<ModelType> {
122 check(derivedFrom.isNotBlank()) { "Model derivedFrom Information is missing." }
123 return modelTypeRepository.findByDerivedFrom(derivedFrom)