0ae618f3a7fbb56234188fd60219b0f1c7dfb8b5
[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 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.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
25 import org.springframework.stereotype.Service
26 import java.io.File
27 import java.util.*
28
29 @Service
30 open class BluePrintCatalogLoadService(private val bluePrintCatalogService: BluePrintCatalogService) {
31
32     private val log = EELFManager.getInstance().getLogger(BluePrintCatalogLoadService::class.java)
33
34     open fun loadPathsBluePrintModelCatalog(paths: List<String>) {
35         paths.forEach { loadPathBluePrintModelCatalog(it) }
36     }
37
38     open fun loadPathBluePrintModelCatalog(path: String) {
39
40         val files = File(path).listFiles()
41         runBlocking {
42             val errorBuilder = StrBuilder()
43             val deferredResults = mutableListOf<Deferred<Unit>>()
44
45             for (file in files) {
46                 deferredResults += async {
47                     loadBluePrintModelCatalog(errorBuilder, file)
48                 }
49             }
50
51             for (deferredResult in deferredResults) {
52                 deferredResult.await()
53             }
54
55             if (!errorBuilder.isEmpty) {
56                 log.error(errorBuilder.toString())
57             }
58         }
59     }
60
61     open suspend fun loadBluePrintModelCatalog(errorBuilder: StrBuilder, file: File) {
62         try {
63             bluePrintCatalogService.saveToDatabase(UUID.randomUUID().toString(), file)
64         } catch (e: Exception) {
65             errorBuilder.appendln("Couldn't load BlueprintModel(${file.name}: ${e.message}")
66         }
67     }
68
69 }