fce06f3f59a29d18f1d45ec41b8f3a990574114d
[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
32     private val log: Logger = LoggerFactory.getLogger(this::class.toString())
33     val PARENT_SERVICE_TEMPLATE: String = "parent"
34
35     var importServiceTemplateMap: MutableMap<String, ServiceTemplate> = hashMapOf()
36
37
38     fun getImportResolvedServiceTemplate(): ServiceTemplate {
39         // Populate Imported Service Templates
40         traverseSchema(PARENT_SERVICE_TEMPLATE, parentServiceTemplate)
41
42         importServiceTemplateMap.forEach { key, serviceTemplate ->
43             ServiceTemplateUtils.merge(parentServiceTemplate, serviceTemplate)
44             log.debug("merged service template $key")
45         }
46         return parentServiceTemplate
47     }
48
49     private fun traverseSchema(key: String, serviceTemplate: ServiceTemplate) {
50         if (key != PARENT_SERVICE_TEMPLATE) {
51             importServiceTemplateMap[key] = serviceTemplate
52         }
53         val imports: List<ImportDefinition>? = serviceTemplate.imports
54
55         imports?.let {
56             serviceTemplate.imports?.forEach { importDefinition ->
57                 val childServiceTemplate = resolveImportDefinition(importDefinition)
58                 val keyName: String = importDefinition.file
59                 traverseSchema(keyName, childServiceTemplate)
60             }
61         }
62     }
63
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)
69         try {
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)) {
76
77                 }
78             } else {
79                 if (!decodedSystemId.startsWith("/")) {
80                     importDefinition.file = StringBuilder().append(blueprintBasePath).append(File.separator).append(file).toString()
81                 }
82                 serviceTemplate = ServiceTemplateUtils.getServiceTemplate(importDefinition.file)
83             }
84         } catch (e: Exception) {
85             throw BluePrintException("failed to populate service template for ${importDefinition.file}", e)
86         }
87         if (serviceTemplate == null) {
88             throw BluePrintException("failed to populate service template for :  ${importDefinition.file}")
89         }
90         return serviceTemplate
91     }
92
93
94 }