Add validation for asd descriptor and invariant id
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / main / java / com / oransc / rappmanager / service / DeploymentArtifactsService.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.service;
21
22 import com.oransc.rappmanager.models.csar.DeploymentItem;
23 import com.oransc.rappmanager.models.csar.DeploymentItemArtifactType;
24 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
25 import com.oransc.rappmanager.models.exception.RappHandlerException;
26 import com.oransc.rappmanager.models.rapp.Rapp;
27 import lombok.RequiredArgsConstructor;
28 import org.springframework.core.io.ByteArrayResource;
29 import org.springframework.http.HttpEntity;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpMethod;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Service;
36 import org.springframework.web.client.HttpClientErrorException;
37 import org.springframework.web.client.RestTemplate;
38
39 @Service
40 @RequiredArgsConstructor
41 public class DeploymentArtifactsService {
42
43     private final RestTemplate restTemplate;
44     private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
45
46     public boolean configureDeploymentArtifacts(Rapp rapp) {
47         return rapp.getAsdMetadata().getDeploymentItems().stream()
48                        .filter(deploymentItem -> deploymentItem.getArtifactType()
49                                                          .equals(DeploymentItemArtifactType.HELMCHART))
50                        .allMatch(deploymentItem -> uploadHelmChart(rapp, deploymentItem));
51     }
52
53     boolean uploadHelmChart(Rapp rApp, DeploymentItem deploymentItem) throws RappHandlerException {
54         try {
55             HttpHeaders httpHeaders = new HttpHeaders();
56             httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
57             HttpEntity<ByteArrayResource> requestHttpEntity =
58                     new HttpEntity<>(rappCsarConfigurationHandler.getArtifactPayload(rApp, deploymentItem.getFile()),
59                             httpHeaders);
60             ResponseEntity<String> responseEntity =
61                     restTemplate.exchange(deploymentItem.getTargetServerUri(), HttpMethod.POST, requestHttpEntity,
62                             String.class);
63             if (responseEntity.getStatusCode().is2xxSuccessful()) {
64                 return true;
65             }
66         } catch (HttpClientErrorException exception) {
67             if (exception.getStatusCode().equals(HttpStatus.CONFLICT)) {
68                 return true;
69             }
70         } catch (Exception e) {
71             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
72                     String.format("Unable to connect to the chartmuseum server %s to upload helm artifact %s",
73                             deploymentItem.getTargetServerUri(), deploymentItem.getFile()));
74         }
75         return false;
76     }
77 }