Kserve participant for simple rApp
[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-2024 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.models.RappDeployer;
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 java.util.List;
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 List<RappDeployer> rappDeployers;
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 (rappDeployers.parallelStream().allMatch(rappDeployer -> rappDeployer.primeRapp(rapp))) {
54                 rapp.setState(RappState.PRIMED);
55                 return ResponseEntity.ok().build();
56             }
57             rapp.setState(RappState.COMMISSIONED);
58             throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rapp.getReason());
59         }
60         throw new RappHandlerException(HttpStatus.BAD_REQUEST,
61                 String.format(STATE_TRANSITION_NOT_PERMITTED, rapp.getState().name(), RappState.PRIMED.name()));
62
63     }
64
65     public ResponseEntity<String> deprimeRapp(Rapp rapp) {
66         if (rapp.getState().equals(RappState.PRIMED) && rapp.getRappInstances().isEmpty()) {
67             rapp.setState(RappState.DEPRIMING);
68             rapp.setReason(null);
69             if (rappDeployers.parallelStream().allMatch(rappDeployer -> rappDeployer.deprimeRapp(rapp))) {
70                 rapp.setState(RappState.COMMISSIONED);
71                 return ResponseEntity.ok().build();
72             }
73             rapp.setState(RappState.PRIMED);
74             throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rapp.getReason());
75         }
76         if (!rapp.getRappInstances().isEmpty()) {
77             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
78                     "Unable to deprime as there are active rapp instances.");
79         } else {
80             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
81                     String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(),
82                             rapp.getState().name()));
83         }
84     }
85
86     public ResponseEntity<String> deleteRapp(Rapp rApp) {
87         if (rApp.getRappInstances().isEmpty() && rApp.getState().equals(RappState.COMMISSIONED)) {
88             rappCacheService.deleteRapp(rApp);
89             return ResponseEntity.ok().build();
90         }
91         if (!rApp.getRappInstances().isEmpty()) {
92             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
93                     String.format("Unable to delete %s as there are active rApp instances.", rApp.getName()));
94         } else {
95             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
96                     String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rApp.getName()));
97         }
98
99     }
100
101     public ResponseEntity<String> deployRappInstance(Rapp rapp, RappInstance rappInstance) {
102         if (rappInstance.getState().equals(RappInstanceState.UNDEPLOYED)) {
103             rappInstance.setReason(null);
104             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING);
105             if (rappDeployers.parallelStream()
106                         .allMatch(rappDeployer -> rappDeployer.deployRappInstance(rapp, rappInstance))) {
107                 return ResponseEntity.accepted().build();
108             }
109             throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rappInstance.getReason());
110         }
111         throw new RappHandlerException(HttpStatus.BAD_REQUEST,
112                 String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state",
113                         rappInstance.getRappInstanceId()));
114
115     }
116
117     public ResponseEntity<String> undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
118         if (rappInstance.getState().equals(RappInstanceState.DEPLOYED)) {
119             rappInstance.setReason(null);
120             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING);
121             if (rappDeployers.parallelStream()
122                         .allMatch(rappDeployer -> rappDeployer.undeployRappInstance(rapp, rappInstance))) {
123                 return ResponseEntity.accepted().build();
124             }
125             throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rappInstance.getReason());
126         }
127         throw new RappHandlerException(HttpStatus.BAD_REQUEST,
128                 String.format("Unable to undeploy rApp instance %s as it is not in DEPLOYED state",
129                         rappInstance.getRappInstanceId()));
130     }
131
132     public ResponseEntity<String> deleteRappInstance(Rapp rApp, UUID rappInstanceId) {
133         if (rApp.getRappInstances().get(rappInstanceId).getState().equals(RappInstanceState.UNDEPLOYED)) {
134             rappInstanceStateMachine.deleteRappInstance(rApp.getRappInstances().get(rappInstanceId));
135             rApp.getRappInstances().remove(rappInstanceId);
136             return ResponseEntity.noContent().build();
137         }
138         throw new RappHandlerException(HttpStatus.BAD_REQUEST,
139                 String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state", rappInstanceId));
140     }
141
142     public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
143         acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);
144     }
145 }