c2d6f6aa58afa5592dea050ae65706bae0b308db
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
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.apps.controllerblueprints.service.utils
19
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
21 import org.onap.ccsdk.apps.controllerblueprints.core.data.ArtifactType
22 import org.onap.ccsdk.apps.controllerblueprints.core.data.DataType
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.RelationshipType
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService
26 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
27 import org.springframework.http.codec.multipart.FilePart
28 import org.springframework.util.StringUtils
29 import reactor.core.publisher.Mono
30 import java.io.File
31 import java.io.IOException
32 import java.nio.file.Path
33 import java.util.*
34
35
36 class BluePrintEnhancerUtils {
37     companion object {
38
39         fun populateDataTypes(bluePrintContext: BluePrintContext, bluePrintRepoService: BluePrintRepoService,
40                               dataTypeName: String): DataType {
41             val dataType = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
42                     ?: bluePrintRepoService.getDataType(dataTypeName)
43                     ?: throw BluePrintException("couldn't get DataType($dataTypeName) from repo.")
44             bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dataType)
45             return dataType
46         }
47
48         fun populateRelationshipType(bluePrintContext: BluePrintContext, bluePrintRepoService: BluePrintRepoService,
49                              relationshipName: String): RelationshipType {
50
51             val relationshipType = bluePrintContext.serviceTemplate.relationshipTypes?.get(relationshipName)
52                     ?: bluePrintRepoService.getRelationshipType(relationshipName)
53                     ?: throw BluePrintException("couldn't get RelationshipType($relationshipName) from repo.")
54             bluePrintContext.serviceTemplate.relationshipTypes?.put(relationshipName, relationshipType)
55             return relationshipType
56         }
57
58
59         fun populateNodeType(bluePrintContext: BluePrintContext, bluePrintRepoService: BluePrintRepoService,
60                              nodeTypeName: String): NodeType {
61
62             val nodeType = bluePrintContext.serviceTemplate.nodeTypes?.get(nodeTypeName)
63                     ?: bluePrintRepoService.getNodeType(nodeTypeName)
64                     ?: throw BluePrintException("couldn't get NodeType($nodeTypeName) from repo.")
65             bluePrintContext.serviceTemplate.nodeTypes?.put(nodeTypeName, nodeType)
66             return nodeType
67         }
68
69         fun populateArtifactType(bluePrintContext: BluePrintContext, bluePrintRepoService: BluePrintRepoService,
70                                  artifactTypeName: String): ArtifactType {
71
72             val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
73                     ?: bluePrintRepoService.getArtifactType(artifactTypeName)
74                     ?: throw BluePrintException("couldn't get ArtifactType($artifactTypeName) from repo.")
75             bluePrintContext.serviceTemplate.artifactTypes?.put(artifactTypeName, artifactType)
76             return artifactType
77         }
78
79         /**
80          * This is a saveCBAFile method
81          * take a [FilePart], transfer it to disk using a Flux of FilePart and return a [Mono] representing the CBA file name
82          *
83          * @param (filePart, targetDirectory) - the request part containing the file to be saved and the default directory where to save
84          * @return a [Mono] String representing the result of the operation
85          * @throws (BluePrintException, IOException) BluePrintException, IOException
86          */
87         @Throws(BluePrintException::class, IOException::class)
88         fun saveCBAFile(filePart: FilePart, targetDirectory: Path): Mono<String> {
89
90             // Normalize file name
91             val fileName = StringUtils.cleanPath(filePart.filename())
92
93             // Check if the file's extension is "CBA"
94             if (StringUtils.getFilenameExtension(fileName) != "zip") {
95                 throw BluePrintException("Invalid file extension required ZIP")
96             }
97
98             // Change file name to match a pattern
99             val changedFileName = UUID.randomUUID().toString() + ".zip"
100             //String changedFileName = BluePrintFileUtils.Companion.getCBAGeneratedFileName(fileName, this.CBA_FILE_NAME_PATTERN);
101
102             // Copy file to the target location (Replacing existing file with the same name)
103             val targetLocation = targetDirectory.resolve(changedFileName)
104
105             // if a file with the same name already exists in a repository, delete and recreate it
106             val file = File(targetLocation.toString())
107             if (file.exists())
108                 file.delete()
109             file.createNewFile()
110
111             return filePart.transferTo(file).thenReturn(changedFileName)
112         }
113     }
114 }