Add Error messages in rest response
[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.rappinstance.RappInstanceState;
28 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
29 import com.oransc.rappmanager.service.RappService;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.UUID;
33 import lombok.RequiredArgsConstructor;
34 import org.apache.commons.lang3.tuple.Pair;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.DeleteMapping;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.PostMapping;
41 import org.springframework.web.bind.annotation.PutMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RestController;
45
46 @RestController
47 @RequestMapping(path = "rapps/{rapp_id}/instance")
48 @RequiredArgsConstructor
49 public class RappInstanceController {
50
51     private final RappCacheService rappCacheService;
52     private final RappInstanceStateMachine rappInstanceStateMachine;
53     private final RappService rappService;
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                         "No instance found for rApp '" + rappId + "'."));
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                            "rApp instance '" + rappInstanceId + "' not found."));
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).map(rapp -> Pair.of(rapp, rapp.getRappInstances()))
106                        .filter(rappPair -> rappPair.getRight().containsKey(rappInstanceId) && rappPair.getRight()
107                                                                                                       .get(rappInstanceId)
108                                                                                                       .getState()
109                                                                                                       .equals(RappInstanceState.UNDEPLOYED))
110                        .map(rappPair -> {
111                            rappInstanceStateMachine.deleteRappInstance(
112                                    rappPair.getLeft().getRappInstances().get(rappInstanceId));
113                            rappPair.getLeft().getRappInstances().remove(rappInstanceId);
114                            return ResponseEntity.noContent().build();
115                        }).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
116                         "rApp instance '" + rappInstanceId + "' not found."));
117     }
118
119 }