Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / utils / BlueprintProcessingUtils.kt
1 /*
2  * Copyright © 2021 Bell Canada.
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 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils
17
18 import io.micrometer.core.instrument.Tag
19 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.springframework.http.HttpStatus
24 import org.springframework.http.codec.multipart.FilePart
25 import org.springframework.util.StringUtils
26 import java.io.File
27 import java.io.IOException
28 import java.nio.file.Path
29 import java.util.UUID
30
31 const val INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE = 500
32
33 @Throws(BluePrintException::class, IOException::class)
34 fun saveCBAFile(filePart: FilePart, targetDirectory: Path): Path {
35
36     val fileName = StringUtils.cleanPath(filePart.filename())
37
38     if (StringUtils.getFilenameExtension(fileName) != "zip") {
39         throw BluePrintException("Invalid file extension required ZIP")
40     }
41
42     val changedFileName = UUID.randomUUID().toString() + ".zip"
43
44     val targetLocation = targetDirectory.resolve(changedFileName)
45
46     val file = File(targetLocation.toString())
47     if (file.exists()) {
48         file.delete()
49     }
50     file.createNewFile()
51
52     filePart.transferTo(file)
53
54     return targetLocation
55 }
56
57 fun determineHttpStatusCode(statusCode: Int): HttpStatus {
58
59     try {
60         return HttpStatus.valueOf(statusCode)
61     } catch (exception: Exception) {
62         // if statusCode cannot be converted to a proper HttpStatus, the resource still needs to assign a HTTP status
63         // code to the response. In this case, a 500 Internal Server Error will be returned as default.
64         return HttpStatus.valueOf(INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE)
65     }
66 }
67
68 fun cbaMetricTags(executionServiceInput: ExecutionServiceInput): MutableList<Tag> =
69     executionServiceInput.actionIdentifiers.let {
70         mutableListOf(
71             Tag.of(BluePrintConstants.METRIC_TAG_BP_NAME, it.blueprintName),
72             Tag.of(BluePrintConstants.METRIC_TAG_BP_VERSION, it.blueprintVersion),
73             Tag.of(BluePrintConstants.METRIC_TAG_BP_ACTION, it.actionName)
74         )
75     }
76
77 fun cbaMetricTags(executionServiceOutput: ExecutionServiceOutput): MutableList<Tag> =
78     executionServiceOutput.let {
79         mutableListOf(
80             Tag.of(BluePrintConstants.METRIC_TAG_BP_NAME, it.actionIdentifiers.blueprintName),
81             Tag.of(BluePrintConstants.METRIC_TAG_BP_VERSION, it.actionIdentifiers.blueprintVersion),
82             Tag.of(BluePrintConstants.METRIC_TAG_BP_ACTION, it.actionIdentifiers.actionName),
83             Tag.of(BluePrintConstants.METRIC_TAG_BP_STATUS, it.status.code.toString()),
84             Tag.of(BluePrintConstants.METRIC_TAG_BP_OUTCOME, it.status.message)
85         )
86     }