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.service;
21 import com.oransc.rappmanager.acm.service.AcmDeployer;
22 import com.oransc.rappmanager.dme.service.DmeDeployer;
23 import com.oransc.rappmanager.models.cache.RappCacheService;
24 import com.oransc.rappmanager.models.exception.RappHandlerException;
25 import com.oransc.rappmanager.models.rapp.Rapp;
26 import com.oransc.rappmanager.models.rapp.RappEvent;
27 import com.oransc.rappmanager.models.rapp.RappState;
28 import com.oransc.rappmanager.models.rappinstance.RappInstance;
29 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
30 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
31 import com.oransc.rappmanager.sme.service.SmeDeployer;
32 import java.util.UUID;
33 import lombok.RequiredArgsConstructor;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
39 @RequiredArgsConstructor
40 public class RappService {
42 private final AcmDeployer acmDeployer;
43 private final SmeDeployer smeDeployer;
44 private final DmeDeployer dmeDeployer;
45 private final RappInstanceStateMachine rappInstanceStateMachine;
46 private final RappCacheService rappCacheService;
47 private static final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
49 public ResponseEntity<String> primeRapp(Rapp rapp) {
50 if (rapp.getState().equals(RappState.COMMISSIONED)) {
51 rapp.setState(RappState.PRIMING);
53 if (acmDeployer.primeRapp(rapp) && dmeDeployer.primeRapp(rapp)) {
54 rapp.setState(RappState.PRIMED);
55 return ResponseEntity.ok().build();
57 rapp.setState(RappState.COMMISSIONED);
58 return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
60 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
61 String.format(STATE_TRANSITION_NOT_PERMITTED, rapp.getState().name(), RappState.PRIMED.name()));
65 public ResponseEntity<String> deprimeRapp(Rapp rapp) {
66 if (rapp.getState().equals(RappState.PRIMED) && rapp.getRappInstances().isEmpty()) {
67 rapp.setState(RappState.DEPRIMING);
69 if (acmDeployer.deprimeRapp(rapp) && dmeDeployer.deprimeRapp(rapp)) {
70 rapp.setState(RappState.COMMISSIONED);
71 return ResponseEntity.ok().build();
73 rapp.setState(RappState.PRIMED);
74 return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
76 if (!rapp.getRappInstances().isEmpty()) {
77 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
78 "Unable to deprime as there are active rapp instances.");
80 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
81 String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(),
82 rapp.getState().name()));
86 public ResponseEntity<String> deleteRapp(Rapp rApp) {
87 if (rApp.getRappInstances().isEmpty() && rApp.getState().equals(RappState.COMMISSIONED)) {
88 rappCacheService.deleteRapp(rApp);
89 return ResponseEntity.ok().build();
91 if (!rApp.getRappInstances().isEmpty()) {
92 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
93 String.format("Unable to delete %s as there are active rApp instances.", rApp.getName()));
95 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
96 String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rApp.getName()));
101 public ResponseEntity<String> deployRappInstance(Rapp rapp, RappInstance rappInstance) {
102 if (rappInstance.getState().equals(RappInstanceState.UNDEPLOYED)) {
103 rappInstance.setReason(null);
104 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING);
105 if (acmDeployer.deployRappInstance(rapp, rappInstance) && smeDeployer.deployRappInstance(rapp, rappInstance)
106 && dmeDeployer.deployRappInstance(rapp, rappInstance)) {
107 return ResponseEntity.accepted().build();
109 return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
111 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
112 String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state",
113 rappInstance.getRappInstanceId()));
117 public ResponseEntity<String> undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
118 if (rappInstance.getState().equals(RappInstanceState.DEPLOYED)) {
119 rappInstance.setReason(null);
120 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING);
121 if (acmDeployer.undeployRappInstance(rapp, rappInstance) && smeDeployer.undeployRappInstance(rapp,
122 rappInstance) && dmeDeployer.undeployRappInstance(rapp, rappInstance)) {
123 return ResponseEntity.accepted().build();
125 return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
127 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
128 String.format("Unable to undeploy rApp instance %s as it is not in DEPLOYED state",
129 rappInstance.getRappInstanceId()));
132 public ResponseEntity<String> deleteRappInstance(Rapp rApp, UUID rappInstanceId) {
133 if (rApp.getRappInstances().get(rappInstanceId).getState().equals(RappInstanceState.UNDEPLOYED)) {
134 rappInstanceStateMachine.deleteRappInstance(rApp.getRappInstances().get(rappInstanceId));
135 rApp.getRappInstances().remove(rappInstanceId);
136 return ResponseEntity.noContent().build();
138 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
139 String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state", rappInstanceId));
142 public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
143 acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);