Add Rapp Instances
[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  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  */
18
19 package com.oransc.rappmanager.models.statemachine;
20
21 import com.oransc.rappmanager.models.rapp.RappEvent;
22 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
23 import java.util.EnumSet;
24 import org.springframework.context.annotation.Bean;
25 import org.springframework.context.annotation.Configuration;
26 import org.springframework.statemachine.config.EnableStateMachineFactory;
27 import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
28 import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
29 import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
30 import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
31 import org.springframework.statemachine.guard.Guard;
32
33 @Configuration
34 @EnableStateMachineFactory
35 public class RappInstanceStateMachineConfig extends EnumStateMachineConfigurerAdapter<RappInstanceState, RappEvent> {
36
37     @Override
38     public void configure(StateMachineStateConfigurer<RappInstanceState, RappEvent> states) throws Exception {
39         states.withStates().initial(RappInstanceState.UNDEPLOYED).states(EnumSet.allOf(RappInstanceState.class));
40     }
41
42     @Override
43     public void configure(StateMachineConfigurationConfigurer<RappInstanceState, RappEvent> config) throws Exception {
44         config.withConfiguration();
45     }
46
47     // @formatter:off
48     @Override
49     public void configure(StateMachineTransitionConfigurer<RappInstanceState, RappEvent> transitions) throws Exception {
50         transitions
51                 .withExternal()
52                     .source(RappInstanceState.UNDEPLOYED).target(RappInstanceState.DEPLOYING).event(RappEvent.DEPLOYING)
53                     .and()
54                 .withExternal()
55                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMDEPLOYFAILED)
56                     .and()
57                 .withExternal()
58                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEDEPLOYFAILED)
59                     .and()
60                 .withExternal()
61                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMUNDEPLOYFAILED)
62                     .and()
63                 .withExternal()
64                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEUNDEPLOYFAILED)
65                     .and()
66                 .withExternal()
67                     .source(RappInstanceState.DEPLOYED).target(RappInstanceState.UNDEPLOYING).event(RappEvent.UNDEPLOYING)
68                     .and()
69                 .withExternal()
70                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMDEPLOYED)
71                     .guard(deployedGuard())
72                     .and()
73                 .withExternal()
74                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEDEPLOYED)
75                     .guard(deployedGuard())
76                     .and()
77                 .withExternal()
78                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMUNDEPLOYED)
79                     .guard(undeployedGuard())
80                     .and()
81                 .withExternal()
82                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEUNDEPLOYED)
83                     .guard(undeployedGuard());
84
85     }
86     // @formatter:on
87
88     @Bean
89     public Guard<RappInstanceState, RappEvent> deployedGuard() {
90         return stateContext -> {
91             stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
92             return stateContext.getExtendedState().getVariables().get(RappEvent.ACMDEPLOYED) != null
93                            && stateContext.getExtendedState().getVariables().get(RappEvent.SMEDEPLOYED) != null;
94         };
95     }
96
97     @Bean
98     public Guard<RappInstanceState, RappEvent> undeployedGuard() {
99         return stateContext -> {
100             stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
101             return stateContext.getExtendedState().getVariables().get(RappEvent.ACMUNDEPLOYED) != null
102                            && stateContext.getExtendedState().getVariables().get(RappEvent.SMEUNDEPLOYED) != null;
103         };
104     }
105 }