Fixnig file import in script, import and template&mapping.
[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.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.ResourceDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceSourceMapping
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
30 import org.springframework.stereotype.Service
31
32 @Service
33 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository) {
34
35     /**
36      * This is a getDataDictionaryByName service
37      *
38      * @param name name
39      * @return DataDictionary
40      * @throws BluePrintException BluePrintException
41      */
42     @Throws(BluePrintException::class)
43     suspend fun getResourceDictionaryByName(name: String): ResourceDictionary {
44         Preconditions.checkArgument(StringUtils.isNotBlank(name), "Resource dictionary Name Information is missing.")
45         val resourceDictionaryDb = resourceDictionaryRepository.findByName(name)
46         return if (resourceDictionaryDb != null) {
47             resourceDictionaryDb
48         } else {
49             throw BluePrintException(String.format("couldn't get resource dictionary for name (%s)", name))
50         }
51     }
52
53     /**
54      * This is a searchResourceDictionaryByNames service
55      *
56      * @param names names
57      * @return List<ResourceDictionary>
58     </ResourceDictionary> */
59     suspend fun searchResourceDictionaryByNames(names: List<String>): List<ResourceDictionary> {
60         Preconditions.checkArgument(CollectionUtils.isNotEmpty(names), "No Search Information provide")
61         return resourceDictionaryRepository.findByNameIn(names)
62     }
63
64     /**
65      * This is a searchResourceDictionaryByTags service
66      *
67      * @param tags tags
68      * @return List<ResourceDictionary>
69     </ResourceDictionary> */
70     suspend fun searchResourceDictionaryByTags(tags: String): List<ResourceDictionary> {
71         Preconditions.checkArgument(StringUtils.isNotBlank(tags), "No search tag information provide")
72         return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags)
73     }
74
75     /**
76      * This is a saveDataDictionary service
77      *
78      * @param resourceDictionary resourceDictionary
79      * @return DataDictionary
80      */
81     @Throws(BluePrintException::class)
82     suspend fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
83         var resourceDictionary = resourceDictionary
84
85         val resourceDefinition = resourceDictionary.definition
86         Preconditions.checkNotNull(resourceDefinition, "failed to get resource definition from content")
87         // Validate the Resource Definitions
88         // TODO( Save Validator)
89         // validate(resourceDefinition)
90
91         resourceDictionary.tags = resourceDefinition.tags!!
92         resourceDefinition.updatedBy = resourceDictionary.updatedBy
93         resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
94         // Set the Property Definitions
95         val propertyDefinition = resourceDefinition.property
96         resourceDictionary.description = propertyDefinition.description!!
97         resourceDictionary.dataType = propertyDefinition.type
98         if (propertyDefinition.entrySchema != null) {
99             resourceDictionary.entrySchema = propertyDefinition.entrySchema!!.type
100         }
101
102         validateResourceDictionary(resourceDictionary)
103
104         val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
105         if (dbResourceDictionaryData != null) {
106             val dbResourceDictionary = dbResourceDictionaryData
107
108             dbResourceDictionary.name = resourceDictionary.name
109             dbResourceDictionary.definition = resourceDictionary.definition
110             dbResourceDictionary.description = resourceDictionary.description
111             dbResourceDictionary.tags = resourceDictionary.tags
112             dbResourceDictionary.resourceDictionaryGroup = resourceDictionary.resourceDictionaryGroup
113             dbResourceDictionary.updatedBy = resourceDictionary.updatedBy
114             dbResourceDictionary.dataType = resourceDictionary.dataType
115             dbResourceDictionary.entrySchema = resourceDictionary.entrySchema
116             resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary)
117         } else {
118             resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
119         }
120
121         return resourceDictionary
122     }
123
124     /**
125      * This is a saveDataDictionary service
126      *
127      * @param resourceDefinition ResourceDefinition
128      * @return ResourceDefinition
129      */
130     @Throws(BluePrintException::class)
131     suspend fun saveResourceDefinition(resourceDefinition: ResourceDefinition): ResourceDefinition {
132         val resourceDictionary = ResourceDictionary()
133         resourceDictionary.name = resourceDefinition.name
134         resourceDictionary.updatedBy = resourceDefinition.updatedBy
135         resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
136         resourceDictionary.entrySchema = resourceDefinition.property.entrySchema?.type
137         if (StringUtils.isBlank(resourceDefinition.tags)) {
138             resourceDictionary.tags = (resourceDefinition.name + ", " + resourceDefinition.updatedBy +
139                     ", " + resourceDefinition.updatedBy)
140         } else {
141             resourceDictionary.tags = resourceDefinition.tags!!
142         }
143         resourceDictionary.description = resourceDefinition.property.description!!
144         resourceDictionary.dataType = resourceDefinition.property.type
145         resourceDictionary.definition = resourceDefinition
146
147         validateResourceDictionary(resourceDictionary)
148
149         return resourceDictionaryRepository.save(resourceDictionary).definition
150     }
151
152     /**
153      * This is a deleteResourceDictionary service
154      *
155      * @param name name
156      */
157     suspend fun deleteResourceDictionary(name: String) {
158         check(name.isNotBlank()) { "Resource dictionary name is missing." }
159         resourceDictionaryRepository.deleteByName(name)
160     }
161
162     /**
163      * This is a getResourceSourceMapping service
164      */
165     suspend fun getResourceSourceMapping(): ResourceSourceMapping {
166         return ResourceSourceMappingFactory.getRegisterSourceMapping()
167     }
168
169     private fun validateResourceDictionary(resourceDictionary: ResourceDictionary): Boolean {
170         checkNotEmpty(resourceDictionary.name) { "DataDictionary Definition name is missing." }
171         checkNotNull(resourceDictionary.definition) { "DataDictionary Definition Information is missing." }
172         checkNotEmpty(resourceDictionary.description) { "DataDictionary Definition Information is missing." }
173         checkNotEmpty(resourceDictionary.tags) { "DataDictionary Definition tags is missing." }
174         checkNotEmpty(resourceDictionary.updatedBy) { "DataDictionary Definition updatedBy is missing." }
175         return true
176     }
177
178     fun getResourceDictionaryDistinct(): List<String> {
179         return resourceDictionaryRepository.findDistinctByResourceDictionaryGroup()
180     }
181 }