Provide proper response message/code in delete rapp instance
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / main / java / com / oransc / rappmanager / rest / RappInstanceController.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.models.cache.RappCacheService;
22 import com.oransc.rappmanager.models.exception.RappHandlerException;
23 import com.oransc.rappmanager.models.rapp.Rapp;
24 import com.oransc.rappmanager.models.rappinstance.DeployOrder;
25 import com.oransc.rappmanager.models.rappinstance.RappInstance;
26 import com.oransc.rappmanager.models.rappinstance.RappInstanceDeployOrder;
27 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
28 import com.oransc.rappmanager.service.RappService;
29 import java.util.Map;
30 import java.util.Optional;
31 import java.util.UUID;
32 import lombok.RequiredArgsConstructor;
33 import org.apache.commons.lang3.tuple.Pair;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.DeleteMapping;
37 import org.springframework.web.bind.annotation.GetMapping;
38 import org.springframework.web.bind.annotation.PathVariable;
39 import org.springframework.web.bind.annotation.PostMapping;
40 import org.springframework.web.bind.annotation.PutMapping;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RestController;
44
45 @RestController
46 @RequestMapping(path = "rapps/{rapp_id}/instance")
47 @RequiredArgsConstructor
48 public class RappInstanceController {
49
50     private final RappCacheService rappCacheService;
51     private final RappInstanceStateMachine rappInstanceStateMachine;
52     private final RappService rappService;
53     private static final String RAPP_INSTANCE_NOT_FOUND = "rApp instance %s not found.";
54
55     @GetMapping
56     public ResponseEntity<Map<UUID, RappInstance>> getAllRappInstances(@PathVariable("rapp_id") String rappId) {
57         return rappCacheService.getRapp(rappId).map(Rapp::getRappInstances).map(ResponseEntity::ok).orElseThrow(
58                 () -> new RappHandlerException(HttpStatus.NOT_FOUND, "No instance found for rApp '" + rappId + "'."));
59     }
60
61     @PostMapping
62     public ResponseEntity<RappInstance> createRappInstance(@PathVariable("rapp_id") String rappId,
63             @RequestBody RappInstance rappInstance) {
64         return rappCacheService.getRapp(rappId).map(rapp -> {
65             rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
66             rapp.getRappInstances().put(rappInstance.getRappInstanceId(), rappInstance);
67             return ResponseEntity.ok(rappInstance);
68         }).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND, "rApp '" + rappId + "' not found."));
69     }
70
71     @GetMapping("{rapp_instance_id}")
72     public ResponseEntity<RappInstance> getRappInstance(@PathVariable("rapp_id") String rappId,
73             @PathVariable("rapp_instance_id") UUID rappInstanceId) {
74         return rappCacheService.getRapp(rappId).map(rapp -> Pair.of(rapp, rapp.getRappInstances().get(rappInstanceId)))
75                        .filter(rappPair -> rappPair.getLeft().getRappInstances().containsKey(rappInstanceId))
76                        .map(rappPair -> {
77                            rappService.updateRappInstanceState(rappPair.getLeft(), rappPair.getRight());
78                            RappInstance rappInstance = rappPair.getLeft().getRappInstances().get(rappInstanceId);
79                            rappInstance.setState(rappInstanceStateMachine.getRappInstanceState(rappInstanceId));
80                            return rappInstance;
81                        }).map(ResponseEntity::ok).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
82                         String.format(RAPP_INSTANCE_NOT_FOUND, rappInstanceId)));
83     }
84
85     @PutMapping("{rapp_instance_id}")
86     public ResponseEntity<String> deployRappInstance(@PathVariable("rapp_id") String rappId,
87             @PathVariable("rapp_instance_id") UUID rappInstanceId,
88             @RequestBody RappInstanceDeployOrder rappInstanceDeployOrder) {
89         //@formatter:off
90         return rappCacheService.getRapp(rappId)
91                    .filter(rapp -> rapp.getRappInstances().containsKey(rappInstanceId))
92                    .map(rapp -> Pair.of(rapp, rapp.getRappInstances().get(rappInstanceId)))
93                    .map(rappPair -> Optional.ofNullable(rappInstanceDeployOrder.getDeployOrder())
94                         .filter(deployOrder -> deployOrder.equals(DeployOrder.DEPLOY))
95                         .map(primeOrder -> rappService.deployRappInstance(rappPair.getLeft(), rappPair.getRight()))
96                         .orElseGet(() -> rappService.undeployRappInstance(rappPair.getLeft(), rappPair.getRight())))
97                    .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
98                            String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));
99         //@formatter:on
100     }
101
102     @DeleteMapping("{rapp_instance_id}")
103     public ResponseEntity<Object> deleteRappInstance(@PathVariable("rapp_id") String rappId,
104             @PathVariable("rapp_instance_id") UUID rappInstanceId) {
105         return rappCacheService.getRapp(rappId).filter(rApp -> rApp.getRappInstances().containsKey(rappInstanceId))
106                        .map(rApp -> rappService.deleteRappInstance(rApp, rappInstanceId)).orElseThrow(
107                         () -> new RappHandlerException(HttpStatus.NOT_FOUND,
108                                 String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));
109     }
110
111 }