6411b2b0672f8875a5bcea9af1d778a3c8950166
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / main / java / com / oransc / rappmanager / service / RappService.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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========================================================================
17  */
18
19 package com.oransc.rappmanager.service;
20
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;
33
34 @Service
35 @RequiredArgsConstructor
36 public class RappService {
37
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.";
42
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);
48             }
49             return ResponseEntity.ok().build();
50         } else {
51             return ResponseEntity.badRequest()
52                            .body(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.PRIMED.name(),
53                                    rapp.getState().name()));
54         }
55     }
56
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);
62             }
63             return ResponseEntity.ok().build();
64         } else {
65             if (!rapp.getRappInstances().isEmpty()) {
66                 return ResponseEntity.badRequest().body("Unable to deprime as there are active rapp instances,");
67             } else {
68                 return ResponseEntity.badRequest()
69                                .body(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(),
70                                        rapp.getState().name()));
71             }
72         }
73     }
74
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,
79                     rappInstance)) {
80                 return ResponseEntity.accepted().build();
81             }
82             return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
83         } else {
84             return ResponseEntity.badRequest()
85                            .body(String.format(STATE_TRANSITION_NOT_PERMITTED, rappInstance.getState().name(),
86                                    RappInstanceState.DEPLOYED.name()));
87         }
88     }
89
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,
94                     rappInstance)) {
95                 return ResponseEntity.accepted().build();
96             }
97             return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
98         } else {
99             return ResponseEntity.badRequest()
100                            .body(String.format(STATE_TRANSITION_NOT_PERMITTED, rappInstance.getState().name(),
101                                    RappInstanceState.UNDEPLOYED.name()));
102         }
103     }
104
105     public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
106         acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);
107     }
108 }