f133fd7c74e05b605889039ecab0f66d2c300e98
[ccsdk/cds.git] / ms / blueprintsprocessor / application / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / uat / 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
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(@RequestPart("cba") cbaFile: FilePart,
76                  @RequestPart("uat", required = false) uatFile: FilePart?): String = runBlocking {
77         val tempFile = createTempFile()
78         setContextColor(COLOR_SERVICES)
79         try {
80             cbaFile.transferTo(tempFile)
81             val uatSpec = when {
82                 uatFile != null -> uatFile.readText()
83                 else -> readZipEntryAsText(tempFile, UAT_SPECIFICATION_FILE)
84             }
85             val uat = UatDefinition.load(mapper, uatSpec)
86             val cbaBytes = tempFile.readBytes()
87             val updatedUat = uatExecutor.execute(uat, cbaBytes)
88             return@runBlocking updatedUat.dump(mapper, FIELDS_TO_EXCLUDE)
89         } catch (t: Throwable) {
90             throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, t.message, t)
91         } finally {
92             tempFile.delete()
93             resetContextColor()
94         }
95     }
96
97     private fun FilePart.readText(): String {
98         val tempFile = createTempFile()
99         try {
100             transferTo(tempFile).block()
101             return tempFile.readText()
102         } finally {
103             tempFile.delete()
104         }
105     }
106
107     @Suppress("SameParameterValue")
108     private fun readZipEntryAsText(file: File, entryName: String): String {
109         return ZipFile(file).use { zipFile -> zipFile.readEntryAsText(entryName) }
110     }
111
112     private fun ZipFile.readEntryAsText(entryName: String): String {
113         val zipEntry = getEntry(entryName)
114         return getInputStream(zipEntry).readBytes().toString(Charsets.UTF_8)
115     }
116
117     companion object {
118         // Fields that can be safely ignored from BPP response, and can be omitted on the UAT specification.
119         private val FIELDS_TO_EXCLUDE = listOf("timestamp")
120     }
121 }