2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved.
5 * ===============================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END========================================================================
20 package com.oransc.rappmanager.service;
22 import com.oransc.rappmanager.acm.service.AcmDeployer;
23 import com.oransc.rappmanager.models.RappDeployer;
24 import com.oransc.rappmanager.models.cache.RappCacheService;
25 import com.oransc.rappmanager.models.exception.RappHandlerException;
26 import com.oransc.rappmanager.models.exception.RappValidationException;
27 import com.oransc.rappmanager.models.rapp.Rapp;
28 import com.oransc.rappmanager.models.rapp.RappEvent;
29 import com.oransc.rappmanager.models.rapp.RappState;
30 import com.oransc.rappmanager.models.rappinstance.RappInstance;
31 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
32 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
33 import java.util.List;
34 import java.util.UUID;
35 import lombok.RequiredArgsConstructor;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Service;
41 @RequiredArgsConstructor
42 public class RappService {
44 private final AcmDeployer acmDeployer;
45 private final List<RappDeployer> rappDeployers;
46 private final RappInstanceStateMachine rappInstanceStateMachine;
47 private final RappCacheService rappCacheService;
48 private final DeploymentArtifactsService deploymentArtifactsService;
49 private static final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
51 public ResponseEntity<String> primeRapp(Rapp rapp) {
52 if (rapp.getState().equals(RappState.COMMISSIONED)) {
53 rapp.setState(RappState.PRIMING);
56 //Configuring the deployment artifact needs to be done before starting the priming with other components
57 //If there are additional conditions needs to be checked before priming, This needs handled as part of pre-priming stage.
58 if (deploymentArtifactsService.configureDeploymentArtifacts(rapp) && rappDeployers.parallelStream()
60 rappDeployer -> rappDeployer.primeRapp(
62 rapp.setState(RappState.PRIMED);
63 return ResponseEntity.ok().build();
65 } catch (RappValidationException e) {
66 rapp.setState(RappState.COMMISSIONED);
69 rapp.setState(RappState.COMMISSIONED);
70 throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rapp.getReason());
72 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
73 String.format(STATE_TRANSITION_NOT_PERMITTED, rapp.getState().name(), RappState.PRIMED.name()));
77 public ResponseEntity<String> deprimeRapp(Rapp rapp) {
78 if (rapp.getState().equals(RappState.PRIMED) && rapp.getRappInstances().isEmpty()) {
79 rapp.setState(RappState.DEPRIMING);
81 if (rappDeployers.parallelStream().allMatch(rappDeployer -> rappDeployer.deprimeRapp(rapp))) {
82 rapp.setState(RappState.COMMISSIONED);
83 return ResponseEntity.ok().build();
85 rapp.setState(RappState.PRIMED);
86 throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rapp.getReason());
88 if (!rapp.getRappInstances().isEmpty()) {
89 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
90 "Unable to deprime as there are active rapp instances.");
92 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
93 String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(),
94 rapp.getState().name()));
98 public ResponseEntity<String> deleteRapp(Rapp rApp) {
99 if (rApp.getRappInstances().isEmpty() && rApp.getState().equals(RappState.COMMISSIONED)) {
100 rappCacheService.deleteRapp(rApp);
101 return ResponseEntity.ok().build();
103 if (!rApp.getRappInstances().isEmpty()) {
104 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
105 String.format("Unable to delete %s as there are active rApp instances.", rApp.getName()));
107 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
108 String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rApp.getName()));
113 public ResponseEntity<String> deployRappInstance(Rapp rapp, RappInstance rappInstance) {
114 if (rappInstance.getState().equals(RappInstanceState.UNDEPLOYED)) {
115 rappInstance.setReason(null);
116 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING);
117 if (rappDeployers.parallelStream()
118 .allMatch(rappDeployer -> rappDeployer.deployRappInstance(rapp, rappInstance))) {
119 return ResponseEntity.accepted().build();
121 throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rappInstance.getReason());
123 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
124 String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state",
125 rappInstance.getRappInstanceId()));
129 public ResponseEntity<String> undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
130 if (rappInstance.getState().equals(RappInstanceState.DEPLOYED)) {
131 rappInstance.setReason(null);
132 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING);
133 if (rappDeployers.parallelStream()
134 .allMatch(rappDeployer -> rappDeployer.undeployRappInstance(rapp, rappInstance))) {
135 return ResponseEntity.accepted().build();
137 throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rappInstance.getReason());
139 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
140 String.format("Unable to undeploy rApp instance %s as it is not in DEPLOYED state",
141 rappInstance.getRappInstanceId()));
144 public ResponseEntity<String> deleteRappInstance(Rapp rApp, UUID rappInstanceId) {
145 if (rApp.getRappInstances().get(rappInstanceId).getState().equals(RappInstanceState.UNDEPLOYED)) {
146 rappInstanceStateMachine.deleteRappInstance(rApp.getRappInstances().get(rappInstanceId));
147 rApp.getRappInstances().remove(rappInstanceId);
148 return ResponseEntity.noContent().build();
150 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
151 String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state", rappInstanceId));
154 public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
155 acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);