1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tradefed.device;
17 
18 /**
19  * Implements the test device allocation state machine.
20  * <p/>
21  * Uses the state machine design pattern
22  */
23 interface DeviceAllocationEventHandler {
handleDeviceEvent(DeviceEvent event)24     DeviceAllocationState handleDeviceEvent(DeviceEvent event);
25 
26     /**
27      * Handles events in {@link DeviceAllocationState#Unknown} state.
28      * <p/>
29      * Transitions:
30      * <ul>
31      * <li>Unknown -> FORCE_ALLOCATE_REQUEST -> Allocated</li>
32      * <li>Unknown -> CONNECTED_ONLINE -> Checking_Availability</li>
33      * <li>Unknown -> CONNECTED_OFFLINE -> Unavailable</li>
34      * <li>Unknown -> STATE_CHANGE_ONLINE -> Checking_Availability</li>
35      * <li>Unknown -> STATE_CHANGE_OFFLINE -> Unavailable</li>
36      * <li>Unknown -> FORCE_AVAILABLE -> Available</li>
37      * </ul>
38      */
39     class UnknownHandler implements DeviceAllocationEventHandler {
40         @Override
handleDeviceEvent(DeviceEvent event)41         public DeviceAllocationState handleDeviceEvent(DeviceEvent event) {
42             switch (event) {
43                 case FORCE_ALLOCATE_REQUEST:
44                     return DeviceAllocationState.Allocated;
45                 case CONNECTED_ONLINE:
46                     return DeviceAllocationState.Checking_Availability;
47                 case CONNECTED_OFFLINE:
48                     return DeviceAllocationState.Unavailable;
49                 case STATE_CHANGE_ONLINE:
50                     return DeviceAllocationState.Checking_Availability;
51                 case STATE_CHANGE_OFFLINE:
52                     return DeviceAllocationState.Unavailable;
53                 case FORCE_AVAILABLE:
54                     return DeviceAllocationState.Available;
55                 default:
56                     return DeviceAllocationState.Unknown;
57             }
58         }
59     }
60 
61     /**
62      * Handles events in {@link DeviceAllocationState#Checking_Availability} state.
63      * <p/>
64      * Transitions:
65      * <ul>
66      * <li>Checking_Availability -> FORCE_ALLOCATE_REQUEST -> Allocated</li>
67      * <li>Checking_Availability -> AVAILABLE_CHECK_PASSED -> Available</li>
68      * <li>Checking_Availability -> AVAILABLE_CHECK_FAILED -> Unavailable</li>
69      * <li>Checking_Availability -> AVAILABLE_CHECK_IGNORED -> Ignored</li>
70      * <li>Checking_Availability -> FORCE_AVAILABLE -> Available</li>
71      * <li>Checking_Availability -> STATE_CHANGE_OFFLINE -> Unavailable</li>
72      * <li>Checking_Availability -> DISCONNECTED -> Unavailable</li>
73      * </ul>
74      */
75     class CheckingAvailHandler implements DeviceAllocationEventHandler {
76         @Override
handleDeviceEvent(DeviceEvent event)77         public DeviceAllocationState handleDeviceEvent(DeviceEvent event) {
78             switch (event) {
79                 case FORCE_ALLOCATE_REQUEST:
80                     return DeviceAllocationState.Allocated;
81                 case AVAILABLE_CHECK_PASSED:
82                     return DeviceAllocationState.Available;
83                 case AVAILABLE_CHECK_FAILED:
84                     return DeviceAllocationState.Unavailable;
85                 case AVAILABLE_CHECK_IGNORED:
86                     return DeviceAllocationState.Ignored;
87                 case FORCE_AVAILABLE:
88                     return DeviceAllocationState.Available;
89                 case STATE_CHANGE_OFFLINE:
90                     return DeviceAllocationState.Unavailable;
91                 case DISCONNECTED:
92                     return DeviceAllocationState.Unavailable;
93                 default:
94                     return DeviceAllocationState.Checking_Availability;
95             }
96         }
97     }
98 
99     /**
100      * Handles events in {@link DeviceAllocationState#Available} state.
101      * <p/>
102      * Transitions:
103      * <ul>
104      * <li>Available -> ALLOCATE_REQUEST -> Allocated</li>
105      * <li>Available -> FORCE_ALLOCATE_REQUEST -> Allocated</li>
106      * <li>Available -> STATE_CHANGE_OFFLINE -> Unavailable</li>
107      * <li>Available -> DISCONNECTED -> Unknown</li>
108      * </ul>
109      */
110     class AvailableHandler implements DeviceAllocationEventHandler {
111         @Override
handleDeviceEvent(DeviceEvent event)112         public DeviceAllocationState handleDeviceEvent(DeviceEvent event) {
113             switch (event) {
114                 case ALLOCATE_REQUEST:
115                 case FORCE_ALLOCATE_REQUEST:
116                     return DeviceAllocationState.Allocated;
117                 case STATE_CHANGE_OFFLINE:
118                     return DeviceAllocationState.Unavailable;
119                 case DISCONNECTED:
120                     return DeviceAllocationState.Unknown;
121                 default:
122                     return DeviceAllocationState.Available;
123 
124             }
125         }
126     }
127 
128     /**
129      * Handles events in {@link DeviceAllocationState#Allocated} state.
130      * <p/>
131      * Transitions:
132      * <ul>
133      * <li>Allocated -> FREE_UNAVAILABLE -> Unavailable</li>
134      * <li>Allocated -> FREE_AVAILABLE -> Available</li>
135      * <li>Allocated -> FREE_UNRESPONSIVE -> Available</li>
136      * <li>Allocated -> FREE_UNKNOWN -> Unknown</li>
137      * </ul>
138      */
139     class AllocatedHandler implements DeviceAllocationEventHandler {
140         @Override
handleDeviceEvent(DeviceEvent event)141         public DeviceAllocationState handleDeviceEvent(DeviceEvent event) {
142             switch (event) {
143                 case FREE_UNAVAILABLE:
144                     return DeviceAllocationState.Unavailable;
145                 case FREE_AVAILABLE:
146                     return DeviceAllocationState.Available;
147                 case FREE_UNRESPONSIVE:
148                     return DeviceAllocationState.Available;
149                 case FREE_UNKNOWN:
150                     return DeviceAllocationState.Unknown;
151                 default:
152                     return DeviceAllocationState.Allocated;
153             }
154         }
155     }
156 
157     /**
158      * Handles events in {@link DeviceAllocationState#Unavailable} state.
159      * <p/>
160      * Transitions:
161      * <ul>
162      * <li>Unavailable -> FORCE_ALLOCATE_REQUEST -> Allocated</li>
163      * <li>Unavailable -> DISCONNECTED -> Unknown</li>
164      * <li>Unavailable -> FORCE_AVAILABLE -> Available</li>
165      * </ul>
166      */
167     class UnavailableHandler implements DeviceAllocationEventHandler {
168         @Override
handleDeviceEvent(DeviceEvent event)169         public DeviceAllocationState handleDeviceEvent(DeviceEvent event) {
170             switch (event) {
171                 case FORCE_ALLOCATE_REQUEST:
172                     return DeviceAllocationState.Allocated;
173                 case DISCONNECTED:
174                     return DeviceAllocationState.Unknown;
175                 case FORCE_AVAILABLE:
176                     return DeviceAllocationState.Available;
177                 default:
178                     return DeviceAllocationState.Unavailable;
179             }
180         }
181     }
182 
183     /**
184      * Handles events in {@link DeviceAllocationState#Ignored} state.
185      * <p/>
186      * Transitions:
187      * <ul>
188      * <li>Ignored -> DISCONNECTED -> Unknown</li>
189      * </ul>
190      */
191     class IgnoredHandler implements DeviceAllocationEventHandler {
192         @Override
handleDeviceEvent(DeviceEvent event)193         public DeviceAllocationState handleDeviceEvent(DeviceEvent event) {
194             switch (event) {
195                 case DISCONNECTED:
196                     return DeviceAllocationState.Unknown;
197                 default:
198                     return DeviceAllocationState.Ignored;
199             }
200         }
201     }
202 }
203