20895efcebb212b0978020f0fbc01de3eeb3e908
[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
35 @Service
36 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository) {
37
38     /**
39      * This is a getDataDictionaryByName service
40      *
41      * @param name name
42      * @return DataDictionary
43      * @throws BluePrintException BluePrintException
44      */
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) {
50             resourceDictionaryDb
51         } else {
52             throw httpProcessorException(ErrorCatalogCodes.RESOURCE_NOT_FOUND, DesignerApiDomains.DESIGNER_API,
53                     String.format("couldn't get resource dictionary for name (%s)", name))
54         }
55     }
56
57     /**
58      * This is a searchResourceDictionaryByNames service
59      *
60      * @param names names
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)
66     }
67
68     /**
69      * This is a searchResourceDictionaryByTags service
70      *
71      * @param tags tags
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)
77     }
78
79     /**
80      * This is a saveDataDictionary service
81      *
82      * @param resourceDictionary resourceDictionary
83      * @return DataDictionary
84      */
85     @Throws(BluePrintException::class)
86     suspend fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
87         var resourceDictionary = resourceDictionary
88
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)
94
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
104         }
105
106         validateResourceDictionary(resourceDictionary)
107
108         val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
109         if (dbResourceDictionaryData != null) {
110             val dbResourceDictionary = dbResourceDictionaryData
111
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)
121         } else {
122             resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
123         }
124
125         return resourceDictionary
126     }
127
128     /**
129      * This is a saveDataDictionary service
130      *
131      * @param resourceDefinition ResourceDefinition
132      * @return ResourceDefinition
133      */
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)
144         } else {
145             resourceDictionary.tags = resourceDefinition.tags!!
146         }
147         resourceDictionary.description = resourceDefinition.property.description!!
148         resourceDictionary.dataType = resourceDefinition.property.type
149         resourceDictionary.definition = resourceDefinition
150
151         validateResourceDictionary(resourceDictionary)
152
153         return resourceDictionaryRepository.save(resourceDictionary).definition
154     }
155
156     /**
157      * This is a deleteResourceDictionary service
158      *
159      * @param name name
160      */
161     suspend fun deleteResourceDictionary(name: String) {
162         check(name.isNotBlank()) { "Resource dictionary name is missing." }
163         resourceDictionaryRepository.deleteByName(name)
164     }
165
166     /**
167      * This is a getResourceSourceMapping service
168      */
169     suspend fun getResourceSourceMapping(): ResourceSourceMapping {
170         return ResourceSourceMappingFactory.getRegisterSourceMapping()
171     }
172
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." }
179         return true
180     }
181
182     fun getResourceDictionaryDistinct(): List<String> {
183         return resourceDictionaryRepository.findDistinctByResourceDictionaryGroup()
184     }
185 }