Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / service / load / BluePrintCatalogLoadService.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.apps.controllerblueprints.service.load
18
19 import com.att.eelf.configuration.EELFManager
20 import kotlinx.coroutines.Deferred
21 import kotlinx.coroutines.async
22 import kotlinx.coroutines.runBlocking
23 import org.apache.commons.lang3.text.StrBuilder
24 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
25 import org.springframework.stereotype.Service
26 import java.io.File
27
28 @Service
29 open class BluePrintCatalogLoadService(private val bluePrintCatalogService: BluePrintCatalogService) {
30
31     private val log = EELFManager.getInstance().getLogger(BluePrintCatalogLoadService::class.java)
32
33     open fun loadPathsBluePrintModelCatalog(paths: List<String>) {
34         paths.forEach { loadPathBluePrintModelCatalog(it) }
35     }
36
37     open fun loadPathBluePrintModelCatalog(path: String) {
38
39         val files = File(path).listFiles()
40         runBlocking {
41             val errorBuilder = StrBuilder()
42             val deferredResults = mutableListOf<Deferred<Unit>>()
43
44             for (file in files) {
45                 deferredResults += async {
46                     loadBluePrintModelCatalog(errorBuilder, file)
47                 }
48             }
49
50             for (deferredResult in deferredResults) {
51                 deferredResult.await()
52             }
53
54             if (!errorBuilder.isEmpty) {
55                 log.error(errorBuilder.toString())
56             }
57         }
58     }
59
60     open fun loadBluePrintModelCatalog(errorBuilder: StrBuilder, file: File) {
61         try {
62             bluePrintCatalogService.saveToDatabase(file)
63         } catch (e: Exception) {
64             errorBuilder.appendln("Couldn't load BlueprintModel(${file.name}: ${e.message}")
65         }
66     }
67
68 }