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.cache.RappCacheService;
24 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
25 import com.oransc.rappmanager.models.csar.RappCsarPathProvider;
26 import com.oransc.rappmanager.models.exception.RappValidationException;
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 AsdDescriptorValidator implements RappValidator {
36 private final RappCacheService rappCacheService;
37 private final RappValidationUtils rappValidationUtils;
38 private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
39 String invalidAsdErrorMsg = "ASD definition in rApp package is invalid.";
41 private static final int VALIDATION_ORDER = 10;
44 public int getOrder() {
45 return VALIDATION_ORDER;
49 public void validate(Object target, Errors errors) {
50 MultipartFile multipartFile = (MultipartFile) target;
51 String asdLocation = rappValidationUtils.getAsdDefinitionLocation(multipartFile);
52 if (asdLocation != null && !asdLocation.isEmpty() && rappValidationUtils.isFileExistsInCsar(multipartFile,
55 String asdContent = rappValidationUtils.getFileFromCsar(multipartFile, asdLocation).toString();
56 if (asdContent != null && !asdContent.isEmpty()) {
57 JsonNode jsonNode = rappCsarConfigurationHandler.getAsdContentNode(asdContent);
58 checkAsdDescriptorExists(jsonNode.at(RappCsarPathProvider.ASD_DESCRIPTOR_JSON_POINTER).asText());
59 checkAsdDescriptorVariantExists(
60 jsonNode.at(RappCsarPathProvider.ASD_DESCRIPTOR_VARIANT_LOCATION_JSON_POINTER).asText());
62 throw new RappValidationException(invalidAsdErrorMsg);
64 } catch (RappValidationException e) {
65 throw new RappValidationException(e.getMessage());
66 } catch (Exception e) {
67 throw new RappValidationException(invalidAsdErrorMsg);
70 throw new RappValidationException(invalidAsdErrorMsg);
74 boolean checkAsdDescriptorExists(String descriptorId) {
75 if (rappCacheService.getAllRapp().stream()
76 .anyMatch(rapp -> rapp.getAsdMetadata().getDescriptorId().equals(descriptorId))) {
77 throw new RappValidationException("ASD descriptor already exists.");
82 boolean checkAsdDescriptorVariantExists(String descriptorVariantId) {
83 if (rappCacheService.getAllRapp().stream()
84 .anyMatch(rapp -> rapp.getAsdMetadata().getDescriptorInvariantId().equals(descriptorVariantId))) {
85 throw new RappValidationException("ASD descriptor invariant already exists.");