Renaming Files having BluePrint to have Blueprint
[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(
53                 ErrorCatalogCodes.RESOURCE_NOT_FOUND, DesignerApiDomains.DESIGNER_API,
54                 String.format("couldn't get resource dictionary for name (%s)", name)
55             )
56         }
57     }
58
59     /**
60      * This is a searchResourceDictionaryByNames service
61      *
62      * @param names names
63      * @return List<ResourceDictionary>
64      </ResourceDictionary> */
65     suspend fun searchResourceDictionaryByNames(names: List<String>): List<ResourceDictionary> {
66         Preconditions.checkArgument(CollectionUtils.isNotEmpty(names), "No Search Information provide")
67         return resourceDictionaryRepository.findByNameIn(names)
68     }
69
70     /**
71      * This is a searchResourceDictionaryByTags service
72      *
73      * @param tags tags
74      * @return List<ResourceDictionary>
75      </ResourceDictionary> */
76     suspend fun searchResourceDictionaryByTags(tags: String): List<ResourceDictionary> {
77         Preconditions.checkArgument(StringUtils.isNotBlank(tags), "No search tag information provide")
78         return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags)
79     }
80
81     /**
82      * This is a saveDataDictionary service
83      *
84      * @param resourceDictionary resourceDictionary
85      * @return DataDictionary
86      */
87     @Throws(BlueprintException::class)
88     suspend fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
89         var resourceDictionary = resourceDictionary
90
91         val resourceDefinition = resourceDictionary.definition
92         Preconditions.checkNotNull(resourceDefinition, "failed to get resource definition from content")
93         // Validate the Resource Definitions
94         // TODO( Save Validator)
95         // validate(resourceDefinition)
96
97         resourceDictionary.tags = resourceDefinition.tags!!
98         resourceDefinition.updatedBy = resourceDictionary.updatedBy
99         resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
100         // Set the Property Definitions
101         val propertyDefinition = resourceDefinition.property
102         resourceDictionary.description = propertyDefinition.description!!
103         resourceDictionary.dataType = propertyDefinition.type
104         if (propertyDefinition.entrySchema != null) {
105             resourceDictionary.entrySchema = propertyDefinition.entrySchema!!.type
106         }
107
108         validateResourceDictionary(resourceDictionary)
109
110         val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
111         if (dbResourceDictionaryData != null) {
112             val dbResourceDictionary = dbResourceDictionaryData
113
114             dbResourceDictionary.name = resourceDictionary.name
115             dbResourceDictionary.definition = resourceDictionary.definition
116             dbResourceDictionary.description = resourceDictionary.description
117             dbResourceDictionary.tags = resourceDictionary.tags
118             dbResourceDictionary.resourceDictionaryGroup = resourceDictionary.resourceDictionaryGroup
119             dbResourceDictionary.updatedBy = resourceDictionary.updatedBy
120             dbResourceDictionary.dataType = resourceDictionary.dataType
121             dbResourceDictionary.entrySchema = resourceDictionary.entrySchema
122             resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary)
123         } else {
124             resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
125         }
126
127         return resourceDictionary
128     }
129
130     /**
131      * This is a saveDataDictionary service
132      *
133      * @param resourceDefinition ResourceDefinition
134      * @return ResourceDefinition
135      */
136     @Throws(BlueprintException::class)
137     suspend fun saveResourceDefinition(resourceDefinition: ResourceDefinition): ResourceDefinition {
138         val resourceDictionary = ResourceDictionary()
139         resourceDictionary.name = resourceDefinition.name
140         resourceDictionary.updatedBy = resourceDefinition.updatedBy
141         resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
142         resourceDictionary.entrySchema = resourceDefinition.property.entrySchema?.type
143         if (StringUtils.isBlank(resourceDefinition.tags)) {
144             resourceDictionary.tags = (
145                 resourceDefinition.name + ", " + resourceDefinition.updatedBy +
146                     ", " + resourceDefinition.updatedBy
147                 )
148         } else {
149             resourceDictionary.tags = resourceDefinition.tags!!
150         }
151         resourceDictionary.description = resourceDefinition.property.description!!
152         resourceDictionary.dataType = resourceDefinition.property.type
153         resourceDictionary.definition = resourceDefinition
154
155         validateResourceDictionary(resourceDictionary)
156
157         return resourceDictionaryRepository.save(resourceDictionary).definition
158     }
159
160     /**
161      * This is a deleteResourceDictionary service
162      *
163      * @param name name
164      */
165     suspend fun deleteResourceDictionary(name: String) {
166         check(name.isNotBlank()) { "Resource dictionary name is missing." }
167         resourceDictionaryRepository.deleteByName(name)
168     }
169
170     /**
171      * This is a getResourceSourceMapping service
172      */
173     suspend fun getResourceSourceMapping(): ResourceSourceMapping {
174         return ResourceSourceMappingFactory.getRegisterSourceMapping()
175     }
176
177     private fun validateResourceDictionary(resourceDictionary: ResourceDictionary): Boolean {
178         checkNotEmpty(resourceDictionary.name) { "DataDictionary Definition name is missing." }
179         checkNotNull(resourceDictionary.definition) { "DataDictionary Definition Information is missing." }
180         checkNotEmpty(resourceDictionary.description) { "DataDictionary Definition Information is missing." }
181         checkNotEmpty(resourceDictionary.tags) { "DataDictionary Definition tags is missing." }
182         checkNotEmpty(resourceDictionary.updatedBy) { "DataDictionary Definition updatedBy is missing." }
183         return true
184     }
185
186     fun getResourceDictionaryDistinct(): List<String> {
187         return resourceDictionaryRepository.findDistinctByResourceDictionaryGroup()
188     }
189 }