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.rapp.Rapp;
27 import com.oransc.rappmanager.models.rapp.RappEvent;
28 import com.oransc.rappmanager.models.rapp.RappState;
29 import com.oransc.rappmanager.models.rappinstance.RappInstance;
30 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
31 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
32 import java.util.List;
33 import java.util.UUID;
34 import lombok.RequiredArgsConstructor;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Service;
40 @RequiredArgsConstructor
41 public class RappService {
43 private final AcmDeployer acmDeployer;
44 private final List<RappDeployer> rappDeployers;
45 private final RappInstanceStateMachine rappInstanceStateMachine;
46 private final RappCacheService rappCacheService;
47 private final DeploymentArtifactsService deploymentArtifactsService;
48 private static final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
50 public ResponseEntity<String> primeRapp(Rapp rapp) {
51 if (rapp.getState().equals(RappState.COMMISSIONED)) {
52 rapp.setState(RappState.PRIMING);
54 //Configuring the deployment artifact needs to be done before starting the priming with other components
55 //If there are additional conditions needs to be checked before priming, This needs handled as part of pre-priming stage.
56 if (deploymentArtifactsService.configureDeploymentArtifacts(rapp) && rappDeployers.parallelStream()
58 rappDeployer -> rappDeployer.primeRapp(
60 rapp.setState(RappState.PRIMED);
61 return ResponseEntity.ok().build();
63 rapp.setState(RappState.COMMISSIONED);
64 throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rapp.getReason());
66 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
67 String.format(STATE_TRANSITION_NOT_PERMITTED, rapp.getState().name(), RappState.PRIMED.name()));
71 public ResponseEntity<String> deprimeRapp(Rapp rapp) {
72 if (rapp.getState().equals(RappState.PRIMED) && rapp.getRappInstances().isEmpty()) {
73 rapp.setState(RappState.DEPRIMING);
75 if (rappDeployers.parallelStream().allMatch(rappDeployer -> rappDeployer.deprimeRapp(rapp))) {
76 rapp.setState(RappState.COMMISSIONED);
77 return ResponseEntity.ok().build();
79 rapp.setState(RappState.PRIMED);
80 throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rapp.getReason());
82 if (!rapp.getRappInstances().isEmpty()) {
83 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
84 "Unable to deprime as there are active rapp instances.");
86 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
87 String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(),
88 rapp.getState().name()));
92 public ResponseEntity<String> deleteRapp(Rapp rApp) {
93 if (rApp.getRappInstances().isEmpty() && rApp.getState().equals(RappState.COMMISSIONED)) {
94 rappCacheService.deleteRapp(rApp);
95 return ResponseEntity.ok().build();
97 if (!rApp.getRappInstances().isEmpty()) {
98 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
99 String.format("Unable to delete %s as there are active rApp instances.", rApp.getName()));
101 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
102 String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rApp.getName()));
107 public ResponseEntity<String> deployRappInstance(Rapp rapp, RappInstance rappInstance) {
108 if (rappInstance.getState().equals(RappInstanceState.UNDEPLOYED)) {
109 rappInstance.setReason(null);
110 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING);
111 if (rappDeployers.parallelStream()
112 .allMatch(rappDeployer -> rappDeployer.deployRappInstance(rapp, rappInstance))) {
113 return ResponseEntity.accepted().build();
115 throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rappInstance.getReason());
117 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
118 String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state",
119 rappInstance.getRappInstanceId()));
123 public ResponseEntity<String> undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
124 if (rappInstance.getState().equals(RappInstanceState.DEPLOYED)) {
125 rappInstance.setReason(null);
126 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING);
127 if (rappDeployers.parallelStream()
128 .allMatch(rappDeployer -> rappDeployer.undeployRappInstance(rapp, rappInstance))) {
129 return ResponseEntity.accepted().build();
131 throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rappInstance.getReason());
133 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
134 String.format("Unable to undeploy rApp instance %s as it is not in DEPLOYED state",
135 rappInstance.getRappInstanceId()));
138 public ResponseEntity<String> deleteRappInstance(Rapp rApp, UUID rappInstanceId) {
139 if (rApp.getRappInstances().get(rappInstanceId).getState().equals(RappInstanceState.UNDEPLOYED)) {
140 rappInstanceStateMachine.deleteRappInstance(rApp.getRappInstances().get(rappInstanceId));
141 rApp.getRappInstances().remove(rappInstanceId);
142 return ResponseEntity.noContent().build();
144 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
145 String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state", rappInstanceId));
148 public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
149 acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);