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.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;
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;
45 @RequestMapping(path = "rapps/{rapp_id}/instance")
46 @RequiredArgsConstructor
47 public class RappInstanceController {
49 private final RappCacheService rappCacheService;
50 private final RappInstanceStateMachine rappInstanceStateMachine;
51 private final RappService rappService;
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());
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());
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))
75 rappService.updateRappInstanceState(rappPair.getLeft(), rappPair.getRight());
76 RappInstance rappInstance = rappPair.getLeft().getRappInstances().get(rappInstanceId);
77 rappInstance.setState(rappInstanceStateMachine.getRappInstanceState(rappInstanceId));
79 }).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
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) {
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());
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()
105 .equals(RappInstanceState.UNDEPLOYED))
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());