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