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.rapp.RappState;
25 import com.oransc.rappmanager.models.rappinstance.RappInstance;
26 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
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;
41 private static final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
43 public ResponseEntity<String> primeRapp(Rapp rapp) {
44 if (rapp.getState().equals(RappState.COMMISSIONED)) {
45 rapp.setState(RappState.PRIMING);
46 if (!acmDeployer.primeRapp(rapp)) {
47 rapp.setState(RappState.COMMISSIONED);
49 return ResponseEntity.ok().build();
51 return ResponseEntity.badRequest()
52 .body(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.PRIMED.name(),
53 rapp.getState().name()));
57 public ResponseEntity<String> deprimeRapp(Rapp rapp) {
58 if (rapp.getState().equals(RappState.PRIMED) && rapp.getRappInstances().isEmpty()) {
59 rapp.setState(RappState.DEPRIMING);
60 if (!acmDeployer.deprimeRapp(rapp)) {
61 rapp.setState(RappState.PRIMED);
63 return ResponseEntity.ok().build();
65 if (!rapp.getRappInstances().isEmpty()) {
66 return ResponseEntity.badRequest().body("Unable to deprime as there are active rapp instances,");
68 return ResponseEntity.badRequest()
69 .body(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(),
70 rapp.getState().name()));
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()
85 .body(String.format(STATE_TRANSITION_NOT_PERMITTED, rappInstance.getState().name(),
86 RappInstanceState.DEPLOYED.name()));
90 public ResponseEntity<String> undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
91 if (rappInstance.getState().equals(RappInstanceState.DEPLOYED)) {
92 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING);
93 if (acmDeployer.undeployRappInstance(rapp, rappInstance) && smeDeployer.undeployRappInstance(rapp,
95 return ResponseEntity.accepted().build();
97 return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
99 return ResponseEntity.badRequest()
100 .body(String.format(STATE_TRANSITION_NOT_PERMITTED, rappInstance.getState().name(),
101 RappInstanceState.UNDEPLOYED.name()));
105 public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
106 acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);