1 package autotest.moblab; 2 3 import autotest.common.ui.TabView; 4 import autotest.moblab.rpc.ConnectedDutInfo; 5 import autotest.moblab.rpc.MoblabRpcCallbacks; 6 import autotest.moblab.rpc.MoblabRpcHelper; 7 import com.google.gwt.event.dom.client.ChangeEvent; 8 import com.google.gwt.event.dom.client.ChangeHandler; 9 import com.google.gwt.event.dom.client.ClickEvent; 10 import com.google.gwt.event.dom.client.ClickHandler; 11 import com.google.gwt.user.client.Window; 12 import com.google.gwt.user.client.ui.Button; 13 import com.google.gwt.user.client.ui.CheckBox; 14 import com.google.gwt.user.client.ui.FlexTable; 15 import com.google.gwt.user.client.ui.HasVerticalAlignment; 16 import com.google.gwt.user.client.ui.HorizontalPanel; 17 import com.google.gwt.user.client.ui.Label; 18 import com.google.gwt.user.client.ui.ListBox; 19 import com.google.gwt.user.client.ui.TextArea; 20 import com.google.gwt.user.client.ui.TextBox; 21 import com.google.gwt.user.client.ui.VerticalPanel; 22 23 /** 24 * Implement a tab that makes it easier to add/remove/maintain duts. 25 */ 26 public class DutManagementView extends TabView { 27 28 private FlexTable dutInfoTable; 29 private VerticalPanel dutSetupPanel; 30 private ListBox options; 31 private TextArea informationArea; 32 private Button actionButton; 33 private CheckBox poolCheckBox; 34 private TextBox poolLabelName; 35 private Label poolLabel; 36 private TextBox attributeName; 37 private TextBox attributeValue; 38 private HorizontalPanel labelActionRow; 39 private HorizontalPanel attribActionRow; 40 private HorizontalPanel attribValueActionRow; 41 42 private static final int DHCP_IP_COLUMN = 0; 43 private static final int DHCP_MAC_COLUMN = 1; 44 private static final int SELECTION_COLUMN = 2; 45 private static final int LABELS_COLUMN = 3; 46 47 @Override getElementId()48 public String getElementId() { 49 return "dut_manage"; 50 } 51 52 @Override refresh()53 public void refresh() { 54 super.refresh(); 55 dutInfoTable.removeAllRows(); 56 poolCheckBox.setValue(false); 57 poolLabelName.setText(""); 58 attributeName.setText(""); 59 attributeValue.setValue(""); 60 61 labelActionRow.setVisible(false); 62 attribActionRow.setVisible(false); 63 64 loadData(); 65 } 66 67 @Override initialize()68 public void initialize() { 69 super.initialize(); 70 // Main table of connected DUT information. 71 dutInfoTable = new FlexTable(); 72 73 // The row of controls underneath the main data table. 74 dutSetupPanel = new VerticalPanel(); 75 76 // List of actions to be applied to connected DUT's. 77 options = new ListBox(); 78 options.addItem("Add Selected DUT's"); 79 options.addItem("Remove Selected DUT's"); 80 options.addItem("Add Label Selected DUT's"); 81 options.addItem("Remove Label Selected DUT's"); 82 options.addItem("Add Attribute to Selected DUT's"); 83 options.addItem("Remove Attribute from Selected DUT's"); 84 options.setStyleName("dut_manage_action_row"); 85 options.addChangeHandler(new ChangeHandler() { 86 @Override 87 public void onChange(ChangeEvent event) { 88 if (options.getSelectedIndex() == 2 || options.getSelectedIndex() == 3) { 89 poolCheckBox.setValue(false); 90 poolLabelName.setText(""); 91 labelActionRow.setVisible(true); 92 attribActionRow.setVisible(false); 93 } else if (options.getSelectedIndex() == 4 || options.getSelectedIndex() == 5) { 94 attributeName.setText(""); 95 attributeValue.setValue(""); 96 labelActionRow.setVisible(false); 97 attribActionRow.setVisible(true); 98 if (options.getSelectedIndex() == 4) { 99 attribValueActionRow.setVisible(true); 100 } else { 101 attribValueActionRow.setVisible(false); 102 } 103 } else { 104 labelActionRow.setVisible(false); 105 attribActionRow.setVisible(false); 106 } 107 } 108 }); 109 110 // Logging area at the end of the screen that gives status messages about bulk 111 // actions requested. 112 informationArea = new TextArea(); 113 informationArea.setVisibleLines(10); 114 informationArea.setCharacterWidth(80); 115 informationArea.setReadOnly(true); 116 117 // Apply button, each action needs to be applied after selecting the devices and 118 // the action to be performed. 119 actionButton = new Button("Apply", new ClickHandler() { 120 public void onClick(ClickEvent event) { 121 ((Button)event.getSource()).setEnabled(false); 122 int action = options.getSelectedIndex(); 123 try { 124 for (int i = 1; i < dutInfoTable.getRowCount(); i++) { 125 if (((CheckBox)dutInfoTable.getWidget(i, SELECTION_COLUMN)).getValue()) { 126 if (action == 0) { 127 addDut(i); 128 } else if (action == 1) { 129 removeDut(i); 130 } else if (action == 2) { 131 addLabel(i, getPoolLabelString()); 132 } else if (action == 3) { 133 removeLabel(i, getPoolLabelString()); 134 } else if (action == 4) { 135 addAttribute(i, attributeName.getText(), attributeValue.getText()); 136 } else if (action == 5) { 137 removeAttribute(i, attributeName.getText()); 138 } 139 } 140 } 141 } finally { 142 ((Button)event.getSource()).setEnabled(true); 143 } 144 }}); 145 146 147 // For adding and removing labels a text input of the label is required. 148 poolCheckBox = new CheckBox(); 149 150 // Pools are just special labels, this is just a helper to so users get 151 // it correct more of the time. 152 poolCheckBox.setText("Is pool label ?"); 153 poolCheckBox.setStyleName("dut_manage_action_row_item"); 154 155 // The text label explaining the text box is for entering the label. 156 poolLabel = new Label(); 157 poolLabel.setText("Label name:"); 158 poolLabel.setStyleName("dut_manage_action_row_item"); 159 160 // The text entry of the label to add or remove. 161 poolLabelName = new TextBox(); 162 poolLabelName.setStyleName("dut_manage_action_row_item"); 163 164 labelActionRow = new HorizontalPanel(); 165 labelActionRow.add(poolCheckBox); 166 labelActionRow.add(poolLabel); 167 labelActionRow.add(poolLabelName); 168 169 // The name/value pair required to set an attribute 170 Label attributeNameLabel = new Label(); 171 attributeNameLabel.setText("Attribute:"); 172 attributeNameLabel.setStyleName("dut_manage_action_row_item"); 173 attributeName = new TextBox(); 174 attributeName.setStyleName("dut_manage_action_row_item"); 175 176 Label attributeValueLabel = new Label(); 177 attributeValueLabel.setText("Value:"); 178 attributeValueLabel.setStyleName("dut_manage_action_row_item"); 179 attributeValue = new TextBox(); 180 attributeValue.setStyleName("dut_manage_action_row_item"); 181 182 attribActionRow = new HorizontalPanel(); 183 attribActionRow.add(attributeNameLabel); 184 attribActionRow.add(attributeName); 185 attribValueActionRow = new HorizontalPanel(); 186 attribValueActionRow.add(attributeValueLabel); 187 attribValueActionRow.add(attributeValue); 188 attribActionRow.add(attribValueActionRow); 189 190 // Assemble the display panels in the correct order. 191 dutSetupPanel.add(dutInfoTable); 192 HorizontalPanel actionRow = new HorizontalPanel(); 193 actionRow.setStyleName("dut_manage_action_row"); 194 actionRow.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE); 195 actionRow.add(options); 196 actionRow.add(labelActionRow); 197 actionRow.add(attribActionRow); 198 actionRow.add(actionButton); 199 dutSetupPanel.add(actionRow); 200 dutSetupPanel.add(informationArea); 201 addWidget(dutSetupPanel, "view_dut_manage"); 202 } 203 getPoolLabelString()204 private String getPoolLabelString() { 205 StringBuilder builder = new StringBuilder(poolLabelName.getText()); 206 if (poolCheckBox.getValue()) { 207 builder.insert(0, "pool:"); 208 } 209 return builder.toString(); 210 } 211 loadData()212 private void loadData() { 213 MoblabRpcHelper.fetchDutInformation(new MoblabRpcCallbacks.FetchConnectedDutInfoCallback() { 214 @Override 215 public void onFetchConnectedDutInfoSubmitted(ConnectedDutInfo info) { 216 addTableHeader(); 217 // The header is row 0 218 int row = 1; 219 for (final String dutIpAddress : info.getConnectedIpsToMacAddress().keySet()) { 220 addRowStyles(row); 221 String labelString; 222 if (info.getConfiguredIpsToLabels().keySet().contains(dutIpAddress)) { 223 labelString = info.getConfiguredIpsToLabels().get(dutIpAddress); 224 } else { 225 boolean sshOk = 226 info.getConnectedIpsToSshConnection().get(dutIpAddress); 227 labelString = sshOk ? "DUT Not Configured in Autotest" : 228 "Unable to connect to DUT over SSH, check device is powered " + 229 "on, connected and has a test image on it."; 230 } 231 addRow(row, dutIpAddress, info.getConnectedIpsToMacAddress().get(dutIpAddress), labelString); 232 row++; 233 } 234 for (final String dutIpAddress : info.getConfiguredIpsToLabels().keySet()) { 235 if (!info.getConnectedIpsToMacAddress().keySet().contains(dutIpAddress)) { 236 // Device is in AFE but not detected in the DHCP table. 237 addRowStyles(row); 238 addRow(row, dutIpAddress, "", 239 "DUT Configured in Autotest but does not appear to be attached."); 240 row++; 241 } 242 } 243 } 244 }); 245 } 246 247 /** 248 * Add the correct css styles for each data row. 249 * @param row index of the row to apply the styles for, first data row is 1. 250 */ addRowStyles(int row)251 private void addRowStyles(int row) { 252 dutInfoTable.getCellFormatter().addStyleName(row, DHCP_IP_COLUMN,"ip_cell"); 253 dutInfoTable.getCellFormatter().addStyleName(row,DHCP_MAC_COLUMN,"mac_cell"); 254 dutInfoTable.getCellFormatter().addStyleName(row,SELECTION_COLUMN,"selection_cell"); 255 dutInfoTable.getCellFormatter().addStyleName(row,LABELS_COLUMN,"labels_cell"); 256 } 257 258 /** 259 * Insert or update the data in the table for a given row. 260 * @param row The row index to update, first data row is 1. 261 * @param ipColumn String to be added into the first column. 262 * @param macColumn String to be added to the second column. 263 * @param labelsColumn String to be added to the fourth column. 264 */ addRow(int row, String ipColumn, String macColumn, String labelsColumn)265 private void addRow(int row, String ipColumn, String macColumn, String labelsColumn) { 266 dutInfoTable.setWidget(row, DHCP_IP_COLUMN, new Label(ipColumn)); 267 dutInfoTable.setWidget(row, DHCP_MAC_COLUMN, new Label(macColumn)); 268 dutInfoTable.setWidget(row, SELECTION_COLUMN, new CheckBox()); 269 dutInfoTable.setWidget(row, LABELS_COLUMN, new Label(labelsColumn)); 270 } 271 272 /** 273 * Add the column headers with the correct css styling into the data table. 274 */ addTableHeader()275 private void addTableHeader() { 276 dutInfoTable.addStyleName("dut_info_table"); 277 dutInfoTable.getCellFormatter().addStyleName(0, DHCP_IP_COLUMN, 278 "dut_manage_column_label_c"); 279 dutInfoTable.getCellFormatter().addStyleName(0, DHCP_MAC_COLUMN, 280 "dut_manage_column_label_c"); 281 dutInfoTable.getCellFormatter().addStyleName(0, SELECTION_COLUMN, 282 "dut_manage_column_label_c"); 283 dutInfoTable.getCellFormatter().addStyleName(0, LABELS_COLUMN, 284 "dut_manage_column_label_c"); 285 dutInfoTable.setWidget(0, DHCP_IP_COLUMN, new Label("DCHP Lease Address")); 286 dutInfoTable.setWidget(0, DHCP_MAC_COLUMN, new Label("DCHP MAC Address")); 287 dutInfoTable.setWidget(0, LABELS_COLUMN, new Label("DUT Labels")); 288 } 289 290 /** 291 * Make an RPC call to the autotest system to enroll the DUT listed at the given row number. 292 * @param row_number the row number in the table that has details of the device to enroll. 293 */ addDut(int row_number)294 private void addDut(int row_number) { 295 String ipAddress = ((Label)dutInfoTable.getWidget(row_number, DHCP_IP_COLUMN)).getText(); 296 MoblabRpcHelper.addMoblabDut(ipAddress, new LogAction(informationArea)); 297 } 298 299 /** 300 * Make an RPC to to the autotest system to delete information about the DUT listed at the given 301 * row. 302 * @param row_number the row number in the table that has details of the device to remove. 303 */ removeDut(int row_number)304 private void removeDut(int row_number) { 305 String ipAddress = ((Label)dutInfoTable.getWidget(row_number, DHCP_IP_COLUMN)).getText(); 306 MoblabRpcHelper.removeMoblabDut(ipAddress, new LogAction(informationArea)); 307 } 308 309 /** 310 * Make an RPC to to the autotest system to add a label to a DUT whoes details are in the given 311 * row. 312 * @param row_number row in the data table that has the information about the DUT 313 * @param labelName the label string to be added. 314 */ addLabel(int row_number, String labelName)315 private void addLabel(int row_number, String labelName) { 316 String ipAddress = ((Label)dutInfoTable.getWidget(row_number, DHCP_IP_COLUMN)).getText(); 317 MoblabRpcHelper.addMoblabLabel(ipAddress, labelName, new LogAction(informationArea)); 318 } 319 320 /** 321 * Make an RPC to to the autotest system to remove a label to a DUT whoes details are in the 322 * given row. 323 * @param row_number row in the data table that has the information about the DUT 324 * @param labelName the label string to be removed. 325 */ removeLabel(int row_number, String labelName)326 private void removeLabel(int row_number, String labelName) { 327 String ipAddress = ((Label)dutInfoTable.getWidget(row_number, DHCP_IP_COLUMN)).getText(); 328 MoblabRpcHelper.removeMoblabLabel(ipAddress, labelName, new LogAction(informationArea)); 329 } 330 331 /** 332 * Make an RPC to to the autotest system to add an attribute to a DUT whoes details are in the 333 * given row. 334 * @param row_number row in the data table that has the information about the DUT 335 * @param attributeName the attribute name to be set. 336 * @param attributeValue the attribute value to be associated with the attributeName. 337 */ addAttribute(int row_number, String attributeName, String attributeValue)338 private void addAttribute(int row_number, String attributeName, String attributeValue) { 339 String ipAddress = ((Label)dutInfoTable.getWidget(row_number, DHCP_IP_COLUMN)).getText(); 340 MoblabRpcHelper.setMoblabAttribute(ipAddress, attributeName, attributeValue, new LogAction(informationArea)); 341 } 342 343 /** 344 * Make an RPC to to the autotest system to remove an attribute to a DUT whoes details are in the 345 * given row. 346 * @param row_number row in the data table that has the information about the DUT 347 * @param attributeName the attribute name to be removed. 348 */ removeAttribute(int row_number, String attributeName)349 private void removeAttribute(int row_number, String attributeName) { 350 String ipAddress = ((Label)dutInfoTable.getWidget(row_number, DHCP_IP_COLUMN)).getText(); 351 MoblabRpcHelper.removeMoblabAttribute(ipAddress, attributeName, new LogAction(informationArea)); 352 } 353 354 /** 355 * Call back that inserts a string from an completed RPC into the UI. 356 */ 357 private static class LogAction implements MoblabRpcCallbacks.LogActionCompleteCallback { 358 private TextArea informationArea; 359 LogAction(TextArea informationArea)360 LogAction(TextArea informationArea){ 361 this.informationArea = informationArea; 362 } 363 @Override onLogActionComplete(boolean status, String information)364 public void onLogActionComplete(boolean status, String information) { 365 String currentText = informationArea.getText(); 366 informationArea 367 .setText(new StringBuilder().append(information).append( 368 "\n").append(currentText).toString()); 369 } 370 } 371 } 372