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