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.models.rapp.Rapp;
23 import com.oransc.rappmanager.models.rapp.RappEvent;
24 import com.oransc.rappmanager.models.rappinstance.RappInstance;
25 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
26 import com.oransc.rappmanager.models.rapp.RappState;
27 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
28 import com.oransc.rappmanager.sme.service.SmeDeployer;
29 import lombok.RequiredArgsConstructor;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.stereotype.Service;
35 @RequiredArgsConstructor
36 public class RappService {
38 private final AcmDeployer acmDeployer;
39 private final SmeDeployer smeDeployer;
40 private final RappInstanceStateMachine rappInstanceStateMachine;
42 public ResponseEntity<String> primeRapp(Rapp rapp) {
43 if (rapp.getState().equals(RappState.COMMISSIONED)) {
44 rapp.setState(RappState.PRIMING);
45 if (!acmDeployer.primeRapp(rapp)) {
46 rapp.setState(RappState.COMMISSIONED);
48 return ResponseEntity.ok().build();
50 return ResponseEntity.badRequest()
51 .body("State transition from " + RappState.PRIMED.name() + " to " + rapp.getState().name()
52 + " is not permitted.");
56 public ResponseEntity<String> deprimeRapp(Rapp rapp) {
57 if (rapp.getState().equals(RappState.PRIMED) && rapp.getRappInstances().isEmpty()) {
58 rapp.setState(RappState.DEPRIMING);
59 if (!acmDeployer.deprimeRapp(rapp)) {
60 rapp.setState(RappState.PRIMED);
62 return ResponseEntity.ok().build();
64 if (!rapp.getRappInstances().isEmpty()) {
65 return ResponseEntity.badRequest().body("Unable to deprime as there are active rapp instances,");
67 return ResponseEntity.badRequest()
68 .body("State transition from " + RappState.COMMISSIONED.name() + " to " + rapp.getState()
70 + " is not permitted.");
75 public ResponseEntity<String> deployRappInstance(Rapp rapp, RappInstance rappInstance) {
76 if (rappInstance.getState().equals(RappInstanceState.UNDEPLOYED)) {
77 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING);
78 if (acmDeployer.deployRappInstance(rapp, rappInstance) && smeDeployer.deployRappInstance(rapp,
80 return ResponseEntity.accepted().build();
82 return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
84 return ResponseEntity.badRequest().body("State transition from " + rappInstance.getState().name() + " to "
85 + RappInstanceState.DEPLOYED.name() + " is not permitted.");
89 public ResponseEntity<String> undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
90 if (rappInstance.getState().equals(RappInstanceState.DEPLOYED)) {
91 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING);
92 if (acmDeployer.undeployRappInstance(rapp, rappInstance) && smeDeployer.undeployRappInstance(rapp,
94 return ResponseEntity.accepted().build();
96 return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
98 return ResponseEntity.badRequest().body("State transition from " + rappInstance.getState().name() + " to "
99 + RappInstanceState.UNDEPLOYED.name()
100 + " is not permitted.");
104 public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
105 acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);