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
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========================================================================
19 package com.oransc.rappmanager.rest;
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;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.StandardCopyOption;
33 import java.util.Collection;
34 import java.util.Optional;
35 import lombok.RequiredArgsConstructor;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
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;
51 @RequestMapping(path = "rapps")
52 @RequiredArgsConstructor
53 public class RappController {
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;
62 public ResponseEntity<Collection<Rapp>> getRapps() {
63 return ResponseEntity.ok(rappCacheService.getAllRapp());
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());
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();
87 logger.info("Invalid Rapp package for {}", rappId);
88 return ResponseEntity.badRequest().build();
92 @PutMapping("{rapp_id}")
93 public ResponseEntity<String> primeRapp(@PathVariable("rapp_id") String rappId,
94 @RequestBody RappPrimeOrder rappPrimeOrder) {
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());
105 @DeleteMapping("{rapp_id}")
106 public ResponseEntity<Object> deleteRapp(@PathVariable("rapp_id") String rappId) {
108 return rappCacheService.getRapp(rappId)
109 .filter(rapp -> rapp.getRappInstances().isEmpty() && rapp.getState().equals(RappState.COMMISSIONED))
111 rappCacheService.deleteRapp(rapp);
112 return ResponseEntity.ok().build();
114 .orElse(ResponseEntity.notFound().build());