Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / application / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / uat / utils / UatServices.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.ccsdk.cds.blueprintsprocessor.uat.utils
21
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import kotlinx.coroutines.runBlocking
24 import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.COLOR_SERVICES
25 import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.resetContextColor
26 import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.setContextColor
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants.UAT_SPECIFICATION_FILE
28 import org.springframework.context.annotation.Profile
29 import org.springframework.http.HttpStatus
30 import org.springframework.http.MediaType
31 import org.springframework.http.codec.multipart.FilePart
32 import org.springframework.security.access.prepost.PreAuthorize
33 import org.springframework.web.bind.annotation.PostMapping
34 import org.springframework.web.bind.annotation.RequestMapping
35 import org.springframework.web.bind.annotation.RequestPart
36 import org.springframework.web.bind.annotation.RestController
37 import org.springframework.web.server.ResponseStatusException
38 import java.io.File
39 import java.util.zip.ZipFile
40
41 /**
42  * Supporting services to help creating UAT specifications.
43  *
44  * @author Eliezio Oliveira
45  */
46 @RestController
47 @RequestMapping("/api/v1/uat")
48 @Profile("uat")
49 open class UatServices(private val uatExecutor: UatExecutor, private val mapper: ObjectMapper) {
50
51     @PostMapping("/verify", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
52     @PreAuthorize("hasRole('USER')")
53     @Suppress("BlockingMethodInNonBlockingContext")
54     open fun verify(@RequestPart("cba") cbaFile: FilePart) = runBlocking {
55         setContextColor(COLOR_SERVICES)
56         val tempFile = createTempFile()
57         try {
58             cbaFile.transferTo(tempFile)
59             val uatSpec = readZipEntryAsText(tempFile, UAT_SPECIFICATION_FILE)
60             val cbaBytes = tempFile.readBytes()
61             uatExecutor.execute(uatSpec, cbaBytes)
62         } catch (e: AssertionError) {
63             throw ResponseStatusException(HttpStatus.BAD_REQUEST, e.message)
64         } catch (t: Throwable) {
65             throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, t.message, t)
66         } finally {
67             tempFile.delete()
68             resetContextColor()
69         }
70     }
71
72     @PostMapping("/spy", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE], produces = ["text/vnd.yaml"])
73     @PreAuthorize("hasRole('USER')")
74     @Suppress("BlockingMethodInNonBlockingContext")
75     open fun spy(
76         @RequestPart("cba") cbaFile: FilePart,
77         @RequestPart("uat", required = false) uatFile: FilePart?
78     ): String = runBlocking {
79         val tempFile = createTempFile()
80         setContextColor(COLOR_SERVICES)
81         try {
82             cbaFile.transferTo(tempFile)
83             val uatSpec = when {
84                 uatFile != null -> uatFile.readText()
85                 else -> readZipEntryAsText(tempFile, UAT_SPECIFICATION_FILE)
86             }
87             val uat = UatDefinition.load(mapper, uatSpec)
88             val cbaBytes = tempFile.readBytes()
89             val updatedUat = uatExecutor.execute(uat, cbaBytes)
90             return@runBlocking updatedUat.dump(
91                 mapper,
92                 FIELDS_TO_EXCLUDE
93             )
94         } catch (t: Throwable) {
95             throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, t.message, t)
96         } finally {
97             tempFile.delete()
98             resetContextColor()
99         }
100     }
101
102     private fun FilePart.readText(): String {
103         val tempFile = createTempFile()
104         try {
105             transferTo(tempFile).block()
106             return tempFile.readText()
107         } finally {
108             tempFile.delete()
109         }
110     }
111
112     @Suppress("SameParameterValue")
113     private fun readZipEntryAsText(file: File, entryName: String): String {
114         return ZipFile(file).use { zipFile -> zipFile.readEntryAsText(entryName) }
115     }
116
117     private fun ZipFile.readEntryAsText(entryName: String): String {
118         val zipEntry = getEntry(entryName)
119         return getInputStream(zipEntry).readBytes().toString(Charsets.UTF_8)
120     }
121
122     companion object {
123
124         // Fields that can be safely ignored from BPP response, and can be omitted on the UAT specification.
125         private val FIELDS_TO_EXCLUDE = listOf("timestamp")
126     }
127 }