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 EXPLICIT_ALLOCATE_REQUEST: 116 case FORCE_ALLOCATE_REQUEST: 117 return DeviceAllocationState.Allocated; 118 case STATE_CHANGE_OFFLINE: 119 return DeviceAllocationState.Unavailable; 120 case DISCONNECTED: 121 return DeviceAllocationState.Unknown; 122 default: 123 return DeviceAllocationState.Available; 124 125 } 126 } 127 } 128 129 /** 130 * Handles events in {@link DeviceAllocationState#Allocated} state. 131 * <p/> 132 * Transitions: 133 * <ul> 134 * <li>Allocated -> FREE_UNAVAILABLE -> Unavailable</li> 135 * <li>Allocated -> FREE_AVAILABLE -> Available</li> 136 * <li>Allocated -> FREE_UNRESPONSIVE -> Available</li> 137 * <li>Allocated -> FREE_UNKNOWN -> Unknown</li> 138 * </ul> 139 */ 140 class AllocatedHandler implements DeviceAllocationEventHandler { 141 @Override handleDeviceEvent(DeviceEvent event)142 public DeviceAllocationState handleDeviceEvent(DeviceEvent event) { 143 switch (event) { 144 case FREE_UNAVAILABLE: 145 return DeviceAllocationState.Unavailable; 146 case FREE_AVAILABLE: 147 return DeviceAllocationState.Available; 148 case FREE_UNRESPONSIVE: 149 return DeviceAllocationState.Available; 150 case FREE_UNKNOWN: 151 return DeviceAllocationState.Unknown; 152 default: 153 return DeviceAllocationState.Allocated; 154 } 155 } 156 } 157 158 /** 159 * Handles events in {@link DeviceAllocationState#Unavailable} state. 160 * <p/> 161 * Transitions: 162 * <ul> 163 * <li>Unavailable -> FORCE_ALLOCATE_REQUEST -> Allocated</li> 164 * <li>Unavailable -> DISCONNECTED -> Unknown</li> 165 * <li>Unavailable -> FORCE_AVAILABLE -> Available</li> 166 * </ul> 167 */ 168 class UnavailableHandler implements DeviceAllocationEventHandler { 169 @Override handleDeviceEvent(DeviceEvent event)170 public DeviceAllocationState handleDeviceEvent(DeviceEvent event) { 171 switch (event) { 172 case FORCE_ALLOCATE_REQUEST: 173 return DeviceAllocationState.Allocated; 174 case DISCONNECTED: 175 return DeviceAllocationState.Unknown; 176 case FORCE_AVAILABLE: 177 return DeviceAllocationState.Available; 178 default: 179 return DeviceAllocationState.Unavailable; 180 } 181 } 182 } 183 184 /** 185 * Handles events in {@link DeviceAllocationState#Ignored} state. 186 * <p/> 187 * Transitions: 188 * <ul> 189 * <li>Ignored -> DISCONNECTED -> Unknown</li> 190 * </ul> 191 */ 192 class IgnoredHandler implements DeviceAllocationEventHandler { 193 @Override handleDeviceEvent(DeviceEvent event)194 public DeviceAllocationState handleDeviceEvent(DeviceEvent event) { 195 switch (event) { 196 case DISCONNECTED: 197 return DeviceAllocationState.Unknown; 198 case EXPLICIT_ALLOCATE_REQUEST: 199 return DeviceAllocationState.Allocated; 200 default: 201 return DeviceAllocationState.Ignored; 202 } 203 } 204 } 205 } 206