Merge "Remove workarounds in ACM installation"
[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.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.DMEDEPLOYFAILED)
62                     .and()
63                 .withExternal()
64                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMUNDEPLOYFAILED)
65                     .and()
66                 .withExternal()
67                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEUNDEPLOYFAILED)
68                     .and()
69                 .withExternal()
70                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.DMEUNDEPLOYFAILED)
71                     .and()
72                 .withExternal()
73                     .source(RappInstanceState.DEPLOYED).target(RappInstanceState.UNDEPLOYING).event(RappEvent.UNDEPLOYING)
74                     .and()
75                 .withExternal()
76                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMDEPLOYED)
77                     .guard(deployedGuard())
78                     .and()
79                 .withExternal()
80                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEDEPLOYED)
81                     .guard(deployedGuard())
82                     .and()
83                 .withExternal()
84                     .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.DMEDEPLOYED)
85                     .guard(deployedGuard())
86                     .and()
87                 .withExternal()
88                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMUNDEPLOYED)
89                     .guard(undeployedGuard())
90                     .and()
91                 .withExternal()
92                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEUNDEPLOYED)
93                     .guard(undeployedGuard())
94                     .and()
95                 .withExternal()
96                     .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.DMEUNDEPLOYED)
97                     .guard(undeployedGuard());
98
99     }
100     // @formatter:on
101
102     @Bean
103     public Guard<RappInstanceState, RappEvent> deployedGuard() {
104         return stateContext -> {
105             stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
106             return stateContext.getExtendedState().getVariables().get(RappEvent.ACMDEPLOYED) != null
107                            && stateContext.getExtendedState().getVariables().get(RappEvent.SMEDEPLOYED) != null
108                            && stateContext.getExtendedState().getVariables().get(RappEvent.DMEDEPLOYED) != null;
109         };
110     }
111
112     @Bean
113     public Guard<RappInstanceState, RappEvent> undeployedGuard() {
114         return stateContext -> {
115             stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
116             return stateContext.getExtendedState().getVariables().get(RappEvent.ACMUNDEPLOYED) != null
117                            && stateContext.getExtendedState().getVariables().get(RappEvent.SMEUNDEPLOYED) != null
118                            && stateContext.getExtendedState().getVariables().get(RappEvent.DMEUNDEPLOYED) != null;
119         };
120     }
121 }