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.ResourceSourceMapping
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
29 import org.springframework.stereotype.Service
32 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository) {
35 * This is a getDataDictionaryByName service
38 * @return DataDictionary
39 * @throws BluePrintException BluePrintException
41 @Throws(BluePrintException::class)
42 suspend fun getResourceDictionaryByName(name: String): ResourceDictionary {
43 Preconditions.checkArgument(StringUtils.isNotBlank(name), "Resource dictionary Name Information is missing.")
44 val resourceDictionaryDb = resourceDictionaryRepository.findByName(name)
45 return if (resourceDictionaryDb != null) {
48 throw BluePrintException(String.format("couldn't get resource dictionary for name (%s)", name))
53 * This is a searchResourceDictionaryByNames service
56 * @return List<ResourceDictionary>
57 </ResourceDictionary> */
58 suspend fun searchResourceDictionaryByNames(names: List<String>): List<ResourceDictionary> {
59 Preconditions.checkArgument(CollectionUtils.isNotEmpty(names), "No Search Information provide")
60 return resourceDictionaryRepository.findByNameIn(names)
64 * This is a searchResourceDictionaryByTags service
67 * @return List<ResourceDictionary>
68 </ResourceDictionary> */
69 suspend fun searchResourceDictionaryByTags(tags: String): List<ResourceDictionary> {
70 Preconditions.checkArgument(StringUtils.isNotBlank(tags), "No search tag information provide")
71 return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags)
75 * This is a saveDataDictionary service
77 * @param resourceDictionary resourceDictionary
78 * @return DataDictionary
80 @Throws(BluePrintException::class)
81 suspend fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
82 var resourceDictionary = resourceDictionary
84 val resourceDefinition = resourceDictionary.definition
85 Preconditions.checkNotNull(resourceDefinition, "failed to get resource definition from content")
86 // Validate the Resource Definitions
87 // TODO( Save Validator)
88 // validate(resourceDefinition)
90 resourceDictionary.tags = resourceDefinition.tags!!
91 resourceDefinition.updatedBy = resourceDictionary.updatedBy
92 resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
93 // Set the Property Definitions
94 val propertyDefinition = resourceDefinition.property
95 resourceDictionary.description = propertyDefinition.description!!
96 resourceDictionary.dataType = propertyDefinition.type
97 if (propertyDefinition.entrySchema != null) {
98 resourceDictionary.entrySchema = propertyDefinition.entrySchema!!.type
101 validateResourceDictionary(resourceDictionary)
103 val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
104 if (dbResourceDictionaryData != null) {
105 val dbResourceDictionary = dbResourceDictionaryData
107 dbResourceDictionary.name = resourceDictionary.name
108 dbResourceDictionary.definition = resourceDictionary.definition
109 dbResourceDictionary.description = resourceDictionary.description
110 dbResourceDictionary.tags = resourceDictionary.tags
111 dbResourceDictionary.resourceDictionaryGroup = resourceDictionary.resourceDictionaryGroup
112 dbResourceDictionary.updatedBy = resourceDictionary.updatedBy
113 dbResourceDictionary.dataType = resourceDictionary.dataType
114 dbResourceDictionary.entrySchema = resourceDictionary.entrySchema
115 resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary)
117 resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
120 return resourceDictionary
124 * This is a deleteResourceDictionary service
128 suspend fun deleteResourceDictionary(name: String) {
129 check(name.isNotBlank()) { "Resource dictionary name is missing." }
130 resourceDictionaryRepository.deleteByName(name)
134 * This is a getResourceSourceMapping service
136 suspend fun getResourceSourceMapping(): ResourceSourceMapping {
137 return ResourceSourceMappingFactory.getRegisterSourceMapping()
140 private fun validateResourceDictionary(resourceDictionary: ResourceDictionary): Boolean {
141 checkNotEmpty(resourceDictionary.name) { "DataDictionary Definition name is missing." }
142 checkNotNull(resourceDictionary.definition) { "DataDictionary Definition Information is missing." }
143 checkNotEmpty(resourceDictionary.description) { "DataDictionary Definition Information is missing." }
144 checkNotEmpty(resourceDictionary.tags) { "DataDictionary Definition tags is missing." }
145 checkNotEmpty(resourceDictionary.updatedBy) { "DataDictionary Definition updatedBy is missing." }
149 fun getResourceDictionaryDistinct(): List<String> {
150 return resourceDictionaryRepository.findDistinctByResourceDictionaryGroup()