d7563e821ca339d58bd5d9060e4e0998aa849f78
[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         resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
93         // Set the Property Definitions
94         val propertyDefinition = resourceDefinition.property
95         resourceDictionary.description = propertyDefinition.description!!
96         resourceDictionary.dataType = propertyDefinition.type
97         if (propertyDefinition.entrySchema != null) {
98             resourceDictionary.entrySchema = propertyDefinition.entrySchema!!.type
99         }
100
101         validateResourceDictionary(resourceDictionary)
102
103         val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
104         if (dbResourceDictionaryData != null) {
105             val dbResourceDictionary = dbResourceDictionaryData
106
107             dbResourceDictionary.name = resourceDictionary.name
108             dbResourceDictionary.definition = resourceDictionary.definition
109             dbResourceDictionary.description = resourceDictionary.description
110             dbResourceDictionary.tags = resourceDictionary.tags
111             dbResourceDictionary.resourceDictionaryGroup = resourceDictionary.resourceDictionaryGroup
112             dbResourceDictionary.updatedBy = resourceDictionary.updatedBy
113             dbResourceDictionary.dataType = resourceDictionary.dataType
114             dbResourceDictionary.entrySchema = resourceDictionary.entrySchema
115             resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary)
116         } else {
117             resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
118         }
119
120         return resourceDictionary
121     }
122
123     /**
124      * This is a deleteResourceDictionary service
125      *
126      * @param name name
127      */
128     suspend fun deleteResourceDictionary(name: String) {
129         check(name.isNotBlank()) { "Resource dictionary name is missing." }
130         resourceDictionaryRepository.deleteByName(name)
131     }
132
133     /**
134      * This is a getResourceSourceMapping service
135      */
136     suspend fun getResourceSourceMapping(): ResourceSourceMapping {
137         return ResourceSourceMappingFactory.getRegisterSourceMapping()
138     }
139
140     private fun validateResourceDictionary(resourceDictionary: ResourceDictionary): Boolean {
141         checkNotEmpty(resourceDictionary.name) { "DataDictionary Definition name is missing." }
142         checkNotNull(resourceDictionary.definition) { "DataDictionary Definition Information is missing." }
143         checkNotEmpty(resourceDictionary.description) { "DataDictionary Definition Information is missing." }
144         checkNotEmpty(resourceDictionary.tags) { "DataDictionary Definition tags is missing." }
145         checkNotEmpty(resourceDictionary.updatedBy) { "DataDictionary Definition updatedBy is missing." }
146         return true
147     }
148
149     fun getResourceDictionaryDistinct(): List<String> {
150         return resourceDictionaryRepository.findDistinctByResourceDictionaryGroup()
151     }
152 }