2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.apps.controllerblueprints.core.service
19 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
20 import org.onap.ccsdk.apps.controllerblueprints.core.data.ImportDefinition
21 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate
22 import org.onap.ccsdk.apps.controllerblueprints.core.utils.ServiceTemplateUtils
23 import org.slf4j.Logger
24 import org.slf4j.LoggerFactory
27 import java.net.URLDecoder
28 import java.nio.charset.Charset
30 class BluePrintImportService(private val parentServiceTemplate: ServiceTemplate, private val blueprintBasePath: String) {
32 private val log: Logger = LoggerFactory.getLogger(this::class.toString())
33 val PARENT_SERVICE_TEMPLATE: String = "parent"
35 var importServiceTemplateMap: MutableMap<String, ServiceTemplate> = hashMapOf()
38 fun getImportResolvedServiceTemplate(): ServiceTemplate {
39 // Populate Imported Service Templates
40 traverseSchema(PARENT_SERVICE_TEMPLATE, parentServiceTemplate)
42 importServiceTemplateMap.forEach { key, serviceTemplate ->
43 ServiceTemplateUtils.merge(parentServiceTemplate, serviceTemplate)
44 log.debug("merged service template $key")
46 return parentServiceTemplate
49 private fun traverseSchema(key: String, serviceTemplate: ServiceTemplate) {
50 if (key != PARENT_SERVICE_TEMPLATE) {
51 importServiceTemplateMap[key] = serviceTemplate
53 val imports: List<ImportDefinition>? = serviceTemplate.imports
56 serviceTemplate.imports?.forEach { importDefinition ->
57 val childServiceTemplate = resolveImportDefinition(importDefinition)
58 val keyName: String = importDefinition.file
59 traverseSchema(keyName, childServiceTemplate)
64 private fun resolveImportDefinition(importDefinition: ImportDefinition): ServiceTemplate {
65 var serviceTemplate: ServiceTemplate? = null
66 val file: String = importDefinition.file
67 val decodedSystemId: String = URLDecoder.decode(file, Charset.defaultCharset().toString())
68 log.trace("file ({}), decodedSystemId ({}) ", file, decodedSystemId)
70 if (decodedSystemId.startsWith("http", true)
71 || decodedSystemId.startsWith("https", true)) {
72 val givenUrl: String = URL(decodedSystemId).toString()
73 val systemUrl: String = File(".").toURI().toURL().toString()
74 log.trace("givenUrl ({}), systemUrl ({}) ", givenUrl, systemUrl)
75 if (givenUrl.startsWith(systemUrl)) {
79 if (!decodedSystemId.startsWith("/")) {
80 importDefinition.file = StringBuilder().append(blueprintBasePath).append(File.separator).append(file).toString()
82 serviceTemplate = ServiceTemplateUtils.getServiceTemplate(importDefinition.file)
84 } catch (e: Exception) {
85 throw BluePrintException("failed to populate service template for ${importDefinition.file}", e)
87 if (serviceTemplate == null) {
88 throw BluePrintException("failed to populate service template for : ${importDefinition.file}")
90 return serviceTemplate