Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / service / enhancer / BluePrintEnhancerServiceImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.service.enhancer
19
20 import com.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintEnhancerService
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
25 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils
29 import org.springframework.stereotype.Service
30 import java.util.*
31
32 @Service
33 open class BluePrintEnhancerServiceImpl(private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
34                                         private val resourceDefinitionEnhancerService: ResourceDefinitionEnhancerService) : BluePrintEnhancerService {
35
36     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintEnhancerServiceImpl::class.toString())
37
38     override suspend fun enhance(basePath: String, enrichedBasePath: String): BluePrintContext {
39
40         // Copy the Blueprint Content to Target Location
41         BluePrintFileUtils.copyBluePrint(basePath, enrichedBasePath)
42
43         // Enhance the Blueprint
44         return enhance(enrichedBasePath)
45     }
46
47     @Throws(BluePrintException::class)
48     override suspend fun enhance(basePath: String): BluePrintContext {
49
50         log.info("Enhancing blueprint($basePath)")
51         val blueprintRuntimeService = BluePrintMetadataUtils
52                 .getBaseEnhancementBluePrintRuntime(UUID.randomUUID().toString(), basePath)
53
54         try {
55
56             bluePrintTypeEnhancerService.enhanceServiceTemplate(blueprintRuntimeService, "service_template",
57                     blueprintRuntimeService.bluePrintContext().serviceTemplate)
58
59             log.info("##### Enhancing blueprint Resource Definitions")
60             val resourceDefinitions = resourceDefinitionEnhancerService.enhance(bluePrintTypeEnhancerService,
61                     blueprintRuntimeService)
62
63             // Write the Enhanced Blueprint Definitions
64             BluePrintFileUtils.writeEnhancedBluePrint(blueprintRuntimeService.bluePrintContext())
65
66             // Write the Enhanced Blueprint Resource Definitions
67             ResourceDictionaryUtils.writeResourceDefinitionTypes(basePath, resourceDefinitions)
68
69             if (blueprintRuntimeService.getBluePrintError().errors.isNotEmpty()) {
70                 throw BluePrintException(blueprintRuntimeService.getBluePrintError().errors.toString())
71             }
72
73         } catch (e: Exception) {
74             throw e
75         }
76         return blueprintRuntimeService.bluePrintContext()
77     }
78
79 }
80