24b7e261c723b0392a08c5bff4747b2c7bb5c9b4
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BluePrintImportService.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core.service
19
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
21 import org.onap.ccsdk.cds.controllerblueprints.core.data.ImportDefinition
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
23 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ServiceTemplateUtils
24 import org.slf4j.Logger
25 import org.slf4j.LoggerFactory
26 import java.io.File
27 import java.net.URL
28 import java.net.URLDecoder
29 import java.nio.charset.Charset
30
31 class BluePrintImportService(private val parentServiceTemplate: ServiceTemplate, private val blueprintBasePath: String) {
32     companion object {
33         private const val PARENT_SERVICE_TEMPLATE: String = "parent"
34     }
35
36     private val log: Logger = LoggerFactory.getLogger(this::class.toString())
37
38     private var importServiceTemplateMap: MutableMap<String, ServiceTemplate> = hashMapOf()
39
40     suspend fun getImportResolvedServiceTemplate(): ServiceTemplate {
41         // Populate Imported Service Templates
42         traverseSchema(PARENT_SERVICE_TEMPLATE, parentServiceTemplate)
43
44         importServiceTemplateMap.forEach { key, serviceTemplate ->
45             ServiceTemplateUtils.merge(parentServiceTemplate, serviceTemplate)
46             log.debug("merged service template $key")
47         }
48         return parentServiceTemplate
49     }
50
51     private suspend fun traverseSchema(key: String, serviceTemplate: ServiceTemplate) {
52         if (key != PARENT_SERVICE_TEMPLATE) {
53             importServiceTemplateMap[key] = serviceTemplate
54         }
55         val imports: List<ImportDefinition>? = serviceTemplate.imports
56
57         imports?.let {
58             serviceTemplate.imports?.forEach { importDefinition ->
59                 val childServiceTemplate = resolveImportDefinition(importDefinition)
60                 val keyName: String = importDefinition.file
61                 traverseSchema(keyName, childServiceTemplate)
62             }
63         }
64     }
65
66     private suspend fun resolveImportDefinition(importDefinition: ImportDefinition): ServiceTemplate {
67         var serviceTemplate: ServiceTemplate? = null
68         val file: String = importDefinition.file
69         val decodedSystemId: String = URLDecoder.decode(file, Charset.defaultCharset().toString())
70         log.trace("file ({}), decodedSystemId ({}) ", file, decodedSystemId)
71         try {
72             if (decodedSystemId.startsWith("http", true) ||
73                 decodedSystemId.startsWith("https", true)
74             ) {
75                 val givenUrl: String = URL(decodedSystemId).toString()
76                 val systemUrl: String = File(".").toURI().toURL().toString()
77                 log.trace("givenUrl ({}), systemUrl ({}) ", givenUrl, systemUrl)
78                 if (givenUrl.startsWith(systemUrl)) {
79                 }
80             } else {
81                 if (!decodedSystemId.startsWith("/")) {
82                     importDefinition.file = StringBuilder().append(blueprintBasePath).append(File.separator).append(file).toString()
83                 }
84                 serviceTemplate = ServiceTemplateUtils.getServiceTemplate(importDefinition.file)
85             }
86         } catch (e: Exception) {
87             throw BluePrintException("failed to populate service template for ${importDefinition.file} original error: ${e.message}", e)
88         }
89         if (serviceTemplate == null) {
90             throw BluePrintException("failed to populate service template for :  ${importDefinition.file}")
91         }
92         return serviceTemplate
93     }
94 }