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.load
19 import com.att.eelf.configuration.EELFManager
20 import org.apache.commons.lang3.StringUtils
21 import org.apache.commons.lang3.text.StrBuilder
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
24 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
25 import org.onap.ccsdk.apps.controllerblueprints.service.ResourceDictionaryService
26 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ResourceDictionary
27 import org.springframework.stereotype.Service
29 import java.nio.charset.Charset
32 open class ResourceDictionaryLoadService(private val resourceDictionaryService: ResourceDictionaryService) {
34 private val log = EELFManager.getInstance().getLogger(ResourceDictionaryLoadService::class.java)
36 open fun loadResourceDictionary(resourceDictionaryPaths: List<String>) {
37 resourceDictionaryPaths.forEach { loadResourceDictionary(it) }
40 open fun loadResourceDictionary(resourceDictionaryPath: String) {
41 log.info(" *************************** loadResourceDictionary **********************")
42 val resourceDictionaries = File(resourceDictionaryPath).listFiles()
43 val errorBuilder = StrBuilder()
45 resourceDictionaries.forEach { file ->
47 log.trace("Loading NodeType(${file.name}")
48 val definitionContent = file.readText(Charset.defaultCharset())
49 val resourceDefinition = JacksonUtils.readValue(definitionContent, ResourceDefinition::class.java)
50 if (resourceDefinition != null) {
52 checkNotNull(resourceDefinition.property) { "Failed to get Property Definition" }
53 val resourceDictionary = ResourceDictionary()
54 resourceDictionary.name = resourceDefinition.name
55 resourceDictionary.definition = resourceDefinition
57 checkNotNull(resourceDefinition.property) { "Property field is missing" }
58 resourceDictionary.description = resourceDefinition.property.description
59 resourceDictionary.dataType = resourceDefinition.property.type
61 if (resourceDefinition.property.entrySchema != null) {
62 resourceDictionary.entrySchema = resourceDefinition.property.entrySchema!!.type
64 resourceDictionary.updatedBy = resourceDefinition.updatedBy
66 if (StringUtils.isBlank(resourceDefinition.tags)) {
67 resourceDictionary.tags = (resourceDefinition.name + ", " + resourceDefinition.updatedBy
68 + ", " + resourceDefinition.updatedBy)
71 resourceDictionary.tags = resourceDefinition.tags
73 resourceDictionaryService.saveResourceDictionary(resourceDictionary)
75 log.trace("Resource dictionary(${file.name}) loaded successfully ")
77 throw BluePrintException("couldn't get dictionary from content information")
79 } catch (e: Exception) {
80 errorBuilder.appendln("Couldn't load Resource dictionary (${file.name}: ${e.message}")
83 if (!errorBuilder.isEmpty) {
84 log.error(errorBuilder.toString())