1 package com.oransc.rappmanager.models.statemachine;
3 import com.oransc.rappmanager.models.RappEvent;
4 import com.oransc.rappmanager.models.RappState;
5 import java.util.EnumSet;
6 import org.springframework.context.annotation.Bean;
7 import org.springframework.context.annotation.Configuration;
8 import org.springframework.statemachine.config.EnableStateMachineFactory;
9 import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
10 import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
11 import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
12 import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
13 import org.springframework.statemachine.guard.Guard;
16 @EnableStateMachineFactory
17 public class RappStateMachineConfig extends EnumStateMachineConfigurerAdapter<RappState, RappEvent> {
20 public void configure(StateMachineStateConfigurer<RappState, RappEvent> states) throws Exception {
21 states.withStates().initial(RappState.ONBOARDED).states(EnumSet.allOf(RappState.class));
25 public void configure(StateMachineConfigurationConfigurer<RappState, RappEvent> config) throws Exception {
26 config.withConfiguration();
31 public void configure(StateMachineTransitionConfigurer<RappState, RappEvent> transitions) throws Exception {
34 .source(RappState.ONBOARDED).target(RappState.DEPLOYING).event(RappEvent.DEPLOYING)
37 .source(RappState.DEPLOYING).target(RappState.FAILED).event(RappEvent.ACMDEPLOYFAILED)
40 .source(RappState.DEPLOYING).target(RappState.FAILED).event(RappEvent.SMEDEPLOYFAILED)
43 .source(RappState.UNDEPLOYING).target(RappState.FAILED).event(RappEvent.ACMUNDEPLOYFAILED)
46 .source(RappState.UNDEPLOYING).target(RappState.FAILED).event(RappEvent.SMEUNDEPLOYFAILED)
49 .source(RappState.DEPLOYED).target(RappState.UNDEPLOYING).event(RappEvent.UNDEPLOYING)
52 .source(RappState.DEPLOYING).target(RappState.DEPLOYED).event(RappEvent.ACMDEPLOYED)
53 .guard(deployedGuard())
56 .source(RappState.DEPLOYING).target(RappState.DEPLOYED).event(RappEvent.SMEDEPLOYED)
57 .guard(deployedGuard())
60 .source(RappState.UNDEPLOYING).target(RappState.UNDEPLOYED).event(RappEvent.ACMUNDEPLOYED)
61 .guard(undeployedGuard())
64 .source(RappState.UNDEPLOYING).target(RappState.UNDEPLOYED).event(RappEvent.SMEUNDEPLOYED)
65 .guard(undeployedGuard());
71 public Guard<RappState, RappEvent> deployedGuard() {
72 return stateContext -> {
73 stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
74 return stateContext.getExtendedState().getVariables().get(RappEvent.ACMDEPLOYED) != null
75 && stateContext.getExtendedState().getVariables().get(RappEvent.SMEDEPLOYED) != null;
80 public Guard<RappState, RappEvent> undeployedGuard() {
81 return stateContext -> {
82 stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
83 return stateContext.getExtendedState().getVariables().get(RappEvent.ACMUNDEPLOYED) != null
84 && stateContext.getExtendedState().getVariables().get(RappEvent.SMEUNDEPLOYED) != null;