Provide proper response message/code in delete rapp instance
[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.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;
37
38 @Service
39 @RequiredArgsConstructor
40 public class RappService {
41
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.";
48
49     public ResponseEntity<String> primeRapp(Rapp rapp) {
50         if (rapp.getState().equals(RappState.COMMISSIONED)) {
51             rapp.setState(RappState.PRIMING);
52             rapp.setReason(null);
53             if (acmDeployer.primeRapp(rapp) && dmeDeployer.primeRapp(rapp)) {
54                 rapp.setState(RappState.PRIMED);
55                 return ResponseEntity.ok().build();
56             } else {
57                 rapp.setState(RappState.COMMISSIONED);
58                 return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
59             }
60         } else {
61             return ResponseEntity.badRequest()
62                            .body(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.PRIMED.name(),
63                                    rapp.getState().name()));
64         }
65     }
66
67     public ResponseEntity<String> deprimeRapp(Rapp rapp) {
68         if (rapp.getState().equals(RappState.PRIMED) && rapp.getRappInstances().isEmpty()) {
69             rapp.setState(RappState.DEPRIMING);
70             rapp.setReason(null);
71             if (acmDeployer.deprimeRapp(rapp) && dmeDeployer.deprimeRapp(rapp)) {
72                 rapp.setState(RappState.COMMISSIONED);
73                 return ResponseEntity.ok().build();
74             } else {
75                 rapp.setState(RappState.PRIMED);
76                 return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
77             }
78         } else {
79             if (!rapp.getRappInstances().isEmpty()) {
80                 return ResponseEntity.badRequest().body("Unable to deprime as there are active rapp instances,");
81             } else {
82                 return ResponseEntity.badRequest()
83                                .body(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(),
84                                        rapp.getState().name()));
85             }
86         }
87     }
88
89     public ResponseEntity<String> deleteRapp(Rapp rApp) {
90         if (rApp.getRappInstances().isEmpty() && rApp.getState().equals(RappState.COMMISSIONED)) {
91             rappCacheService.deleteRapp(rApp);
92             return ResponseEntity.ok().build();
93         } else {
94             if (!rApp.getRappInstances().isEmpty()) {
95                 return ResponseEntity.badRequest()
96                                .body("Unable to delete '" + rApp.getName() + "' as there are active rApp instances.");
97             } else {
98                 return ResponseEntity.badRequest().body("Unable to delete '" + rApp.getName()
99                                                                 + "' as the rApp is not in COMMISSIONED state.");
100             }
101         }
102     }
103
104     public ResponseEntity<String> deployRappInstance(Rapp rapp, RappInstance rappInstance) {
105         if (rappInstance.getState().equals(RappInstanceState.UNDEPLOYED)) {
106             rappInstance.setReason(null);
107             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING);
108             if (acmDeployer.deployRappInstance(rapp, rappInstance) && smeDeployer.deployRappInstance(rapp, rappInstance)
109                         && dmeDeployer.deployRappInstance(rapp, rappInstance)) {
110                 return ResponseEntity.accepted().build();
111             }
112             return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
113         } else {
114             return ResponseEntity.badRequest()
115                            .body(String.format(STATE_TRANSITION_NOT_PERMITTED, rappInstance.getState().name(),
116                                    RappInstanceState.DEPLOYED.name()));
117         }
118     }
119
120     public ResponseEntity<String> undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
121         if (rappInstance.getState().equals(RappInstanceState.DEPLOYED)) {
122             rappInstance.setReason(null);
123             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING);
124             if (acmDeployer.undeployRappInstance(rapp, rappInstance) && smeDeployer.undeployRappInstance(rapp,
125                     rappInstance) && dmeDeployer.undeployRappInstance(rapp, rappInstance)) {
126                 return ResponseEntity.accepted().build();
127             }
128             return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
129         } else {
130             return ResponseEntity.badRequest()
131                            .body(String.format(STATE_TRANSITION_NOT_PERMITTED, rappInstance.getState().name(),
132                                    RappInstanceState.UNDEPLOYED.name()));
133         }
134     }
135
136     public ResponseEntity<Object> deleteRappInstance(Rapp rApp, UUID rappInstanceId) {
137         if (rApp.getRappInstances().get(rappInstanceId).getState().equals(RappInstanceState.UNDEPLOYED)) {
138             rappInstanceStateMachine.deleteRappInstance(rApp.getRappInstances().get(rappInstanceId));
139             rApp.getRappInstances().remove(rappInstanceId);
140             return ResponseEntity.noContent().build();
141         }
142         throw new RappHandlerException(HttpStatus.BAD_REQUEST,
143                 String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state", rappInstanceId));
144     }
145
146     public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
147         acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);
148     }
149 }