Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / service / 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.apps.controllerblueprints.service.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.apps.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow
25 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceSourceMapping
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
27 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ResourceDictionary
28 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ResourceDictionaryRepository
29 import org.springframework.stereotype.Service
30
31 @Service
32 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository) {
33
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     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.isPresent) {
47             resourceDictionaryDb.get()
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     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     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     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         // 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.isPresent) {
105             val dbResourceDictionary = dbResourceDictionaryData.get()
106
107             dbResourceDictionary.name = resourceDictionary.name
108             dbResourceDictionary.definition = resourceDictionary.definition
109             dbResourceDictionary.description = resourceDictionary.description
110             dbResourceDictionary.tags = resourceDictionary.tags
111             dbResourceDictionary.updatedBy = resourceDictionary.updatedBy
112             dbResourceDictionary.dataType = resourceDictionary.dataType
113             dbResourceDictionary.entrySchema = resourceDictionary.entrySchema
114             resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary)
115         } else {
116             resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
117         }
118
119         return resourceDictionary
120     }
121
122     /**
123      * This is a deleteResourceDictionary service
124      *
125      * @param name name
126      */
127     fun deleteResourceDictionary(name: String) {
128         check(name.isNotBlank()) { "Resource dictionary name is missing." }
129         resourceDictionaryRepository.deleteByName(name)
130     }
131
132     /**
133      * This is a getResourceSourceMapping service
134      */
135     fun getResourceSourceMapping(): ResourceSourceMapping {
136         return ResourceSourceMappingFactory.getRegisterSourceMapping()
137     }
138
139     private fun validateResourceDictionary(resourceDictionary: ResourceDictionary): Boolean {
140         checkNotEmptyOrThrow(resourceDictionary.name, "DataDictionary Definition name is missing.")
141         checkNotNull(resourceDictionary.definition) { "DataDictionary Definition Information is missing." }
142         checkNotEmptyOrThrow(resourceDictionary.description, "DataDictionary Definition Information is missing.")
143         checkNotEmptyOrThrow(resourceDictionary.tags, "DataDictionary Definition tags is missing.")
144         checkNotEmptyOrThrow(resourceDictionary.updatedBy, "DataDictionary Definition updatedBy is missing.")
145         return true
146
147     }
148 }