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.domain.ResourceDictionary
24 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ResourceDictionaryRepository
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
27 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceSourceMapping
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
30 import org.springframework.stereotype.Service
33 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository) {
36 * This is a getDataDictionaryByName service
39 * @return DataDictionary
40 * @throws BluePrintException BluePrintException
42 @Throws(BluePrintException::class)
43 suspend fun getResourceDictionaryByName(name: String): ResourceDictionary {
44 Preconditions.checkArgument(StringUtils.isNotBlank(name), "Resource dictionary Name Information is missing.")
45 val resourceDictionaryDb = resourceDictionaryRepository.findByName(name)
46 return if (resourceDictionaryDb != null) {
49 throw BluePrintException(String.format("couldn't get resource dictionary for name (%s)", name))
54 * This is a searchResourceDictionaryByNames service
57 * @return List<ResourceDictionary>
58 </ResourceDictionary> */
59 suspend fun searchResourceDictionaryByNames(names: List<String>): List<ResourceDictionary> {
60 Preconditions.checkArgument(CollectionUtils.isNotEmpty(names), "No Search Information provide")
61 return resourceDictionaryRepository.findByNameIn(names)
65 * This is a searchResourceDictionaryByTags service
68 * @return List<ResourceDictionary>
69 </ResourceDictionary> */
70 suspend fun searchResourceDictionaryByTags(tags: String): List<ResourceDictionary> {
71 Preconditions.checkArgument(StringUtils.isNotBlank(tags), "No search tag information provide")
72 return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags)
76 * This is a saveDataDictionary service
78 * @param resourceDictionary resourceDictionary
79 * @return DataDictionary
81 @Throws(BluePrintException::class)
82 suspend fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
83 var resourceDictionary = resourceDictionary
85 val resourceDefinition = resourceDictionary.definition
86 Preconditions.checkNotNull(resourceDefinition, "failed to get resource definition from content")
87 // Validate the Resource Definitions
88 // TODO( Save Validator)
89 // validate(resourceDefinition)
91 resourceDictionary.tags = resourceDefinition.tags!!
92 resourceDefinition.updatedBy = resourceDictionary.updatedBy
93 resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
94 // Set the Property Definitions
95 val propertyDefinition = resourceDefinition.property
96 resourceDictionary.description = propertyDefinition.description!!
97 resourceDictionary.dataType = propertyDefinition.type
98 if (propertyDefinition.entrySchema != null) {
99 resourceDictionary.entrySchema = propertyDefinition.entrySchema!!.type
102 validateResourceDictionary(resourceDictionary)
104 val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
105 if (dbResourceDictionaryData != null) {
106 val dbResourceDictionary = dbResourceDictionaryData
108 dbResourceDictionary.name = resourceDictionary.name
109 dbResourceDictionary.definition = resourceDictionary.definition
110 dbResourceDictionary.description = resourceDictionary.description
111 dbResourceDictionary.tags = resourceDictionary.tags
112 dbResourceDictionary.resourceDictionaryGroup = resourceDictionary.resourceDictionaryGroup
113 dbResourceDictionary.updatedBy = resourceDictionary.updatedBy
114 dbResourceDictionary.dataType = resourceDictionary.dataType
115 dbResourceDictionary.entrySchema = resourceDictionary.entrySchema
116 resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary)
118 resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
121 return resourceDictionary
125 * This is a saveDataDictionary service
127 * @param resourceDefinition ResourceDefinition
128 * @return ResourceDefinition
130 @Throws(BluePrintException::class)
131 suspend fun saveResourceDefinition(resourceDefinition: ResourceDefinition): ResourceDefinition {
132 val resourceDictionary = ResourceDictionary()
133 resourceDictionary.name = resourceDefinition.name
134 resourceDictionary.updatedBy = resourceDefinition.updatedBy
135 resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
136 resourceDictionary.entrySchema = resourceDefinition.property.entrySchema?.type
137 if (StringUtils.isBlank(resourceDefinition.tags)) {
138 resourceDictionary.tags = (resourceDefinition.name + ", " + resourceDefinition.updatedBy +
139 ", " + resourceDefinition.updatedBy)
141 resourceDictionary.tags = resourceDefinition.tags!!
143 resourceDictionary.description = resourceDefinition.property.description!!
144 resourceDictionary.dataType = resourceDefinition.property.type
145 resourceDictionary.definition = resourceDefinition
147 validateResourceDictionary(resourceDictionary)
149 return resourceDictionaryRepository.save(resourceDictionary).definition
153 * This is a deleteResourceDictionary service
157 suspend fun deleteResourceDictionary(name: String) {
158 check(name.isNotBlank()) { "Resource dictionary name is missing." }
159 resourceDictionaryRepository.deleteByName(name)
163 * This is a getResourceSourceMapping service
165 suspend fun getResourceSourceMapping(): ResourceSourceMapping {
166 return ResourceSourceMappingFactory.getRegisterSourceMapping()
169 private fun validateResourceDictionary(resourceDictionary: ResourceDictionary): Boolean {
170 checkNotEmpty(resourceDictionary.name) { "DataDictionary Definition name is missing." }
171 checkNotNull(resourceDictionary.definition) { "DataDictionary Definition Information is missing." }
172 checkNotEmpty(resourceDictionary.description) { "DataDictionary Definition Information is missing." }
173 checkNotEmpty(resourceDictionary.tags) { "DataDictionary Definition tags is missing." }
174 checkNotEmpty(resourceDictionary.updatedBy) { "DataDictionary Definition updatedBy is missing." }
178 fun getResourceDictionaryDistinct(): List<String> {
179 return resourceDictionaryRepository.findDistinctByResourceDictionaryGroup()