Add Rapp Instances
[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.rappinstance.DeployOrder;
22 import com.oransc.rappmanager.models.rapp.Rapp;
23 import com.oransc.rappmanager.models.rappinstance.RappInstance;
24 import com.oransc.rappmanager.models.rappinstance.RappInstanceDeployOrder;
25 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
26 import com.oransc.rappmanager.models.cache.RappCacheService;
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.ResponseEntity;
35 import org.springframework.web.bind.annotation.DeleteMapping;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.PathVariable;
38 import org.springframework.web.bind.annotation.PostMapping;
39 import org.springframework.web.bind.annotation.PutMapping;
40 import org.springframework.web.bind.annotation.RequestBody;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.RestController;
43
44 @RestController
45 @RequestMapping(path = "rapps/{rapp_id}/instance")
46 @RequiredArgsConstructor
47 public class RappInstanceController {
48
49     private final RappCacheService rappCacheService;
50     private final RappInstanceStateMachine rappInstanceStateMachine;
51     private final RappService rappService;
52
53     @GetMapping
54     public ResponseEntity<Map<UUID, RappInstance>> getAllRappInstances(@PathVariable("rapp_id") String rappId) {
55         return rappCacheService.getRapp(rappId).map(Rapp::getRappInstances).map(ResponseEntity::ok)
56                        .orElse(ResponseEntity.notFound().build());
57     }
58
59     @PostMapping
60     public ResponseEntity<RappInstance> createRappInstance(@PathVariable("rapp_id") String rappId,
61             @RequestBody RappInstance rappInstance) {
62         return rappCacheService.getRapp(rappId).map(rapp -> {
63             rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
64             rapp.getRappInstances().put(rappInstance.getRappInstanceId(), rappInstance);
65             return ResponseEntity.ok(rappInstance);
66         }).orElse(ResponseEntity.notFound().build());
67     }
68
69     @GetMapping("{rapp_instance_id}")
70     public ResponseEntity<RappInstance> getRappInstance(@PathVariable("rapp_id") String rappId,
71             @PathVariable("rapp_instance_id") UUID rappInstanceId) {
72         return rappCacheService.getRapp(rappId).map(rapp -> Pair.of(rapp, rapp.getRappInstances().get(rappInstanceId)))
73                        .filter(rappPair -> rappPair.getLeft().getRappInstances().containsKey(rappInstanceId))
74                        .map(rappPair -> {
75                            rappService.updateRappInstanceState(rappPair.getLeft(), rappPair.getRight());
76                            RappInstance rappInstance = rappPair.getLeft().getRappInstances().get(rappInstanceId);
77                            rappInstance.setState(rappInstanceStateMachine.getRappInstanceState(rappInstanceId));
78                            return rappInstance;
79                        }).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
80     }
81
82     @PutMapping("{rapp_instance_id}")
83     public ResponseEntity<String> deployRappInstance(@PathVariable("rapp_id") String rappId,
84             @PathVariable("rapp_instance_id") UUID rappInstanceId,
85             @RequestBody RappInstanceDeployOrder rappInstanceDeployOrder) {
86         //@formatter:off
87         return rappCacheService.getRapp(rappId)
88                    .filter(rapp -> rapp.getRappInstances().containsKey(rappInstanceId))
89                    .map(rapp -> Pair.of(rapp, rapp.getRappInstances().get(rappInstanceId)))
90                    .map(rappPair -> Optional.ofNullable(rappInstanceDeployOrder.getDeployOrder())
91                         .filter(deployOrder -> deployOrder.equals(DeployOrder.DEPLOY))
92                         .map(primeOrder -> rappService.deployRappInstance(rappPair.getLeft(), rappPair.getRight()))
93                         .orElseGet(() -> rappService.undeployRappInstance(rappPair.getLeft(), rappPair.getRight())))
94                    .orElse(ResponseEntity.notFound().build());
95         //@formatter:on
96     }
97
98     @DeleteMapping("{rapp_instance_id}")
99     public ResponseEntity<Object> deleteRappInstance(@PathVariable("rapp_id") String rappId,
100             @PathVariable("rapp_instance_id") UUID rappInstanceId) {
101         return rappCacheService.getRapp(rappId).map(rapp -> Pair.of(rapp, rapp.getRappInstances()))
102                        .filter(rappPair -> rappPair.getRight().containsKey(rappInstanceId) && rappPair.getRight()
103                                                                                                       .get(rappInstanceId)
104                                                                                                       .getState()
105                                                                                                       .equals(RappInstanceState.UNDEPLOYED))
106                        .map(rappPair -> {
107                            rappInstanceStateMachine.deleteRappInstance(
108                                    rappPair.getLeft().getRappInstances().get(rappInstanceId));
109                            rappPair.getLeft().getRappInstances().remove(rappInstanceId);
110                            return ResponseEntity.noContent().build();
111                        }).orElse(ResponseEntity.notFound().build());
112     }
113
114 }