2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.apps.controllerblueprints.service.handler
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
32 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository) {
36 * This is a getDataDictionaryByName service
39 * @return DataDictionary
40 * @throws BluePrintException BluePrintException
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()
49 throw BluePrintException(String.format("couldn't get resource dictionary for name (%s)", name))
54 * This is a searchResourceDictionaryByNames service
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)
65 * This is a searchResourceDictionaryByTags service
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)
76 * This is a saveDataDictionary service
78 * @param resourceDictionary resourceDictionary
79 * @return DataDictionary
81 @Throws(BluePrintException::class)
82 fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
83 var resourceDictionary = resourceDictionary
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)
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
101 validateResourceDictionary(resourceDictionary)
103 val dbResourceDictionaryData = resourceDictionaryRepository.findByName(resourceDictionary.name)
104 if (dbResourceDictionaryData.isPresent) {
105 val dbResourceDictionary = dbResourceDictionaryData.get()
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)
116 resourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
119 return resourceDictionary
123 * This is a deleteResourceDictionary service
127 fun deleteResourceDictionary(name: String) {
128 check(name.isNotBlank()) { "Resource dictionary name is missing." }
129 resourceDictionaryRepository.deleteByName(name)
133 * This is a getResourceSourceMapping service
135 fun getResourceSourceMapping(): ResourceSourceMapping {
136 return ResourceSourceMappingFactory.getRegisterSourceMapping()
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.")