1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.sdc.be.components.impl;
import fj.data.Either;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
import org.openecomp.sdc.be.tosca.CsarUtils;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.common.util.GeneralUtility;
import org.openecomp.sdc.exception.ResponseFormat;
public class CsarValidationUtils {
private static final Logger log = Logger.getLogger(CsarValidationUtils.class.getName());
private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
private static final String CSAR_VERSION = "CSAR-Version";
private static final String CREATED_BY = "Created-By";
private static final String NEW_LINE_DELM = "\n";
private static final String TOSCA_METADATA = "TOSCA-Metadata";
private static final String TOSCA_FILE = "TOSCA.meta";
private static final String DEL_PATTERN = "([/\\\\]+)";
public static final String TOSCA_METADATA_PATH_PATTERN = TOSCA_METADATA +
// Artifact Group (i.e Deployment/Informational)
DEL_PATTERN + TOSCA_FILE;
public static final String TOSCA_META_ENTRY_DEFINITIONS = "Entry-Definitions";
private static final String[] TOSCA_METADATA_FIELDS = {TOSCA_META_FILE_VERSION, CSAR_VERSION, CREATED_BY, TOSCA_META_ENTRY_DEFINITIONS};
private static final String ARTIFACTS_METADATA_FILE = "HEAT.meta";
private static final String TOSCA_CSAR_EXTENSION = ".csar";
private static final String TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID = "TOSCA-Metadata/TOSCA.meta file is not in expected key-value form in csar, csar ID '%s'";
private static final String TOSCA_METADATA_TOSCA_META_FILE_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_WITH_ID = "TOSCA-Metadata/TOSCA.meta file not in expected key-value form in CSAR with id ";
private static final String CSAR_INTERNALS_ARE_INVALID = "CSAR internals are invalid";
private static final String ENTRY_DEFINITIONS_ENTRY_NOT_FOUND_IN_TOSCA_METADATA_TOSCA_META_FILE_CSAR_ID = "Entry-Definitions entry not found in TOSCA-Metadata/TOSCA.meta file, csar ID {}";
private static final String FILE_NOT_FOUND_IN_CSAR_WITH_ID = " file not found in CSAR with id ";
private static final String CSAR_STRUCTURE_IS_INVALID = "CSAR structure is invalid";
private static final String ENTRY_DEFINITIONS = "Entry-Definitions ";
private static final Pattern floatPattern = Pattern.compile("^\\d+\\.\\d+$");
/**
* Utility classes, which are collections of static members, are not meant to be instantiated
*/
private CsarValidationUtils() {
}
/**
* Validates Csar
*
* @param csar
* @param csarUUID
* @param componentsUtils
* @return
*/
public static Either<Boolean, ResponseFormat> validateCsar(Map<String, byte[]> csar, String csarUUID, ComponentsUtils componentsUtils) {
Either<Boolean, ResponseFormat> validateStatus = validateIsTOSCAMetadataExist(csar, csarUUID, componentsUtils);
if (validateStatus.isRight()) {
return validateStatus;
}
removeNonUniqueArtifactsFromCsar(csar);
log.trace("TOSCA-Metadata/TOSCA.meta file found, CSAR id {}", csarUUID);
validateStatus = validateTOSCAMetadataFile(csar, csarUUID, componentsUtils);
if (validateStatus.isRight()) {
return validateStatus;
}
return Either.left(true);
}
private static void removeNonUniqueArtifactsFromCsar(Map<String, byte[]> csar) {
List<String> nonUniqueArtifactsToRemove = new ArrayList<>();
String[] paths = csar.keySet().toArray(new String[csar.keySet().size()]);
int numberOfArtifacts = paths.length;
for (int i = 0; i < numberOfArtifacts; ++i) {
collectNonUniqueArtifact(paths, i, numberOfArtifacts, nonUniqueArtifactsToRemove);
}
nonUniqueArtifactsToRemove.stream().forEach(csar::remove);
}
private static void collectNonUniqueArtifact(String[] paths, int currInd, int numberOfArtifacts, List<String> nonUniqueArtifactsToRemove) {
String[] parsedPath = paths[currInd].split("/");
String[] otherParsedPath;
int artifactNameInd = parsedPath.length - 1;
for (int j = currInd + 1; j < numberOfArtifacts; ++j) {
otherParsedPath = paths[j].split("/");
if (parsedPath.length == otherParsedPath.length && parsedPath.length > 3 && isEqualArtifactNames(parsedPath, otherParsedPath)) {
log.error(
"Can't upload two artifact with the same name {}. The artifact with path {} will be handled, and the artifact with path {} will be ignored. ",
parsedPath[artifactNameInd], paths[currInd], paths[j]);
nonUniqueArtifactsToRemove.add(paths[j]);
}
}
}
private static boolean isEqualArtifactNames(String[] parsedPath, String[] otherParsedPath) {
boolean isEqualArtifactNames = false;
int artifactNameInd = parsedPath.length - 1;
int artifactGroupTypeInd = parsedPath.length - 3;
String groupType = parsedPath[artifactGroupTypeInd];
String artifactName = parsedPath[artifactNameInd];
String otherGroupType = otherParsedPath[artifactGroupTypeInd];
String otherArtifactName = otherParsedPath[artifactNameInd];
String vfcToscaName = parsedPath.length == 5 ? parsedPath[1] : null;
if (artifactName.equalsIgnoreCase(otherArtifactName) && groupType.equalsIgnoreCase(otherGroupType)) {
isEqualArtifactNames = vfcToscaName == null || vfcToscaName.equalsIgnoreCase(otherParsedPath[1]);
}
return isEqualArtifactNames;
}
public static Either<ImmutablePair<String, String>, ResponseFormat> getToscaYaml(Map<String, byte[]> csar, String csarUUID,
ComponentsUtils componentsUtils, AuditingActionEnum auditingAction) {
if (!AuditingActionEnum.UPDATE_SERVICE_TOSCA_TEMPLATE.equals(auditingAction)) {
Either<Boolean, ResponseFormat> validateStatus = validateIsTOSCAMetadataExist(csar, csarUUID, componentsUtils);
if (validateStatus.isRight()) {
return Either.right(validateStatus.right().value());
}
}
Pattern pattern = Pattern.compile(TOSCA_METADATA_PATH_PATTERN);
Optional<String> keyOp = csar.keySet().stream().filter(k -> pattern.matcher(k).matches()).findAny();
if (keyOp.isEmpty()) {
log.error(String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID));
BeEcompErrorManager.getInstance()
.logInternalDataError(TOSCA_METADATA_TOSCA_META_FILE_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_WITH_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID,
String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID)));
}
Properties props = new Properties();
try {
props.load(new StringReader(new String(csar.get(keyOp.get())).replace("\\", "\\\\")));
} catch (IOException e) {
log.error(String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID), e);
BeEcompErrorManager.getInstance()
.logInternalDataError(TOSCA_METADATA_TOSCA_META_FILE_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_WITH_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID,
String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID)));
}
String yamlFileName = props.getProperty(TOSCA_META_ENTRY_DEFINITIONS);
String result = Arrays.asList(yamlFileName.split(DEL_PATTERN)).stream().map(x -> x).collect(Collectors.joining(DEL_PATTERN));
keyOp = csar.keySet().stream().filter(k -> Pattern.compile(result).matcher(k).matches()).findAny();
if (keyOp.isEmpty()) {
log.error(ENTRY_DEFINITIONS_ENTRY_NOT_FOUND_IN_TOSCA_METADATA_TOSCA_META_FILE_CSAR_ID, csarUUID);
BeEcompErrorManager.getInstance()
.logInternalDataError(ENTRY_DEFINITIONS_ENTRY_NOT_FOUND_IN_TOSCA_METADATA_TOSCA_META_FILE_CSAR_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.YAML_NOT_FOUND_IN_CSAR, csarUUID, yamlFileName));
}
log.trace("Found Entry-Definitions property in TOSCA-Metadata/TOSCA.meta, Entry-Definitions: {}, CSAR id: {}", yamlFileName, csarUUID);
byte[] yamlFileBytes = csar.get(yamlFileName);
if (yamlFileBytes == null) {
log.error("Entry-Definitions {} file not found in csar, csar ID {}", yamlFileName, csarUUID);
BeEcompErrorManager.getInstance()
.logInternalDataError(ENTRY_DEFINITIONS + yamlFileName + FILE_NOT_FOUND_IN_CSAR_WITH_ID + csarUUID, CSAR_STRUCTURE_IS_INVALID,
ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.YAML_NOT_FOUND_IN_CSAR, csarUUID, yamlFileName));
}
return Either.left(new ImmutablePair<>(yamlFileName, new String(yamlFileBytes)));
}
public static Either<ImmutablePair<String, String>, ResponseFormat> getArtifactsMeta(Map<String, byte[]> csar, String csarUUID,
ComponentsUtils componentsUtils) {
if (!csar.containsKey(CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE)) {
log.error(ENTRY_DEFINITIONS_ENTRY_NOT_FOUND_IN_TOSCA_METADATA_TOSCA_META_FILE_CSAR_ID, csarUUID);
BeEcompErrorManager.getInstance()
.logInternalDataError(ENTRY_DEFINITIONS_ENTRY_NOT_FOUND_IN_TOSCA_METADATA_TOSCA_META_FILE_CSAR_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.YAML_NOT_FOUND_IN_CSAR, csarUUID, ARTIFACTS_METADATA_FILE));
}
log.trace("Found Entry-Definitions property in TOSCA-Metadata/TOSCA.meta, Entry-Definitions: {}, CSAR id: {}", ARTIFACTS_METADATA_FILE,
csarUUID);
byte[] artifactsMetaBytes = csar.get(CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE);
if (artifactsMetaBytes == null) {
log.error("Entry-Definitions {}{} file not found in csar, csar ID {}", CsarUtils.ARTIFACTS_PATH, ARTIFACTS_METADATA_FILE, csarUUID);
BeEcompErrorManager.getInstance().logInternalDataError(
ENTRY_DEFINITIONS + CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE + FILE_NOT_FOUND_IN_CSAR_WITH_ID + csarUUID,
CSAR_STRUCTURE_IS_INVALID, ErrorSeverity.ERROR);
return Either.right(
componentsUtils.getResponseFormat(ActionStatus.YAML_NOT_FOUND_IN_CSAR, csarUUID, CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE));
}
return Either.left(new ImmutablePair<>(CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE, new String(artifactsMetaBytes)));
}
public static Either<ImmutablePair<String, byte[]>, ResponseFormat> getArtifactContent(String csarUUID, Map<String, byte[]> csar,
String artifactPath, String artifactName,
ComponentsUtils componentsUtils) {
if (!csar.containsKey(artifactPath)) {
log.error("Entry-Definitions entry not found in Artifacts/HEAT.meta file, csar ID {}", csarUUID);
BeEcompErrorManager.getInstance()
.logInternalDataError(ENTRY_DEFINITIONS_ENTRY_NOT_FOUND_IN_TOSCA_METADATA_TOSCA_META_FILE_CSAR_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either
.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND_IN_CSAR, CsarUtils.ARTIFACTS_PATH + artifactName, csarUUID));
}
log.trace("Found Entry-Definitions property in Artifacts/HEAT.meta, Entry-Definitions: {}, CSAR id: {}", artifactPath, csarUUID);
byte[] artifactFileBytes = csar.get(artifactPath);
if (artifactFileBytes == null) {
log.error("Entry-Definitions {}{} file not found in csar, csar ID {}", CsarUtils.ARTIFACTS_PATH, artifactName, csarUUID);
BeEcompErrorManager.getInstance()
.logInternalDataError(ENTRY_DEFINITIONS + artifactPath + FILE_NOT_FOUND_IN_CSAR_WITH_ID + csarUUID, CSAR_STRUCTURE_IS_INVALID,
ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND_IN_CSAR, artifactPath, csarUUID));
}
return Either.left(new ImmutablePair<>(artifactName, artifactFileBytes));
}
private static Either<Boolean, ResponseFormat> validateTOSCAMetadataFile(Map<String, byte[]> csar, String csarUUID,
ComponentsUtils componentsUtils) {
Pattern pattern = Pattern.compile(TOSCA_METADATA_PATH_PATTERN);
Optional<String> keyOp = csar.keySet().stream().filter(k -> pattern.matcher(k).matches()).findAny();
if (keyOp.isEmpty()) {
log.error(String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID));
BeEcompErrorManager.getInstance()
.logInternalDataError(TOSCA_METADATA_TOSCA_META_FILE_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_WITH_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID,
String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID)));
}
String toscaMetadata = new String(csar.get(keyOp.get()));
Either<Boolean, ResponseFormat> blockStatus = validateBlock(csarUUID, componentsUtils, toscaMetadata);
if (blockStatus.isRight()) {
return blockStatus;
}
return Either.left(true);
}
private static Either<Boolean, ResponseFormat> validateBlock(String csarUUID, ComponentsUtils componentsUtils, String toscaMetadata) {
final String[] splited = toscaMetadata.split(NEW_LINE_DELM);
if (splited == null || splited.length < TOSCA_METADATA_FIELDS.length) {
log.error(String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID));
BeEcompErrorManager.getInstance()
.logInternalDataError(TOSCA_METADATA_TOSCA_META_FILE_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_WITH_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID,
String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID)));
}
final Properties props = new Properties();
try {
props.load(new ByteArrayInputStream(toscaMetadata.getBytes()));
} catch (final IOException e) {
log.error(String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID), e);
BeEcompErrorManager.getInstance()
.logInternalDataError(TOSCA_METADATA_TOSCA_META_FILE_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_WITH_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID,
String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID)));
}
for (final String toscaField : TOSCA_METADATA_FIELDS) {
if (!props.containsKey(toscaField)) {
final String msg = "TOSCA.meta file format is invalid: No new line after block_0 as expected in csar, csar ID {}";
log.error(msg, csarUUID);
BeEcompErrorManager.getInstance()
.logInternalDataError(TOSCA_METADATA_TOSCA_META_FILE_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_WITH_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID, msg, csarUUID));
}
final String value = props.getProperty(toscaField);
if (value == null || value.isEmpty()) {
log.error(String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID));
BeEcompErrorManager.getInstance()
.logInternalDataError(TOSCA_METADATA_TOSCA_META_FILE_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_WITH_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID,
String.format(TOSCA_METADATA_TOSCA_META_FILE_IS_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_CSAR_ID, csarUUID)));
}
// TOSCA-Meta-File-Version & CSAR-Version : digit.digit - format validation
if ((toscaField.equals(TOSCA_META_FILE_VERSION) || toscaField.equals(CSAR_VERSION)) && !validateTOSCAMetaProperty(value)) {
final String msg = "TOSCA-Metadata/TOSCA.meta file contains {} in wrong format (digit.digit), csar ID {}";
log.error(msg, toscaField, csarUUID);
BeEcompErrorManager.getInstance()
.logInternalDataError(TOSCA_METADATA_TOSCA_META_FILE_NOT_IN_EXPECTED_KEY_VALUE_FORM_IN_CSAR_WITH_ID + csarUUID,
CSAR_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID, msg, toscaField, csarUUID));
}
}
return Either.left(true);
}
private static boolean validateTOSCAMetaProperty(String toscaProperty) {
return floatPattern.matcher(toscaProperty).matches();
}
private static Either<Boolean, ResponseFormat> validateIsTOSCAMetadataExist(Map<String, byte[]> csar, String csarUUID,
ComponentsUtils componentsUtils) {
if (csar == null || csar.isEmpty()) {
log.error("Error when fetching csar with ID {}", csarUUID);
BeEcompErrorManager.getInstance().logBeDaoSystemError("Creating resource from CSAR: fetching CSAR with id " + csarUUID + " failed");
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID, csarUUID));
}
Pattern pattern = Pattern.compile(TOSCA_METADATA_PATH_PATTERN);
Optional<String> keyOp = csar.keySet().stream().filter(k -> pattern.matcher(k).matches()).findAny();
if (keyOp.isEmpty()) {
log.error("TOSCA-Metadata/TOSCA.meta file not found in csar, csar ID {}", csarUUID);
BeEcompErrorManager.getInstance()
.logInternalDataError("TOSCA-Metadata/TOSCA.meta file not found in CSAR with id " + csarUUID, CSAR_STRUCTURE_IS_INVALID,
ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID, csarUUID));
}
byte[] toscaMetaBytes = csar.get(keyOp.get());
if (toscaMetaBytes == null || toscaMetaBytes.length == 0) {
log.error("TOSCA-Metadata/TOSCA.meta file not found in csar, csar ID {}", csarUUID);
BeEcompErrorManager.getInstance()
.logInternalDataError("TOSCA-Metadata/TOSCA.meta file not found in CSAR with id " + csarUUID, CSAR_STRUCTURE_IS_INVALID,
ErrorSeverity.ERROR);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID, csarUUID));
}
return Either.left(Boolean.TRUE);
}
public static Either<String, ResponseFormat> getToscaYamlChecksum(Map<String, byte[]> csar, String csarUUID, ComponentsUtils componentsUtils) {
Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlRes = getToscaYaml(csar, csarUUID, componentsUtils, AuditingActionEnum.GET_TOSCA_MODEL);
if (toscaYamlRes.isRight() || toscaYamlRes.left().value() == null || toscaYamlRes.left().value().getRight() == null) {
log.error("Faild to create toscaYamlChecksum for csar, csar ID {}", csarUUID);
return Either.right(toscaYamlRes.right().value());
}
return Either.left(GeneralUtility.calculateMD5Base64EncodedByByteArray(toscaYamlRes.left().value().getRight().getBytes()));
}
public static boolean isCsarPayloadName(String payloadName) {
return payloadName != null && payloadName.toLowerCase().endsWith(TOSCA_CSAR_EXTENSION);
}
}
|