eddaa1cc27e9fd6907562be332db3d04857c8f35
[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.load
18
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
28 import java.io.File
29 import java.nio.charset.Charset
30
31 @Service
32 open class ResourceDictionaryLoadService(private val resourceDictionaryService: ResourceDictionaryService) {
33
34     private val log = EELFManager.getInstance().getLogger(ResourceDictionaryLoadService::class.java)
35
36     open fun loadResourceDictionary(resourceDictionaryPaths: List<String>) {
37         resourceDictionaryPaths.forEach { loadResourceDictionary(it) }
38     }
39
40     open fun loadResourceDictionary(resourceDictionaryPath: String) {
41         log.info(" *************************** loadResourceDictionary **********************")
42         val resourceDictionaries = File(resourceDictionaryPath).listFiles()
43         val errorBuilder = StrBuilder()
44
45         resourceDictionaries.forEach { file ->
46             try {
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) {
51
52                     checkNotNull(resourceDefinition.property) { "Failed to get Property Definition" }
53                     val resourceDictionary = ResourceDictionary()
54                     resourceDictionary.name = resourceDefinition.name
55                     resourceDictionary.definition = resourceDefinition
56
57                     checkNotNull(resourceDefinition.property) { "Property field is missing" }
58                     resourceDictionary.description = resourceDefinition.property.description
59                     resourceDictionary.dataType = resourceDefinition.property.type
60
61                     if (resourceDefinition.property.entrySchema != null) {
62                         resourceDictionary.entrySchema = resourceDefinition.property.entrySchema!!.type
63                     }
64                     resourceDictionary.updatedBy = resourceDefinition.updatedBy
65
66                     if (StringUtils.isBlank(resourceDefinition.tags)) {
67                         resourceDictionary.tags = (resourceDefinition.name + ", " + resourceDefinition.updatedBy
68                                 + ", " + resourceDefinition.updatedBy)
69
70                     } else {
71                         resourceDictionary.tags = resourceDefinition.tags
72                     }
73                     resourceDictionaryService.saveResourceDictionary(resourceDictionary)
74
75                     log.trace("Resource dictionary(${file.name}) loaded successfully ")
76                 } else {
77                     throw BluePrintException("couldn't get dictionary from content information")
78                 }
79             } catch (e: Exception) {
80                 errorBuilder.appendln("Couldn't load Resource dictionary (${file.name}: ${e.message}")
81             }
82         }
83         if (!errorBuilder.isEmpty) {
84             log.error(errorBuilder.toString())
85         }
86     }
87
88 }