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.collections4.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
34 import org.springframework.data.domain.Page
35 import org.springframework.data.domain.Pageable
38 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository) {
41 * This is a getResourceDictionaryByName service
44 * @return ResourceDictionary
45 * @throws BluePrintException BluePrintException
47 @Throws(BluePrintException::class)
48 suspend fun getResourceDictionaryByName(name: String): ResourceDictionary {
49 Preconditions.checkArgument(StringUtils.isNotBlank(name), "Resource dictionary Name Information is missing.")
50 val resourceDictionaryDb = resourceDictionaryRepository.findByName(name)
51 return if (resourceDictionaryDb != null) {
54 throw httpProcessorException(
55 ErrorCatalogCodes.RESOURCE_NOT_FOUND, DesignerApiDomains.DESIGNER_API,
56 String.format("couldn't get resource dictionary for name (%s)", name)
62 * This is a searchResourceDictionaryByNames service
65 * @return List<ResourceDictionary>
67 suspend fun searchResourceDictionaryByNames(names: List<String>): List<ResourceDictionary> {
68 Preconditions.checkArgument(CollectionUtils.isNotEmpty(names), "No Search Information provide")
69 return resourceDictionaryRepository.findByNameIn(names)
73 * This is a searchResourceDictionaryByTags service
76 * @return List<ResourceDictionary>
78 suspend fun searchResourceDictionaryByTags(tags: String): List<ResourceDictionary> {
79 Preconditions.checkArgument(StringUtils.isNotBlank(tags), "No search tag information provide")
80 return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags)
84 * This is a saveResourceDictionary service
86 * @param resourceDictionary resourceDictionary
87 * @return ResourceDictionary
89 @Throws(BluePrintException::class)
90 suspend fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
91 var resourceDictionary = resourceDictionary
93 val resourceDefinition = resourceDictionary.definition
94 Preconditions.checkNotNull(resourceDefinition, "failed to get resource definition from content")
95 // Validate the Resource Definitions
96 // TODO( Save Validator)
97 // validate(resourceDefinition)
99 resourceDictionary.tags = resourceDefinition.tags!!
100 resourceDefinition.updatedBy = resourceDictionary.updatedBy
101 resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
102 // Set the Property Definitions
103 val propertyDefinition = resourceDefinition.property
104 resourceDictionary.description = propertyDefinition.description!!
105 resourceDictionary.dataType = propertyDefinition.type
106 if (propertyDefinition.entrySchema != null) {
107 resourceDictionary.entrySchema = propertyDefinition.entrySchema!!.type
110 validateResourceDictionary(resourceDictionary)
112 val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
113 if (dbResourceDictionaryData != null) {
114 val dbResourceDictionary = dbResourceDictionaryData
116 dbResourceDictionary.name = resourceDictionary.name
117 dbResourceDictionary.definition = resourceDictionary.definition
118 dbResourceDictionary.description = resourceDictionary.description
119 dbResourceDictionary.tags = resourceDictionary.tags
120 dbResourceDictionary.resourceDictionaryGroup = resourceDictionary.resourceDictionaryGroup
121 dbResourceDictionary.updatedBy = resourceDictionary.updatedBy
122 dbResourceDictionary.dataType = resourceDictionary.dataType
123 dbResourceDictionary.entrySchema = resourceDictionary.entrySchema
124 resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary)
126 resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
129 return resourceDictionary
133 * This is to save single ResourceDictionary from json content
135 * @param resourceDefinition ResourceDefinition
136 * @return ResourceDictionary
138 @Throws(BluePrintException::class)
139 suspend fun saveResourceDefinition(resourceDefinition: ResourceDefinition): ResourceDictionary {
140 return resourceDictionaryRepository.save(enrichResourceDictionary(resourceDefinition))
144 * This is to save multiple ResourceDictionaries from json array content
146 * @param resourceDefinitionList List<ResourceDefinition>
147 * @return MutableList<ResourceDictionary>
149 @Throws(BluePrintException::class)
150 suspend fun saveAllResourceDefinition(resourceDefinitionList: List<ResourceDefinition>): MutableList<ResourceDictionary> {
151 val dictionaryList: MutableList<ResourceDictionary> = mutableListOf()
152 resourceDefinitionList.forEach {
153 dictionaryList.add(enrichResourceDictionary(it))
155 return resourceDictionaryRepository.saveAll(dictionaryList)
158 private fun enrichResourceDictionary(resourceDefinition: ResourceDefinition): ResourceDictionary {
159 val resourceDictionary = ResourceDictionary()
160 resourceDictionary.name = resourceDefinition.name
161 resourceDictionary.updatedBy = resourceDefinition.updatedBy
162 resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
163 resourceDictionary.entrySchema = resourceDefinition.property.entrySchema?.type
164 resourceDictionary.description = resourceDefinition.property.description!!
165 resourceDictionary.dataType = resourceDefinition.property.type
166 resourceDictionary.definition = resourceDefinition
168 if (StringUtils.isBlank(resourceDefinition.tags)) {
169 resourceDictionary.tags = resourceDefinition.name + ", " + resourceDefinition.updatedBy
171 resourceDictionary.tags = resourceDefinition.tags!!
174 validateResourceDictionary(resourceDictionary)
175 return resourceDictionary
179 * This is a deleteResourceDictionary service
183 suspend fun deleteResourceDictionary(name: String) {
184 check(name.isNotBlank()) { "Resource dictionary name is missing." }
185 resourceDictionaryRepository.deleteByName(name)
189 * This is a getResourceSourceMapping service
191 suspend fun getResourceSourceMapping(): ResourceSourceMapping {
192 return ResourceSourceMappingFactory.getRegisterSourceMapping()
196 * This is a getResourceDirectories service
198 suspend fun getAllDictionary(pageRequest: Pageable): Page<ResourceDictionary> {
199 return resourceDictionaryRepository.findAll(pageRequest)
202 private fun validateResourceDictionary(resourceDictionary: ResourceDictionary): Boolean {
203 checkNotEmpty(resourceDictionary.name) { "ResourceDictionary Definition name is missing." }
204 checkNotNull(resourceDictionary.definition) { "ResourceDictionary Definition Information is missing." }
205 checkNotEmpty(resourceDictionary.description) { "ResourceDictionary Definition Information is missing." }
206 checkNotEmpty(resourceDictionary.tags) { "ResourceDictionary Definition tags is missing." }
207 checkNotEmpty(resourceDictionary.updatedBy) { "ResourceDictionary Definition updatedBy is missing." }
211 fun getResourceDictionaryDistinct(): List<String> {
212 return resourceDictionaryRepository.findDistinctByResourceDictionaryGroup()