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.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;
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;
47 @RequestMapping(path = "rapps/{rapp_id}/instance")
48 @RequiredArgsConstructor
49 public class RappInstanceController {
51 private final RappCacheService rappCacheService;
52 private final RappInstanceStateMachine rappInstanceStateMachine;
53 private final RappService rappService;
54 private static final String RAPP_INSTANCE_NOT_FOUND = "rApp instance %s not found.";
57 public ResponseEntity<Map<UUID, RappInstance>> getAllRappInstances(@PathVariable("rapp_id") String rappId) {
58 return rappCacheService.getRapp(rappId).map(Rapp::getRappInstances).map(ResponseEntity::ok).orElseThrow(
59 () -> new RappHandlerException(HttpStatus.NOT_FOUND, "No instance found for rApp '" + rappId + "'."));
63 public ResponseEntity<RappInstance> createRappInstance(@PathVariable("rapp_id") String rappId,
64 @RequestBody RappInstance rappInstance) {
65 return rappCacheService.getRapp(rappId).map(rapp -> {
66 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
67 rapp.getRappInstances().put(rappInstance.getRappInstanceId(), rappInstance);
68 return ResponseEntity.ok(rappInstance);
69 }).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND, "rApp '" + rappId + "' not found."));
72 @GetMapping("{rapp_instance_id}")
73 public ResponseEntity<RappInstance> getRappInstance(@PathVariable("rapp_id") String rappId,
74 @PathVariable("rapp_instance_id") UUID rappInstanceId) {
75 return rappCacheService.getRapp(rappId).map(rapp -> Pair.of(rapp, rapp.getRappInstances().get(rappInstanceId)))
76 .filter(rappPair -> rappPair.getLeft().getRappInstances().containsKey(rappInstanceId))
78 rappService.updateRappInstanceState(rappPair.getLeft(), rappPair.getRight());
79 RappInstance rappInstance = rappPair.getLeft().getRappInstances().get(rappInstanceId);
80 rappInstance.setState(rappInstanceStateMachine.getRappInstanceState(rappInstanceId));
82 }).map(ResponseEntity::ok).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
83 String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));
86 @PutMapping("{rapp_instance_id}")
87 public ResponseEntity<String> deployRappInstance(@PathVariable("rapp_id") String rappId,
88 @PathVariable("rapp_instance_id") UUID rappInstanceId,
89 @RequestBody RappInstanceDeployOrder rappInstanceDeployOrder) {
91 return rappCacheService.getRapp(rappId)
92 .filter(rapp -> rapp.getRappInstances().containsKey(rappInstanceId))
93 .map(rapp -> Pair.of(rapp, rapp.getRappInstances().get(rappInstanceId)))
94 .map(rappPair -> Optional.ofNullable(rappInstanceDeployOrder.getDeployOrder())
95 .filter(deployOrder -> deployOrder.equals(DeployOrder.DEPLOY))
96 .map(primeOrder -> rappService.deployRappInstance(rappPair.getLeft(), rappPair.getRight()))
97 .orElseGet(() -> rappService.undeployRappInstance(rappPair.getLeft(), rappPair.getRight())))
98 .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
99 String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));
103 @DeleteMapping("{rapp_instance_id}")
104 public ResponseEntity<Object> deleteRappInstance(@PathVariable("rapp_id") String rappId,
105 @PathVariable("rapp_instance_id") UUID rappInstanceId) {
106 return rappCacheService.getRapp(rappId).map(rapp -> Pair.of(rapp, rapp.getRappInstances()))
107 .filter(rappPair -> rappPair.getRight().containsKey(rappInstanceId) && rappPair.getRight()
110 .equals(RappInstanceState.UNDEPLOYED))
112 rappInstanceStateMachine.deleteRappInstance(
113 rappPair.getLeft().getRappInstances().get(rappInstanceId));
114 rappPair.getLeft().getRappInstances().remove(rappInstanceId);
115 return ResponseEntity.noContent().build();
116 }).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
117 String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));