Merge "Add installation mode to use snapshot image"
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / main / java / com / oransc / rappmanager / models / statemachine / RappInstanceStateMachineConfig.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * Copyright (C) 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.models.statemachine;
21
22 import com.oransc.rappmanager.models.rapp.RappEvent;
23 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
24 import java.util.EnumSet;
25 import org.springframework.context.annotation.Bean;
26 import org.springframework.context.annotation.Configuration;
27 import org.springframework.statemachine.StateContext;
28 import org.springframework.statemachine.config.EnableStateMachineFactory;
29 import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
30 import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
31 import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
32 import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
33 import org.springframework.statemachine.guard.Guard;
34
35 @Configuration
36 @EnableStateMachineFactory
37 public class RappInstanceStateMachineConfig extends EnumStateMachineConfigurerAdapter<RappInstanceState, RappEvent> {
38
39     @Override
40     public void configure(StateMachineStateConfigurer<RappInstanceState, RappEvent> states) throws Exception {
41         states.withStates().initial(RappInstanceState.UNDEPLOYED).states(EnumSet.allOf(RappInstanceState.class));
42     }
43
44     @Override
45     public void configure(StateMachineConfigurationConfigurer<RappInstanceState, RappEvent> config) throws Exception {
46         config.withConfiguration();
47     }
48
49     // @formatter:off
50     @Override
51     public void configure(StateMachineTransitionConfigurer<RappInstanceState, RappEvent> transitions) throws Exception {
52         transitions
53                 .withExternal()
54                     .source(RappInstanceState.UNDEPLOYED).target(RappInstanceState.DEPLOYING).event(RappEvent.DEPLOYING)
55                     .and()
56                 .withExternal()
57                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMDEPLOYFAILED)
58                     .and()
59                 .withExternal()
60                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEDEPLOYFAILED)
61                     .and()
62                 .withExternal()
63                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.DMEDEPLOYFAILED)
64                     .and()
65                 .withExternal()
66                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMUNDEPLOYFAILED)
67                     .and()
68                 .withExternal()
69                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEUNDEPLOYFAILED)
70                     .and()
71                 .withExternal()
72                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.DMEUNDEPLOYFAILED)
73                     .and()
74                 .withExternal()
75                     .source(RappInstanceState.DEPLOYED).target(RappInstanceState.UNDEPLOYING).event(RappEvent.UNDEPLOYING)
76                     .and()
77                 .withExternal()
78                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMDEPLOYED)
79                     .guard(deployedGuard())
80                     .and()
81                 .withExternal()
82                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEDEPLOYED)
83                     .guard(deployedGuard())
84                     .and()
85                 .withExternal()
86                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.DMEDEPLOYED)
87                     .guard(deployedGuard())
88                     .and()
89                 .withExternal()
90                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMUNDEPLOYED)
91                     .guard(undeployedGuard())
92                     .and()
93                 .withExternal()
94                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEUNDEPLOYED)
95                     .guard(undeployedGuard())
96                     .and()
97                 .withExternal()
98                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.DMEUNDEPLOYED)
99                     .guard(undeployedGuard());
100
101     }
102     // @formatter:on
103
104     @Bean
105     public Guard<RappInstanceState, RappEvent> deployedGuard() {
106         return stateContext -> {
107             stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
108             return stateContext.getExtendedState().getVariables().get(RappEvent.ACMDEPLOYED) != null && smeGuard(
109                     stateContext, RappEvent.SMEDEPLOYED) && dmeGuard(stateContext, RappEvent.DMEDEPLOYED);
110         };
111     }
112
113     boolean smeGuard(StateContext<RappInstanceState, RappEvent> stateContext, RappEvent rappEvent) {
114         Boolean smeEnabled = (Boolean) stateContext.getExtendedState().getVariables().get("sme");
115         if (smeEnabled != null && smeEnabled.equals(Boolean.TRUE)) {
116             return stateContext.getExtendedState().getVariables().get(rappEvent) != null;
117         }
118         return true;
119     }
120
121     boolean dmeGuard(StateContext<RappInstanceState, RappEvent> stateContext, RappEvent rappEvent) {
122         Boolean dmeEnabled = (Boolean) stateContext.getExtendedState().getVariables().get("dme");
123         if (dmeEnabled != null && dmeEnabled.equals(Boolean.TRUE)) {
124             return stateContext.getExtendedState().getVariables().get(rappEvent) != null;
125         }
126         return true;
127     }
128
129     @Bean
130     public Guard<RappInstanceState, RappEvent> undeployedGuard() {
131         return stateContext -> {
132             stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
133             return stateContext.getExtendedState().getVariables().get(RappEvent.ACMUNDEPLOYED) != null && smeGuard(
134                     stateContext, RappEvent.SMEUNDEPLOYED) && dmeGuard(stateContext, RappEvent.DMEUNDEPLOYED);
135         };
136     }
137 }