2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.apps.controllerblueprints.service.handler
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
32 class ResourceDictionaryHandler(private val resourceDictionaryRepository: ResourceDictionaryRepository,
33 private val resourceDictionaryValidationService: ResourceDefinitionValidationService) {
37 * This is a getDataDictionaryByName service
40 * @return DataDictionary
41 * @throws BluePrintException BluePrintException
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()
50 throw BluePrintException(String.format("couldn't get resource dictionary for name (%s)", name))
55 * This is a searchResourceDictionaryByNames service
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)
66 * This is a searchResourceDictionaryByTags service
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)
77 * This is a saveDataDictionary service
79 * @param resourceDictionary resourceDictionary
80 * @return DataDictionary
82 @Throws(BluePrintException::class)
83 fun saveResourceDictionary(resourceDictionary: ResourceDictionary): ResourceDictionary {
84 var resourceDictionary = resourceDictionary
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)
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 ResourceDictionaryValidator.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()