Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / service / load / BluePrintDatabaseLoadService.kt
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.cds.controllerblueprints.service.load
18
19 import com.att.eelf.configuration.EELFManager
20 import kotlinx.coroutines.runBlocking
21 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
22 import org.springframework.boot.context.event.ApplicationReadyEvent
23 import org.springframework.context.event.EventListener
24 import org.springframework.stereotype.Service
25
26 @Service
27 open class BluePrintDatabaseLoadService(private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
28                                         private val modelTypeLoadService: ModelTypeLoadService,
29                                         private val resourceDictionaryLoadService: ResourceDictionaryLoadService,
30                                         private val bluePrintCatalogLoadService: BluePrintCatalogLoadService) {
31
32     private val log = EELFManager.getInstance().getLogger(BluePrintDatabaseLoadService::class.java)
33
34
35     @EventListener(ApplicationReadyEvent::class)
36     open fun init() {
37         if (bluePrintLoadConfiguration.loadInitialData) {
38             initModelTypes()
39             initResourceDictionary()
40             initBluePrintCatalog()
41         } else {
42             log.info("Initial data load is disabled")
43         }
44     }
45
46     open fun initModelTypes() {
47         log.info("model types load configuration(${bluePrintLoadConfiguration.loadModelType}) " +
48                 "under paths(${bluePrintLoadConfiguration.loadModeTypePaths})")
49
50         if (bluePrintLoadConfiguration.loadModelType) {
51             val paths = bluePrintLoadConfiguration.loadModeTypePaths?.split(",")
52             paths?.let {
53                 runBlocking {
54                     modelTypeLoadService.loadPathsModelType(paths)
55                 }
56             }
57         }
58     }
59
60     open fun initResourceDictionary() {
61         log.info("resource dictionary load configuration(${bluePrintLoadConfiguration.loadResourceDictionary}) " +
62                 "under paths(${bluePrintLoadConfiguration.loadResourceDictionaryPaths})")
63
64         if (bluePrintLoadConfiguration.loadResourceDictionary) {
65             val paths = bluePrintLoadConfiguration.loadResourceDictionaryPaths?.split(",")
66             paths?.let {
67                 resourceDictionaryLoadService.loadPathsResourceDictionary(paths)
68             }
69         }
70     }
71
72     open fun initBluePrintCatalog() {
73         log.info("blueprint load configuration(${bluePrintLoadConfiguration.loadBluePrint}) " +
74                 "under paths(${bluePrintLoadConfiguration.loadBluePrintPaths})")
75
76         if (bluePrintLoadConfiguration.loadBluePrint) {
77             val paths = bluePrintLoadConfiguration.loadBluePrintPaths?.split(",")
78             paths?.let {
79                 bluePrintCatalogLoadService.loadPathsBluePrintModelCatalog(paths)
80             }
81         }
82     }
83 }