c249314845e6f386a604d9bbd8da71310dc1a1eb
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.service.handler
18
19 import com.google.common.base.Preconditions
20 import org.apache.commons.collections.CollectionUtils
21 import org.apache.commons.lang3.StringUtils
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceSourceMapping
24 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
25 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.service.ResourceDefinitionValidationService
26 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ResourceDictionary
27 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ResourceDictionaryRepository
28 import org.onap.ccsdk.apps.controllerblueprints.service.validator.ResourceDictionaryValidator
29 import org.springframework.stereotype.Service
30
31 @Service
32 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository,
33                                 private val resourceDictionaryValidationService: ResourceDefinitionValidationService) {
34
35
36     /**
37      * This is a getDataDictionaryByName service
38      *
39      * @param name name
40      * @return DataDictionary
41      * @throws BluePrintException BluePrintException
42      */
43     @Throws(BluePrintException::class)
44     fun getResourceDictionaryByName(name: String): ResourceDictionary {
45         Preconditions.checkArgument(StringUtils.isNotBlank(name), "Resource dictionary Name Information is missing.")
46         val resourceDictionaryDb = resourceDictionaryRepository.findByName(name)
47         return if (resourceDictionaryDb.isPresent) {
48             resourceDictionaryDb.get()
49         } else {
50             throw BluePrintException(String.format("couldn't get resource dictionary for name (%s)", name))
51         }
52     }
53
54     /**
55      * This is a searchResourceDictionaryByNames service
56      *
57      * @param names names
58      * @return List<ResourceDictionary>
59     </ResourceDictionary> */
60     fun searchResourceDictionaryByNames(names: List<String>): List<ResourceDictionary> {
61         Preconditions.checkArgument(CollectionUtils.isNotEmpty(names), "No Search Information provide")
62         return resourceDictionaryRepository.findByNameIn(names)
63     }
64
65     /**
66      * This is a searchResourceDictionaryByTags service
67      *
68      * @param tags tags
69      * @return List<ResourceDictionary>
70     </ResourceDictionary> */
71     fun searchResourceDictionaryByTags(tags: String): List<ResourceDictionary> {
72         Preconditions.checkArgument(StringUtils.isNotBlank(tags), "No search tag information provide")
73         return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags)
74     }
75
76     /**
77      * This is a saveDataDictionary service
78      *
79      * @param resourceDictionary resourceDictionary
80      * @return DataDictionary
81      */
82     @Throws(BluePrintException::class)
83     fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
84         var resourceDictionary = resourceDictionary
85
86         val resourceDefinition = resourceDictionary.definition
87         Preconditions.checkNotNull(resourceDefinition, "failed to get resource definition from content")
88         // Validate the Resource Definitions
89         resourceDictionaryValidationService.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         ResourceDictionaryValidator.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 }