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.exception.RappHandlerException;
25 import com.oransc.rappmanager.models.rapp.PrimeOrder;
26 import com.oransc.rappmanager.models.rapp.Rapp;
27 import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
28 import com.oransc.rappmanager.models.rapp.RappState;
29 import com.oransc.rappmanager.service.RappService;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.StandardCopyOption;
34 import java.util.Collection;
35 import java.util.Optional;
36 import lombok.RequiredArgsConstructor;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.DeleteMapping;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.PutMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import org.springframework.web.bind.annotation.RequestPart;
49 import org.springframework.web.bind.annotation.RestController;
50 import org.springframework.web.multipart.MultipartFile;
53 @RequestMapping(path = "rapps")
54 @RequiredArgsConstructor
55 public class RappController {
57 Logger logger = LoggerFactory.getLogger(RappController.class);
58 private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
59 private final RappManagerConfiguration rappManagerConfiguration;
60 private final RappCacheService rappCacheService;
61 private final RappService rappService;
62 private static final String RAPP_NOT_FOUND = "rApp %s not found.";
65 public ResponseEntity<Collection<Rapp>> getRapps() {
66 return ResponseEntity.ok(rappCacheService.getAllRapp());
69 @GetMapping("{rapp_id}")
70 public ResponseEntity<Rapp> getRapp(@PathVariable("rapp_id") String rappId) {
71 return rappCacheService.getRapp(rappId).map(ResponseEntity::ok).orElseThrow(
72 () -> new RappHandlerException(HttpStatus.NOT_FOUND, String.format(RAPP_NOT_FOUND, rappId)));
75 @PostMapping("{rapp_id}")
76 public ResponseEntity<Rapp> createRapp(@PathVariable("rapp_id") String rappId,
77 @RequestPart("file") MultipartFile csarFilePart) throws IOException {
78 if (rappCsarConfigurationHandler.isValidRappPackage(csarFilePart)) {
79 File csarFile = new File(
80 rappCsarConfigurationHandler.getRappPackageLocation(rappManagerConfiguration.getCsarLocation(),
81 rappId, csarFilePart.getOriginalFilename()).toUri());
82 csarFile.getParentFile().mkdirs();
83 Files.copy(csarFilePart.getInputStream(), csarFile.getAbsoluteFile().toPath(),
84 StandardCopyOption.REPLACE_EXISTING);
85 Rapp rapp = Rapp.builder().name(rappId).packageLocation(rappManagerConfiguration.getCsarLocation())
86 .packageName(csarFile.getName()).state(RappState.COMMISSIONED).build();
87 rapp.setRappResources(rappCsarConfigurationHandler.getRappResource(rapp));
88 rappCacheService.putRapp(rapp);
89 return ResponseEntity.accepted().build();
91 logger.info("Invalid Rapp package for {}", rappId);
92 throw new RappHandlerException(HttpStatus.BAD_REQUEST, "Invalid rApp package.");
96 @PutMapping("{rapp_id}")
97 public ResponseEntity<String> primeRapp(@PathVariable("rapp_id") String rappId,
98 @RequestBody RappPrimeOrder rappPrimeOrder) {
100 return rappCacheService.getRapp(rappId)
101 .map(rapp -> Optional.ofNullable(rappPrimeOrder.getPrimeOrder())
102 .filter(primeOrder -> primeOrder.equals(PrimeOrder.PRIME))
103 .map(primeOrder -> rappService.primeRapp(rapp))
104 .orElseGet(() -> rappService.deprimeRapp(rapp)))
105 .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
106 String.format(RAPP_NOT_FOUND, rappId)));
110 @DeleteMapping("{rapp_id}")
111 public ResponseEntity<String> deleteRapp(@PathVariable("rapp_id") String rappId) {
113 return rappCacheService.getRapp(rappId)
114 .map(rappService::deleteRapp)
115 .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
116 String.format(RAPP_NOT_FOUND, rappId)));