Add installation script
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / main / java / com / oransc / rappmanager / rest / RappController.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. 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 package com.oransc.rappmanager.rest;
20
21 import com.oransc.rappmanager.configuration.RappManagerConfiguration;
22 import com.oransc.rappmanager.models.cache.RappCacheService;
23 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
24 import com.oransc.rappmanager.models.rapp.PrimeOrder;
25 import com.oransc.rappmanager.models.rapp.Rapp;
26 import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
27 import com.oransc.rappmanager.models.rapp.RappState;
28 import com.oransc.rappmanager.service.RappService;
29 import java.io.File;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.StandardCopyOption;
33 import java.util.Optional;
34 import lombok.RequiredArgsConstructor;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.cache.Cache;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.bind.annotation.DeleteMapping;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.PostMapping;
43 import org.springframework.web.bind.annotation.PutMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestPart;
47 import org.springframework.web.bind.annotation.RestController;
48 import org.springframework.web.multipart.MultipartFile;
49
50 @RestController
51 @RequestMapping(path = "rapps")
52 @RequiredArgsConstructor
53 public class RappController {
54
55     Logger logger = LoggerFactory.getLogger(RappController.class);
56     private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
57     private final RappManagerConfiguration rappManagerConfiguration;
58     private final RappCacheService rappCacheService;
59     private final RappService rappService;
60
61     @GetMapping
62     public ResponseEntity<Cache> getRapps() {
63         return ResponseEntity.ok(rappCacheService.getAllRapp());
64     }
65
66     @GetMapping("{rapp_id}")
67     public ResponseEntity<Rapp> getRapp(@PathVariable("rapp_id") String rappId) {
68         return rappCacheService.getRapp(rappId).map(ResponseEntity::ok).orElse(ResponseEntity.badRequest().build());
69     }
70
71     @PostMapping("{rapp_id}")
72     public ResponseEntity<Rapp> createRapp(@PathVariable("rapp_id") String rappId,
73             @RequestPart("file") MultipartFile csarFilePart) throws IOException {
74         if (rappCsarConfigurationHandler.isValidRappPackage(csarFilePart)) {
75             File csarFile = new File(
76                     rappCsarConfigurationHandler.getRappPackageLocation(rappManagerConfiguration.getCsarLocation(),
77                             rappId, csarFilePart.getOriginalFilename()).toUri());
78             csarFile.getParentFile().mkdirs();
79             Files.copy(csarFilePart.getInputStream(), csarFile.getAbsoluteFile().toPath(),
80                     StandardCopyOption.REPLACE_EXISTING);
81             Rapp rapp = Rapp.builder().name(rappId).packageLocation(rappManagerConfiguration.getCsarLocation())
82                                 .packageName(csarFile.getName()).state(RappState.COMMISSIONED).build();
83             rapp.setRappResources(rappCsarConfigurationHandler.getRappResource(rapp));
84             rappCacheService.putRapp(rapp);
85             return ResponseEntity.accepted().build();
86         } else {
87             logger.info("Invalid Rapp package for {}", rappId);
88             return ResponseEntity.badRequest().build();
89         }
90     }
91
92     @PutMapping("{rapp_id}")
93     public ResponseEntity<String> primeRapp(@PathVariable("rapp_id") String rappId,
94             @RequestBody RappPrimeOrder rappPrimeOrder) {
95         // @formatter:off
96         return rappCacheService.getRapp(rappId)
97                        .map(rapp -> Optional.ofNullable(rappPrimeOrder.getPrimeOrder())
98                             .filter(primeOrder -> primeOrder.equals(PrimeOrder.PRIME))
99                             .map(primeOrder -> rappService.primeRapp(rapp))
100                             .orElseGet(() -> rappService.deprimeRapp(rapp)))
101                        .orElse(ResponseEntity.notFound().build());
102         // @formatter:on
103     }
104
105     @DeleteMapping("{rapp_id}")
106     public ResponseEntity<Object> deleteRapp(@PathVariable("rapp_id") String rappId) {
107         // @formatter:off
108         return rappCacheService.getRapp(rappId)
109                .filter(rapp -> rapp.getRappInstances().isEmpty() && rapp.getState().equals(RappState.COMMISSIONED))
110                .map(rapp -> {
111                    rappCacheService.deleteRapp(rapp);
112                    return ResponseEntity.ok().build();
113                })
114                .orElse(ResponseEntity.notFound().build());
115         // @formatter:on
116     }
117 }