2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.controllerblueprints.core.service
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
28 import java.net.URLDecoder
29 import java.nio.charset.Charset
31 class BluePrintImportService(private val parentServiceTemplate: ServiceTemplate, private val blueprintBasePath: String) {
33 private const val PARENT_SERVICE_TEMPLATE: String = "parent"
36 private val log: Logger = LoggerFactory.getLogger(this::class.toString())
38 private var importServiceTemplateMap: MutableMap<String, ServiceTemplate> = hashMapOf()
40 suspend fun getImportResolvedServiceTemplate(): ServiceTemplate {
41 // Populate Imported Service Templates
42 traverseSchema(PARENT_SERVICE_TEMPLATE, parentServiceTemplate)
44 importServiceTemplateMap.forEach { key, serviceTemplate ->
45 ServiceTemplateUtils.merge(parentServiceTemplate, serviceTemplate)
46 log.debug("merged service template $key")
48 return parentServiceTemplate
51 private suspend fun traverseSchema(key: String, serviceTemplate: ServiceTemplate) {
52 if (key != PARENT_SERVICE_TEMPLATE) {
53 importServiceTemplateMap[key] = serviceTemplate
55 val imports: List<ImportDefinition>? = serviceTemplate.imports
58 serviceTemplate.imports?.forEach { importDefinition ->
59 val childServiceTemplate = resolveImportDefinition(importDefinition)
60 val keyName: String = importDefinition.file
61 traverseSchema(keyName, childServiceTemplate)
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)
72 if (decodedSystemId.startsWith("http", true) ||
73 decodedSystemId.startsWith("https", true)
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)) {
81 if (!decodedSystemId.startsWith("/")) {
82 importDefinition.file = StringBuilder().append(blueprintBasePath).append(File.separator).append(file).toString()
84 serviceTemplate = ServiceTemplateUtils.getServiceTemplate(importDefinition.file)
86 } catch (e: Exception) {
87 throw BluePrintException("failed to populate service template for ${importDefinition.file} original error: ${e.message}", e)
89 if (serviceTemplate == null) {
90 throw BluePrintException("failed to populate service template for : ${importDefinition.file}")
92 return serviceTemplate