2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END========================================================================
20 package com.oransc.rappmanager.models.csar.validator;
22 import com.fasterxml.jackson.databind.JsonNode;
23 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
24 import com.oransc.rappmanager.models.csar.RappCsarPathProvider;
25 import com.oransc.rappmanager.models.exception.RappValidationException;
26 import java.util.List;
27 import lombok.RequiredArgsConstructor;
28 import org.springframework.stereotype.Component;
29 import org.springframework.validation.Errors;
30 import org.springframework.web.multipart.MultipartFile;
33 @RequiredArgsConstructor
34 public class ArtifactDefinitionValidator implements RappValidator {
36 private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
37 private final RappValidationUtils rappValidationUtils;
38 String invalidAsdErrorMsg = "ASD definition in rApp package is invalid.";
39 private static final int VALIDATION_ORDER = 10;
42 public int getOrder() {
43 return VALIDATION_ORDER;
47 public void validate(Object target, Errors errors) {
48 MultipartFile multipartFile = (MultipartFile) target;
49 String asdLocation = rappValidationUtils.getAsdDefinitionLocation(multipartFile);
50 if (asdLocation != null && !asdLocation.isEmpty() && rappValidationUtils.isFileExistsInCsar(multipartFile,
53 String asdContent = rappValidationUtils.getFileFromCsar(multipartFile, asdLocation).toString();
54 if(asdContent != null && !asdContent.isEmpty()) {
55 JsonNode jsonNode = rappCsarConfigurationHandler.getAsdContentNode(asdContent);
56 List<String> artifactFileList =
57 jsonNode.at(RappCsarPathProvider.ARTIFACTS_LOCATION_JSON_POINTER).findValuesAsText("file");
58 artifactFileList.forEach(
59 artifactFile -> rappValidationUtils.isFileExistsInCsar(multipartFile, artifactFile));
61 throw new RappValidationException(invalidAsdErrorMsg);
63 } catch (RappValidationException e) {
64 throw new RappValidationException(e.getMessage());
65 } catch (Exception e) {
66 throw new RappValidationException(invalidAsdErrorMsg);
69 throw new RappValidationException(invalidAsdErrorMsg);