Add Rapp Instances
[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.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;
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
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);
47             }
48             return ResponseEntity.ok().build();
49         } else {
50             return ResponseEntity.badRequest()
51                            .body("State transition from " + RappState.PRIMED.name() + " to " + rapp.getState().name()
52                                          + " is not permitted.");
53         }
54     }
55
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);
61             }
62             return ResponseEntity.ok().build();
63         } else {
64             if (!rapp.getRappInstances().isEmpty()) {
65                 return ResponseEntity.badRequest().body("Unable to deprime as there are active rapp instances,");
66             } else {
67                 return ResponseEntity.badRequest()
68                                .body("State transition from " + RappState.COMMISSIONED.name() + " to " + rapp.getState()
69                                                                                                                  .name()
70                                              + " is not permitted.");
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().body("State transition from " + rappInstance.getState().name() + " to "
85                                                             + RappInstanceState.DEPLOYED.name() + " is not permitted.");
86         }
87     }
88
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,
93                     rappInstance)) {
94                 return ResponseEntity.accepted().build();
95             }
96             return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
97         } else {
98             return ResponseEntity.badRequest().body("State transition from " + rappInstance.getState().name() + " to "
99                                                             + RappInstanceState.UNDEPLOYED.name()
100                                                             + " is not permitted.");
101         }
102     }
103
104     public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
105         acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);
106     }
107 }