9fddd4dd1304cea1670f391d85ffa0dc9c972b45
[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  * Copyright (C) 2023 OpenInfra Foundation Europe. All rights reserved.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END========================================================================
18  */
19
20 package com.oransc.rappmanager.service;
21
22 import com.oransc.rappmanager.acm.service.AcmDeployer;
23 import com.oransc.rappmanager.dme.service.DmeDeployer;
24 import com.oransc.rappmanager.models.cache.RappCacheService;
25 import com.oransc.rappmanager.models.exception.RappHandlerException;
26 import com.oransc.rappmanager.models.rapp.Rapp;
27 import com.oransc.rappmanager.models.rapp.RappEvent;
28 import com.oransc.rappmanager.models.rapp.RappState;
29 import com.oransc.rappmanager.models.rappinstance.RappInstance;
30 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
31 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
32 import com.oransc.rappmanager.sme.service.SmeDeployer;
33 import java.util.UUID;
34 import lombok.RequiredArgsConstructor;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Service;
38
39 @Service
40 @RequiredArgsConstructor
41 public class RappService {
42
43     private final AcmDeployer acmDeployer;
44     private final SmeDeployer smeDeployer;
45     private final DmeDeployer dmeDeployer;
46     private final RappInstanceStateMachine rappInstanceStateMachine;
47     private final RappCacheService rappCacheService;
48     private static final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
49
50     public ResponseEntity<String> primeRapp(Rapp rapp) {
51         if (rapp.getState().equals(RappState.COMMISSIONED)) {
52             rapp.setState(RappState.PRIMING);
53             rapp.setReason(null);
54             if (acmDeployer.primeRapp(rapp) && dmeDeployer.primeRapp(rapp)) {
55                 rapp.setState(RappState.PRIMED);
56                 return ResponseEntity.ok().build();
57             }
58             rapp.setState(RappState.COMMISSIONED);
59             return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
60         }
61         throw new RappHandlerException(HttpStatus.BAD_REQUEST,
62                 String.format(STATE_TRANSITION_NOT_PERMITTED, rapp.getState().name(), RappState.PRIMED.name()));
63
64     }
65
66     public ResponseEntity<String> deprimeRapp(Rapp rapp) {
67         if (rapp.getState().equals(RappState.PRIMED) && rapp.getRappInstances().isEmpty()) {
68             rapp.setState(RappState.DEPRIMING);
69             rapp.setReason(null);
70             if (acmDeployer.deprimeRapp(rapp) && dmeDeployer.deprimeRapp(rapp)) {
71                 rapp.setState(RappState.COMMISSIONED);
72                 return ResponseEntity.ok().build();
73             }
74             rapp.setState(RappState.PRIMED);
75             return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
76         }
77         if (!rapp.getRappInstances().isEmpty()) {
78             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
79                     "Unable to deprime as there are active rapp instances.");
80         } else {
81             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
82                     String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(),
83                             rapp.getState().name()));
84         }
85     }
86
87     public ResponseEntity<String> deleteRapp(Rapp rApp) {
88         if (rApp.getRappInstances().isEmpty() && rApp.getState().equals(RappState.COMMISSIONED)) {
89             rappCacheService.deleteRapp(rApp);
90             return ResponseEntity.ok().build();
91         }
92         if (!rApp.getRappInstances().isEmpty()) {
93             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
94                     String.format("Unable to delete %s as there are active rApp instances.", rApp.getName()));
95         } else {
96             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
97                     String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rApp.getName()));
98         }
99
100     }
101
102     public ResponseEntity<String> deployRappInstance(Rapp rapp, RappInstance rappInstance) {
103         if (rappInstance.getState().equals(RappInstanceState.UNDEPLOYED)) {
104             rappInstance.setReason(null);
105             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING);
106             if (acmDeployer.deployRappInstance(rapp, rappInstance) && smeDeployer.deployRappInstance(rapp,
107                     rappInstance)) {
108                 return ResponseEntity.accepted().build();
109             }
110             return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
111         }
112         throw new RappHandlerException(HttpStatus.BAD_REQUEST,
113                 String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state",
114                         rappInstance.getRappInstanceId()));
115
116     }
117
118     public ResponseEntity<String> undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
119         if (rappInstance.getState().equals(RappInstanceState.DEPLOYED)) {
120             rappInstance.setReason(null);
121             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING);
122             if (acmDeployer.undeployRappInstance(rapp, rappInstance) && smeDeployer.undeployRappInstance(rapp,
123                     rappInstance)) {
124                 return ResponseEntity.accepted().build();
125             }
126             return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
127         }
128         throw new RappHandlerException(HttpStatus.BAD_REQUEST,
129                 String.format("Unable to undeploy rApp instance %s as it is not in DEPLOYED state",
130                         rappInstance.getRappInstanceId()));
131     }
132
133     public ResponseEntity<String> deleteRappInstance(Rapp rApp, UUID rappInstanceId) {
134         if (rApp.getRappInstances().get(rappInstanceId).getState().equals(RappInstanceState.UNDEPLOYED)) {
135             rappInstanceStateMachine.deleteRappInstance(rApp.getRappInstances().get(rappInstanceId));
136             rApp.getRappInstances().remove(rappInstanceId);
137             return ResponseEntity.noContent().build();
138         }
139         throw new RappHandlerException(HttpStatus.BAD_REQUEST,
140                 String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state", rappInstanceId));
141     }
142
143     public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
144         acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);
145     }
146 }