Adding some minor features
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / handler / ResourceDictionaryHandler.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler
19
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
34 import org.springframework.data.domain.Page
35 import org.springframework.data.domain.Pageable
36
37 @Service
38 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository) {
39
40     /**
41      * This is a getResourceDictionaryByName service
42      *
43      * @param name name
44      * @return ResourceDictionary
45      * @throws BluePrintException BluePrintException
46      */
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) {
52             resourceDictionaryDb
53         } else {
54             throw httpProcessorException(
55                 ErrorCatalogCodes.RESOURCE_NOT_FOUND, DesignerApiDomains.DESIGNER_API,
56                 String.format("couldn't get resource dictionary for name (%s)", name)
57             )
58         }
59     }
60
61     /**
62      * This is a searchResourceDictionaryByNames service
63      *
64      * @param names names
65      * @return List<ResourceDictionary>
66      */
67     suspend fun searchResourceDictionaryByNames(names: List<String>): List<ResourceDictionary> {
68         Preconditions.checkArgument(CollectionUtils.isNotEmpty(names), "No Search Information provide")
69         return resourceDictionaryRepository.findByNameIn(names)
70     }
71
72     /**
73      * This is a searchResourceDictionaryByTags service
74      *
75      * @param tags tags
76      * @return List<ResourceDictionary>
77      */
78     suspend fun searchResourceDictionaryByTags(tags: String): List<ResourceDictionary> {
79         Preconditions.checkArgument(StringUtils.isNotBlank(tags), "No search tag information provide")
80         return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags)
81     }
82
83     /**
84      * This is a saveResourceDictionary service
85      *
86      * @param resourceDictionary resourceDictionary
87      * @return ResourceDictionary
88      */
89     @Throws(BluePrintException::class)
90     suspend fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
91         var resourceDictionary = resourceDictionary
92
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)
98
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
108         }
109
110         validateResourceDictionary(resourceDictionary)
111
112         val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
113         if (dbResourceDictionaryData != null) {
114             val dbResourceDictionary = dbResourceDictionaryData
115
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)
125         } else {
126             resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
127         }
128
129         return resourceDictionary
130     }
131
132     /**
133      * This is to save single ResourceDictionary from json content
134      *
135      * @param resourceDefinition ResourceDefinition
136      * @return ResourceDictionary
137      */
138     @Throws(BluePrintException::class)
139     suspend fun saveResourceDefinition(resourceDefinition: ResourceDefinition): ResourceDictionary {
140         return resourceDictionaryRepository.save(enrichResourceDictionary(resourceDefinition))
141     }
142
143     /**
144      * This is to save multiple ResourceDictionaries from json array content
145      *
146      * @param resourceDefinitionList List<ResourceDefinition>
147      * @return MutableList<ResourceDictionary>
148      */
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))
154         }
155         return resourceDictionaryRepository.saveAll(dictionaryList)
156     }
157
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
167
168         if (StringUtils.isBlank(resourceDefinition.tags)) {
169             resourceDictionary.tags = resourceDefinition.name + ", " + resourceDefinition.updatedBy
170         } else {
171             resourceDictionary.tags = resourceDefinition.tags!!
172         }
173
174         validateResourceDictionary(resourceDictionary)
175         return resourceDictionary
176     }
177
178     /**
179      * This is a deleteResourceDictionary service
180      *
181      * @param name name
182      */
183     suspend fun deleteResourceDictionary(name: String) {
184         check(name.isNotBlank()) { "Resource dictionary name is missing." }
185         resourceDictionaryRepository.deleteByName(name)
186     }
187
188     /**
189      * This is a getResourceSourceMapping service
190      */
191     suspend fun getResourceSourceMapping(): ResourceSourceMapping {
192         return ResourceSourceMappingFactory.getRegisterSourceMapping()
193     }
194
195     /**
196      * This is a getResourceDirectories service
197      */
198     suspend fun getAllDictionary(pageRequest: Pageable): Page<ResourceDictionary> {
199         return resourceDictionaryRepository.findAll(pageRequest)
200     }
201
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." }
208         return true
209     }
210
211     fun getResourceDictionaryDistinct(): List<String> {
212         return resourceDictionaryRepository.findDistinctByResourceDictionaryGroup()
213     }
214 }