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 com.google.common.base.Preconditions
21 import org.apache.commons.collections.CollectionUtils
22 import org.apache.commons.lang3.StringUtils
23 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.DesignerApiDomains
24 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ResourceDictionary
25 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ResourceDictionaryRepository
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
28 import org.onap.ccsdk.cds.controllerblueprints.core.httpProcessorException
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceSourceMapping
31 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
32 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
33 import org.springframework.stereotype.Service
36 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository) {
39 * This is a getDataDictionaryByName service
42 * @return DataDictionary
43 * @throws BluePrintException BluePrintException
45 @Throws(BluePrintException::class)
46 suspend fun getResourceDictionaryByName(name: String): ResourceDictionary {
47 Preconditions.checkArgument(StringUtils.isNotBlank(name), "Resource dictionary Name Information is missing.")
48 val resourceDictionaryDb = resourceDictionaryRepository.findByName(name)
49 return if (resourceDictionaryDb != null) {
52 throw httpProcessorException(ErrorCatalogCodes.RESOURCE_NOT_FOUND, DesignerApiDomains.DESIGNER_API,
53 String.format("couldn't get resource dictionary for name (%s)", name))
58 * This is a searchResourceDictionaryByNames service
61 * @return List<ResourceDictionary>
62 </ResourceDictionary> */
63 suspend fun searchResourceDictionaryByNames(names: List<String>): List<ResourceDictionary> {
64 Preconditions.checkArgument(CollectionUtils.isNotEmpty(names), "No Search Information provide")
65 return resourceDictionaryRepository.findByNameIn(names)
69 * This is a searchResourceDictionaryByTags service
72 * @return List<ResourceDictionary>
73 </ResourceDictionary> */
74 suspend fun searchResourceDictionaryByTags(tags: String): List<ResourceDictionary> {
75 Preconditions.checkArgument(StringUtils.isNotBlank(tags), "No search tag information provide")
76 return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags)
80 * This is a saveDataDictionary service
82 * @param resourceDictionary resourceDictionary
83 * @return DataDictionary
85 @Throws(BluePrintException::class)
86 suspend fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
87 var resourceDictionary = resourceDictionary
89 val resourceDefinition = resourceDictionary.definition
90 Preconditions.checkNotNull(resourceDefinition, "failed to get resource definition from content")
91 // Validate the Resource Definitions
92 // TODO( Save Validator)
93 // validate(resourceDefinition)
95 resourceDictionary.tags = resourceDefinition.tags!!
96 resourceDefinition.updatedBy = resourceDictionary.updatedBy
97 resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
98 // Set the Property Definitions
99 val propertyDefinition = resourceDefinition.property
100 resourceDictionary.description = propertyDefinition.description!!
101 resourceDictionary.dataType = propertyDefinition.type
102 if (propertyDefinition.entrySchema != null) {
103 resourceDictionary.entrySchema = propertyDefinition.entrySchema!!.type
106 validateResourceDictionary(resourceDictionary)
108 val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
109 if (dbResourceDictionaryData != null) {
110 val dbResourceDictionary = dbResourceDictionaryData
112 dbResourceDictionary.name = resourceDictionary.name
113 dbResourceDictionary.definition = resourceDictionary.definition
114 dbResourceDictionary.description = resourceDictionary.description
115 dbResourceDictionary.tags = resourceDictionary.tags
116 dbResourceDictionary.resourceDictionaryGroup = resourceDictionary.resourceDictionaryGroup
117 dbResourceDictionary.updatedBy = resourceDictionary.updatedBy
118 dbResourceDictionary.dataType = resourceDictionary.dataType
119 dbResourceDictionary.entrySchema = resourceDictionary.entrySchema
120 resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary)
122 resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
125 return resourceDictionary
129 * This is a saveDataDictionary service
131 * @param resourceDefinition ResourceDefinition
132 * @return ResourceDefinition
134 @Throws(BluePrintException::class)
135 suspend fun saveResourceDefinition(resourceDefinition: ResourceDefinition): ResourceDefinition {
136 val resourceDictionary = ResourceDictionary()
137 resourceDictionary.name = resourceDefinition.name
138 resourceDictionary.updatedBy = resourceDefinition.updatedBy
139 resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
140 resourceDictionary.entrySchema = resourceDefinition.property.entrySchema?.type
141 if (StringUtils.isBlank(resourceDefinition.tags)) {
142 resourceDictionary.tags = (resourceDefinition.name + ", " + resourceDefinition.updatedBy +
143 ", " + resourceDefinition.updatedBy)
145 resourceDictionary.tags = resourceDefinition.tags!!
147 resourceDictionary.description = resourceDefinition.property.description!!
148 resourceDictionary.dataType = resourceDefinition.property.type
149 resourceDictionary.definition = resourceDefinition
151 validateResourceDictionary(resourceDictionary)
153 return resourceDictionaryRepository.save(resourceDictionary).definition
157 * This is a deleteResourceDictionary service
161 suspend fun deleteResourceDictionary(name: String) {
162 check(name.isNotBlank()) { "Resource dictionary name is missing." }
163 resourceDictionaryRepository.deleteByName(name)
167 * This is a getResourceSourceMapping service
169 suspend fun getResourceSourceMapping(): ResourceSourceMapping {
170 return ResourceSourceMappingFactory.getRegisterSourceMapping()
173 private fun validateResourceDictionary(resourceDictionary: ResourceDictionary): Boolean {
174 checkNotEmpty(resourceDictionary.name) { "DataDictionary Definition name is missing." }
175 checkNotNull(resourceDictionary.definition) { "DataDictionary Definition Information is missing." }
176 checkNotEmpty(resourceDictionary.description) { "DataDictionary Definition Information is missing." }
177 checkNotEmpty(resourceDictionary.tags) { "DataDictionary Definition tags is missing." }
178 checkNotEmpty(resourceDictionary.updatedBy) { "DataDictionary Definition updatedBy is missing." }
182 fun getResourceDictionaryDistinct(): List<String> {
183 return resourceDictionaryRepository.findDistinctByResourceDictionaryGroup()