Removed redundant timeout handling for executeCommand
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / 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.blueprintsprocessor.designer.api.enhancer
19
20 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.DesignerApiDomains
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.cds.controllerblueprints.core.httpProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintEnhancerService
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
26 import org.onap.ccsdk.cds.controllerblueprints.core.logger
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
28 import org.onap.ccsdk.cds.controllerblueprints.core.updateErrorMessage
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
31 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils
32 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
33 import org.onap.ccsdk.cds.error.catalog.core.utils.errorCauseOrDefault
34 import org.springframework.stereotype.Service
35 import java.io.IOException
36 import java.util.UUID
37
38 @Service
39 open class BluePrintEnhancerServiceImpl(
40     private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
41     private val resourceDefinitionEnhancerService: ResourceDefinitionEnhancerService
42 ) : BluePrintEnhancerService {
43
44     private val log = logger(BluePrintEnhancerServiceImpl::class)
45
46     override suspend fun enhance(basePath: String, enrichedBasePath: String): BluePrintContext {
47
48         // Copy the Blueprint Content to Target Location
49         BluePrintFileUtils.copyBluePrint(basePath, enrichedBasePath)
50
51         // Enhance the Blueprint
52         return enhance(enrichedBasePath)
53     }
54
55     @Throws(BluePrintException::class)
56     override suspend fun enhance(basePath: String): BluePrintContext {
57
58         log.info("Enhancing blueprint($basePath)")
59         val blueprintRuntimeService = BluePrintMetadataUtils
60             .getBaseEnhancementBluePrintRuntime(UUID.randomUUID().toString(), basePath)
61
62         try {
63
64             bluePrintTypeEnhancerService.enhanceServiceTemplate(
65                 blueprintRuntimeService, "service_template",
66                 blueprintRuntimeService.bluePrintContext().serviceTemplate
67             )
68
69             log.info("##### Enhancing blueprint Resource Definitions")
70             val resourceDefinitions = resourceDefinitionEnhancerService.enhance(
71                 bluePrintTypeEnhancerService,
72                 blueprintRuntimeService
73             )
74
75             // Write the Enhanced Blueprint Definitions
76             BluePrintFileUtils.writeEnhancedBluePrint(blueprintRuntimeService.bluePrintContext())
77
78             // Write the Enhanced Blueprint Resource Definitions
79             ResourceDictionaryUtils.writeResourceDefinitionTypes(basePath, resourceDefinitions)
80
81             if (blueprintRuntimeService.getBluePrintError().allErrors().isNotEmpty()) {
82                 throw BluePrintException(blueprintRuntimeService.getBluePrintError().allErrors().toString())
83             }
84         } catch (e: BluePrintProcessorException) {
85             val errorMsg = "Error while enriching the CBA package."
86             throw e.updateErrorMessage(
87                 DesignerApiDomains.DESIGNER_API, errorMsg,
88                 "Wrong blueprint definitions or resource definitions."
89             )
90         } catch (e: IOException) {
91             throw httpProcessorException(
92                 ErrorCatalogCodes.IO_FILE_INTERRUPT, DesignerApiDomains.DESIGNER_API,
93                 "IO Error: CBA file failed enrichment - ${e.message}", e.errorCauseOrDefault()
94             )
95         } catch (e: Exception) {
96             throw httpProcessorException(
97                 ErrorCatalogCodes.IO_FILE_INTERRUPT, DesignerApiDomains.DESIGNER_API,
98                 "Error in Enriching CBA: ${e.message}", e.errorCauseOrDefault()
99             )
100         }
101         return blueprintRuntimeService.bluePrintContext()
102     }
103 }