26eb19de17ce63dc508662207dfef1b615b6be7c
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.core.service
18
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
25 import java.io.File
26 import java.net.URL
27 import java.net.URLDecoder
28 import java.nio.charset.Charset
29
30 class BluePrintImportService(private val parentServiceTemplate: ServiceTemplate, private val blueprintBasePath: String) {
31     companion object {
32         private const val PARENT_SERVICE_TEMPLATE: String = "parent"
33     }
34
35     private val log: Logger = LoggerFactory.getLogger(this::class.toString())
36
37     private var importServiceTemplateMap: MutableMap<String, ServiceTemplate> = hashMapOf()
38
39
40     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 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 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                 val givenUrl: String = URL(decodedSystemId).toString()
75                 val systemUrl: String = File(".").toURI().toURL().toString()
76                 log.trace("givenUrl ({}), systemUrl ({}) ", givenUrl, systemUrl)
77                 if (givenUrl.startsWith(systemUrl)) {
78
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}", e)
88         }
89         if (serviceTemplate == null) {
90             throw BluePrintException("failed to populate service template for :  ${importDefinition.file}")
91         }
92         return serviceTemplate
93     }
94
95
96 }