forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathResourceState.java
More file actions
116 lines (100 loc) · 5.54 KB
/
ResourceState.java
File metadata and controls
116 lines (100 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.resource;
import java.util.List;
import java.util.Set;
import com.cloud.utils.fsm.StateMachine;
public enum ResourceState {
Creating, Enabled, Disabled, PrepareForMaintenance, ErrorInMaintenance, Maintenance, Error;
public enum Event {
InternalCreated("Resource is created"),
Enable("Admin enables"),
Disable("Admin disables"),
AdminAskMaintenace("Admin asks to enter maintenance"),
AdminCancelMaintenance("Admin asks to cancel maintenance"),
InternalEnterMaintenance("Resource enters maintenance"),
UpdatePassword("Admin updates password of host"),
UnableToMigrate("Management server migrates VM failed"),
Error("An internal error happened"),
DeleteHost("Admin delete a host"),
/*
* Below events don't cause resource state to change, they are merely
* for ClusterManager propagating event from one mgmt server to another
*/
Unmanaged("Umanage a cluster");
private final String comment;
private Event(String comment) {
this.comment = comment;
}
public String getDescription() {
return this.comment;
}
public static Event toEvent(String e) {
if (Enable.toString().equals(e)) {
return Enable;
} else if (Disable.toString().equals(e)) {
return Disable;
}
return null;
}
}
public ResourceState getNextState(Event a) {
return s_fsm.getNextState(this, a);
}
public ResourceState[] getFromStates(Event a) {
List<ResourceState> from = s_fsm.getFromStates(this, a);
return from.toArray(new ResourceState[from.size()]);
}
public Set<Event> getPossibleEvents() {
return s_fsm.getPossibleEvents(this);
}
public static String[] toString(ResourceState... states) {
String[] strs = new String[states.length];
for (int i = 0; i < states.length; i++) {
strs[i] = states[i].toString();
}
return strs;
}
protected static final StateMachine<ResourceState, Event> s_fsm = new StateMachine<ResourceState, Event>();
static {
s_fsm.addTransition(null, Event.InternalCreated, ResourceState.Enabled);
s_fsm.addTransition(ResourceState.Creating, Event.InternalCreated, ResourceState.Enabled);
s_fsm.addTransition(ResourceState.Creating, Event.Error, ResourceState.Error);
s_fsm.addTransition(ResourceState.Enabled, Event.Enable, ResourceState.Enabled);
s_fsm.addTransition(ResourceState.Enabled, Event.InternalCreated, ResourceState.Enabled);
s_fsm.addTransition(ResourceState.Enabled, Event.Disable, ResourceState.Disabled);
s_fsm.addTransition(ResourceState.Enabled, Event.AdminAskMaintenace, ResourceState.PrepareForMaintenance);
s_fsm.addTransition(ResourceState.Enabled, Event.InternalEnterMaintenance, ResourceState.Maintenance);
s_fsm.addTransition(ResourceState.Disabled, Event.Enable, ResourceState.Enabled);
s_fsm.addTransition(ResourceState.Disabled, Event.Disable, ResourceState.Disabled);
s_fsm.addTransition(ResourceState.Disabled, Event.InternalCreated, ResourceState.Disabled);
s_fsm.addTransition(ResourceState.PrepareForMaintenance, Event.InternalEnterMaintenance, ResourceState.Maintenance);
s_fsm.addTransition(ResourceState.PrepareForMaintenance, Event.AdminCancelMaintenance, ResourceState.Enabled);
s_fsm.addTransition(ResourceState.PrepareForMaintenance, Event.UnableToMigrate, ResourceState.ErrorInMaintenance);
s_fsm.addTransition(ResourceState.PrepareForMaintenance, Event.InternalCreated, ResourceState.PrepareForMaintenance);
s_fsm.addTransition(ResourceState.Maintenance, Event.AdminCancelMaintenance, ResourceState.Enabled);
s_fsm.addTransition(ResourceState.Maintenance, Event.InternalCreated, ResourceState.Maintenance);
s_fsm.addTransition(ResourceState.Maintenance, Event.DeleteHost, ResourceState.Disabled);
s_fsm.addTransition(ResourceState.ErrorInMaintenance, Event.InternalCreated, ResourceState.ErrorInMaintenance);
s_fsm.addTransition(ResourceState.ErrorInMaintenance, Event.Disable, ResourceState.Disabled);
s_fsm.addTransition(ResourceState.ErrorInMaintenance, Event.DeleteHost, ResourceState.Disabled);
s_fsm.addTransition(ResourceState.ErrorInMaintenance, Event.InternalEnterMaintenance, ResourceState.Maintenance);
s_fsm.addTransition(ResourceState.ErrorInMaintenance, Event.AdminCancelMaintenance, ResourceState.Enabled);
s_fsm.addTransition(ResourceState.Error, Event.InternalCreated, ResourceState.Error);
s_fsm.addTransition(ResourceState.Disabled, Event.DeleteHost, ResourceState.Disabled);
}
}