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.apps.controllerblueprints.core.service
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
21 import org.onap.ccsdk.apps.controllerblueprints.core.data.ImportDefinition
22 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate
23 import org.onap.ccsdk.apps.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()
41 suspend fun getImportResolvedServiceTemplate(): ServiceTemplate {
42 // Populate Imported Service Templates
43 traverseSchema(PARENT_SERVICE_TEMPLATE, parentServiceTemplate)
45 importServiceTemplateMap.forEach { key, serviceTemplate ->
46 ServiceTemplateUtils.merge(parentServiceTemplate, serviceTemplate)
47 log.debug("merged service template $key")
49 return parentServiceTemplate
52 private suspend fun traverseSchema(key: String, serviceTemplate: ServiceTemplate) {
53 if (key != PARENT_SERVICE_TEMPLATE) {
54 importServiceTemplateMap[key] = serviceTemplate
56 val imports: List<ImportDefinition>? = serviceTemplate.imports
59 serviceTemplate.imports?.forEach { importDefinition ->
60 val childServiceTemplate = resolveImportDefinition(importDefinition)
61 val keyName: String = importDefinition.file
62 traverseSchema(keyName, childServiceTemplate)
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)
73 if (decodedSystemId.startsWith("http", true)
74 || 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)) {
82 if (!decodedSystemId.startsWith("/")) {
83 importDefinition.file = StringBuilder().append(blueprintBasePath).append(File.separator).append(file).toString()
85 serviceTemplate = ServiceTemplateUtils.getServiceTemplate(importDefinition.file)
87 } catch (e: Exception) {
88 throw BluePrintException("failed to populate service template for ${importDefinition.file}", e)
90 if (serviceTemplate == null) {
91 throw BluePrintException("failed to populate service template for : ${importDefinition.file}")
93 return serviceTemplate