Add initial version of code
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / main / java / com / oransc / rappmanager / rest / OnboardingController.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.acm.service.AcmDeployer;
22 import com.oransc.rappmanager.configuration.RappManagerConfiguration;
23 import com.oransc.rappmanager.models.Rapp;
24 import com.oransc.rappmanager.models.RappCsarConfigurationHandler;
25 import com.oransc.rappmanager.models.RappEvent;
26 import com.oransc.rappmanager.models.RappState;
27 import com.oransc.rappmanager.models.cache.RappCacheService;
28 import com.oransc.rappmanager.models.statemachine.RappStateMachine;
29 import com.oransc.rappmanager.sme.service.SmeDeployer;
30 import java.io.File;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.StandardCopyOption;
34 import java.util.Optional;
35 import lombok.RequiredArgsConstructor;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.cache.Cache;
39 import org.springframework.http.ResponseEntity;
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.RequestMapping;
44 import org.springframework.web.bind.annotation.RequestPart;
45 import org.springframework.web.bind.annotation.RestController;
46 import org.springframework.web.multipart.MultipartFile;
47
48 @RestController
49 @RequestMapping(path = "rapps")
50 @RequiredArgsConstructor
51 public class OnboardingController {
52
53     Logger logger = LoggerFactory.getLogger(OnboardingController.class);
54     private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
55     private final AcmDeployer acmDeployer;
56     private final SmeDeployer smeDeployer;
57     private final RappManagerConfiguration rappManagerConfiguration;
58     private final RappStateMachine rappStateMachine;
59     private final RappCacheService rappCacheService;
60
61
62     @GetMapping
63     public ResponseEntity<Cache> getRapps() {
64         return ResponseEntity.ok(rappCacheService.getAllRapp());
65     }
66
67     @GetMapping("{rapp_id}")
68     public ResponseEntity<Rapp> getRapps(@PathVariable("rapp_id") String rappId) {
69         Optional<Rapp> rappOptional = rappCacheService.getRapp(rappId);
70         if (rappOptional.isPresent()) {
71             acmDeployer.syncRappStatus(rappOptional.get());
72             return ResponseEntity.ok(rappCacheService.getRapp(rappId).get());
73         }
74         return ResponseEntity.badRequest().build();
75     }
76
77     @PostMapping("{rapp_id}/onboard")
78     public ResponseEntity<Object> uploadRappCsarFile(@PathVariable("rapp_id") String rappId,
79             @RequestPart("file") MultipartFile csarFilePart) throws IOException {
80         if (rappCsarConfigurationHandler.isValidRappPackage(csarFilePart)) {
81             File csarFile = new File(
82                     rappCsarConfigurationHandler.getRappPackageLocation(rappManagerConfiguration.getCsarLocation(),
83                             rappId, csarFilePart.getOriginalFilename()).toUri());
84             csarFile.getParentFile().mkdirs();
85             Files.copy(csarFilePart.getInputStream(), csarFile.getAbsoluteFile().toPath(),
86                     StandardCopyOption.REPLACE_EXISTING);
87             Rapp rapp = Rapp.builder().name(rappId).packageLocation(rappManagerConfiguration.getCsarLocation())
88                                 .packageName(csarFile.getName()).state(RappState.ONBOARDED).build();
89             rappCacheService.putRapp(rapp);
90             rappStateMachine.onboardRapp(rapp.getRappId());
91             return ResponseEntity.accepted().build();
92         } else {
93             logger.info("Invalid Rapp package for {}", rappId);
94             return ResponseEntity.badRequest().build();
95         }
96     }
97
98     @PostMapping("{rapp_id}/deploy")
99     public ResponseEntity<?> deployRapp(@PathVariable("rapp_id") String rappId) {
100         Optional<Rapp> rappOptional = rappCacheService.getRapp(rappId);
101         rappOptional.ifPresent(rapp -> {
102             rappStateMachine.sendRappEvent(rapp, RappEvent.DEPLOYING);
103         });
104         if (rappOptional.isPresent() && acmDeployer.deployRapp(rappOptional.get()) && smeDeployer.deployRapp(
105                 rappOptional.get())) {
106             return ResponseEntity.accepted().build();
107         }
108         return ResponseEntity.internalServerError().build();
109     }
110
111     @PostMapping("{rapp_id}/undeploy")
112     public ResponseEntity<?> undeployRapp(@PathVariable("rapp_id") String rappId) {
113         Optional<Rapp> rappOptional = rappCacheService.getRapp(rappId);
114         rappOptional.ifPresent(rapp -> {
115             rappStateMachine.sendRappEvent(rapp, RappEvent.UNDEPLOYING);
116         });
117         if (rappOptional.isPresent() && acmDeployer.undeployRapp(rappOptional.get()) && smeDeployer.undeployRapp(
118                 rappOptional.get())) {
119             rappCacheService.deleteRapp(rappOptional.get());
120             rappStateMachine.deleteRapp(rappOptional.get());
121             return ResponseEntity.accepted().build();
122         }
123         return ResponseEntity.internalServerError().build();
124     }
125
126     @GetMapping("info")
127     public ResponseEntity<Object> getInfo() {
128         return ResponseEntity.ok(acmDeployer.getAllParticipants());
129     }
130
131 }