Add validation for asd descriptor and invariant id
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / main / java / com / oransc / rappmanager / models / csar / validator / ArtifactDefinitionValidator.java
1 /*
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
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  * ============LICENSE_END========================================================================
17  *
18  */
19
20 package com.oransc.rappmanager.models.csar.validator;
21
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;
31
32 @Component
33 @RequiredArgsConstructor
34 public class ArtifactDefinitionValidator implements RappValidator {
35
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 = 15;
40
41     @Override
42     public int getOrder() {
43         return VALIDATION_ORDER;
44     }
45
46     @Override
47     public void validate(Object target, Errors errors) {
48         MultipartFile multipartFile = (MultipartFile) target;
49         String asdLocation = rappValidationUtils.getAsdDefinitionLocation(multipartFile);
50         try {
51             String asdContent = rappValidationUtils.getFileFromCsar(multipartFile, asdLocation).toString();
52             JsonNode jsonNode = rappCsarConfigurationHandler.getAsdContentNode(asdContent);
53             List<String> artifactFileList =
54                     jsonNode.at(RappCsarPathProvider.ASD_ARTIFACTS_LOCATION_JSON_POINTER).findValuesAsText("file");
55             artifactFileList.forEach(
56                     artifactFile -> rappValidationUtils.isFileExistsInCsar(multipartFile, artifactFile));
57         } catch (RappValidationException e) {
58             throw new RappValidationException(e.getMessage());
59         } catch (Exception e) {
60             throw new RappValidationException(invalidAsdErrorMsg);
61         }
62     }
63 }