4d7e486beba4be35b98dfa3a07e2a2d684e78f4b
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / main / java / com / oransc / rappmanager / models / statemachine / RappStateMachineConfig.java
1 package com.oransc.rappmanager.models.statemachine;
2
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;
14
15 @Configuration
16 @EnableStateMachineFactory
17 public class RappStateMachineConfig extends EnumStateMachineConfigurerAdapter<RappState, RappEvent> {
18
19     @Override
20     public void configure(StateMachineStateConfigurer<RappState, RappEvent> states) throws Exception {
21         states.withStates().initial(RappState.ONBOARDED).states(EnumSet.allOf(RappState.class));
22     }
23
24     @Override
25     public void configure(StateMachineConfigurationConfigurer<RappState, RappEvent> config) throws Exception {
26         config.withConfiguration();
27     }
28
29     // @formatter:off
30     @Override
31     public void configure(StateMachineTransitionConfigurer<RappState, RappEvent> transitions) throws Exception {
32         transitions
33                 .withExternal()
34                     .source(RappState.ONBOARDED).target(RappState.DEPLOYING).event(RappEvent.DEPLOYING)
35                     .and()
36                 .withExternal()
37                     .source(RappState.DEPLOYING).target(RappState.FAILED).event(RappEvent.ACMDEPLOYFAILED)
38                     .and()
39                 .withExternal()
40                     .source(RappState.DEPLOYING).target(RappState.FAILED).event(RappEvent.SMEDEPLOYFAILED)
41                     .and()
42                 .withExternal()
43                     .source(RappState.UNDEPLOYING).target(RappState.FAILED).event(RappEvent.ACMUNDEPLOYFAILED)
44                     .and()
45                 .withExternal()
46                     .source(RappState.UNDEPLOYING).target(RappState.FAILED).event(RappEvent.SMEUNDEPLOYFAILED)
47                     .and()
48                 .withExternal()
49                     .source(RappState.DEPLOYED).target(RappState.UNDEPLOYING).event(RappEvent.UNDEPLOYING)
50                     .and()
51                 .withExternal()
52                     .source(RappState.DEPLOYING).target(RappState.DEPLOYED).event(RappEvent.ACMDEPLOYED)
53                     .guard(deployedGuard())
54                     .and()
55                 .withExternal()
56                     .source(RappState.DEPLOYING).target(RappState.DEPLOYED).event(RappEvent.SMEDEPLOYED)
57                     .guard(deployedGuard())
58                     .and()
59                 .withExternal()
60                     .source(RappState.UNDEPLOYING).target(RappState.UNDEPLOYED).event(RappEvent.ACMUNDEPLOYED)
61                     .guard(undeployedGuard())
62                     .and()
63                 .withExternal()
64                     .source(RappState.UNDEPLOYING).target(RappState.UNDEPLOYED).event(RappEvent.SMEUNDEPLOYED)
65                     .guard(undeployedGuard());
66
67     }
68     // @formatter:on
69
70     @Bean
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;
76         };
77     }
78
79     @Bean
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;
85         };
86     }
87 }