Add missing k8s-rb-instance-release-name.json
[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
34         private const val PARENT_SERVICE_TEMPLATE: String = "parent"
35     }
36
37     private val log: Logger = LoggerFactory.getLogger(this::class.toString())
38
39     private var importServiceTemplateMap: MutableMap<String, ServiceTemplate> = hashMapOf()
40
41     suspend fun getImportResolvedServiceTemplate(): ServiceTemplate {
42         // Populate Imported Service Templates
43         traverseSchema(PARENT_SERVICE_TEMPLATE, parentServiceTemplate)
44
45         importServiceTemplateMap.forEach { key, serviceTemplate ->
46             ServiceTemplateUtils.merge(parentServiceTemplate, serviceTemplate)
47             log.debug("merged service template $key")
48         }
49         return parentServiceTemplate
50     }
51
52     private suspend fun traverseSchema(key: String, serviceTemplate: ServiceTemplate) {
53         if (key != PARENT_SERVICE_TEMPLATE) {
54             importServiceTemplateMap[key] = serviceTemplate
55         }
56         val imports: List<ImportDefinition>? = serviceTemplate.imports
57
58         imports?.let {
59             serviceTemplate.imports?.forEach { importDefinition ->
60                 val childServiceTemplate = resolveImportDefinition(importDefinition)
61                 val keyName: String = importDefinition.file
62                 traverseSchema(keyName, childServiceTemplate)
63             }
64         }
65     }
66
67     private suspend fun resolveImportDefinition(importDefinition: ImportDefinition): ServiceTemplate {
68         var serviceTemplate: ServiceTemplate? = null
69         val file: String = importDefinition.file
70         val decodedSystemId: String = URLDecoder.decode(file, Charset.defaultCharset().toString())
71         log.trace("file ({}), decodedSystemId ({}) ", file, decodedSystemId)
72         try {
73             if (decodedSystemId.startsWith("http", true) ||
74                 decodedSystemId.startsWith("https", true)
75             ) {
76                 val givenUrl: String = URL(decodedSystemId).toString()
77                 val systemUrl: String = File(".").toURI().toURL().toString()
78                 log.trace("givenUrl ({}), systemUrl ({}) ", givenUrl, systemUrl)
79                 if (givenUrl.startsWith(systemUrl)) {
80                 }
81             } else {
82                 if (!decodedSystemId.startsWith("/")) {
83                     importDefinition.file = StringBuilder().append(blueprintBasePath).append(File.separator).append(file).toString()
84                 }
85                 serviceTemplate = ServiceTemplateUtils.getServiceTemplate(importDefinition.file)
86             }
87         } catch (e: Exception) {
88             throw BlueprintException("failed to populate service template for ${importDefinition.file} original error: ${e.message}", e)
89         }
90         if (serviceTemplate == null) {
91             throw BlueprintException("failed to populate service template for :  ${importDefinition.file}")
92         }
93         return serviceTemplate
94     }
95 }