package com.servprise.documentcode.wreapimanual; import static org.testng.AssertJUnit.assertFalse; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import org.testng.Assert; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; import com.servprise.dev.schemas._2007._06.WebRebootService_wsdl.WebReboot; import com.servprise.dev.schemas._2007._06.WebRebootService_wsdl.WebRebootService; import com.servprise.dev.schemas._2007._06.WebRebootService_wsdl.WebRebootServiceLocator; import com.servprise.dev.schemas._2007._06.WebRebootService_wsdl.WebRebootSoapBindingStub; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.CommandResult; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.ConnectionStatusEnum; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.EditLogConfigurationRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.EditLogConfigurationResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.EditNetworkConfigurationRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.EditNetworkConfigurationResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.EditPasswordRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.EditPasswordResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.EditWebRebootPortRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.EditWebRebootPortResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetBatchWebRebootPortRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetBatchWebRebootPortResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetLogConfigurationRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetLogConfigurationResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetNetworkConfigurationRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetNetworkConfigurationResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetSystemInformationRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetSystemInformationResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetWebRebootPortRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.GetWebRebootPortResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.LogConfiguration; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.LogLevelEnum; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.PowerOffRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.PowerOffResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.PowerOnRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.PowerOnResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.PowerRebootRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.PowerRebootResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.PowerStatusEnum; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.ResetSwitchRebootRequest; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.ResetSwitchRebootResponse; import com.servprise.dev.schemas._2007._06.WebRebootService_xsd.WebRebootPort; @Test(groups = { "examples" }) public class Examples { // The maximum line length for Word formatting is to this mark >>> | CommandResult // refJVarCommandResult . commandResult; WebRebootService service = null; WebReboot wr = null; public Examples() { } /** * Setup and authenticate the connection to the WebReboot service. * * @throws MalformedURLException * @throws ServiceException */ @BeforeSuite(alwaysRun = true) public void connectToWebRebootService() throws MalformedURLException, ServiceException { final String ipAddress = System.getProperty("developmentHostAddress.property", "127.0.0.1"); final String port = System.getProperty("developmentHostPort.property", "8080"); // The development server uses a session configuration scope. If we want the development // server to look like a real WebReboot Enterprise, we need it to maintain state in between // web service calls. By setting this to TRUE, we ensure that we properly keep session information // in between calls. Note that this is only relevant to the development server; it won't affect // anything when used with a real WebReboot Enterprise. // refJCodeConnectToService . WebRebootService service = new WebRebootServiceLocator(); URL url = new URL("http://" + ipAddress + ":" + port + "/admin/services/WebRebootService"); WebReboot wr = service.getWebRebootService(url); ((WebRebootSoapBindingStub) wr).setMaintainSession(true); ((WebRebootSoapBindingStub) wr)._setProperty(javax.xml.rpc.Stub.USERNAME_PROPERTY, "admin"); ((WebRebootSoapBindingStub) wr)._setProperty(javax.xml.rpc.Stub.PASSWORD_PROPERTY, "password"); // refJCodeConnectToService . // Make sure we set the primary instance in this class this.service = service; this.wr = wr; } /** * Example showing iterating through port by port, and directly accessing a single port * * @throws RemoteException */ public void showSinglePortAccess() throws RemoteException { // refJCodeListFirstTenPortNames . for (int i = 0; i < 10; i++) { GetWebRebootPortRequest request = new GetWebRebootPortRequest(i + 1); GetWebRebootPortResponse response = wr.getWebRebootPort(request); WebRebootPort port = response.getWebRebootPort(); System.out.println(port.getPortNumber() + ". " + port.getName()); } //refJCodeListFirstTenPortNames . GetWebRebootPortRequest request = new GetWebRebootPortRequest(1); GetWebRebootPortResponse response = wr. getWebRebootPort // refJMethodGetPortConfiguration . (request); WebRebootPort // refJVarPortConfiguration . portConfiguration = response.getWebRebootPort(); Assert.assertNotNull(portConfiguration); } /** * Example showing how to grab all port configurations at once * * @throws RemoteException */ public void showMultiplePortAccess() throws RemoteException { // refJCodeListAllPortNames . GetBatchWebRebootPortRequest request = new GetBatchWebRebootPortRequest(); GetBatchWebRebootPortResponse response = wr.getBatchWebRebootPort(request); WebRebootPort[] ports = response.getWebRebootPortArray().getWebRebootPort(); for (int i = 0; i < ports.length; i++) { System.out.println(ports[i].getPortNumber() + ". " + ports[i].getName()); } // refJCodeListAllPortNames . GetBatchWebRebootPortRequest batchRequest = new GetBatchWebRebootPortRequest(); GetBatchWebRebootPortResponse batchResponse = wr. getBatchWebRebootPort // refJMethodGetBatchPortConfiguration . (batchRequest); Assert.assertNotNull(batchResponse.getWebRebootPortArray()); } /** * Example showing reboot capabilities based on switch configuration * * @throws RemoteException */ public void showServerSwitchConfiguration() throws RemoteException { // refJCodeShowServerSwitchConfiguration . GetBatchWebRebootPortRequest request = new GetBatchWebRebootPortRequest(); GetBatchWebRebootPortResponse response = wr.getBatchWebRebootPort(request); WebRebootPort[] ports = response.getWebRebootPortArray(). getWebRebootPort(); for (int i = 0; i < ports.length; i++) { WebRebootPort port = ports[i]; if (true == port.getServerResetSwitchSupported()) { System.out.println("Can reboot via reset switch: " + port.getName()); } if (true == port.getServerPowerSwitchSupported()) { System.out.println("Can reboot, power off, and power on via power switch: " + port.getName()); } } // refJCodeShowServerSwitchConfiguration . } /** * Example showing how to retrieve the status information for a server * * @throws RemoteException */ public void showServerStatus() throws RemoteException { // refJCodeShowServerStatus . GetBatchWebRebootPortRequest request = new GetBatchWebRebootPortRequest(); GetBatchWebRebootPortResponse response = wr.getBatchWebRebootPort(request); WebRebootPort[] ports = response.getWebRebootPortArray(). getWebRebootPort(); for (int i = 0; i < ports.length; i++) { WebRebootPort port = ports[i]; if (ConnectionStatusEnum.CONNECTED == port.getConnectionStatus()) { System.out.println("Server " + port.getName() +" is connected."); } else if (ConnectionStatusEnum.DISCONNECTED == port.getConnectionStatus()) { System.out.println("Server " + port.getName() +" is disconnected."); } if (PowerStatusEnum.ON == port.getPowerStatus()) { System.out.println("Server " + port.getName() +" is powered on."); } else if (PowerStatusEnum.OFF == port.getPowerStatus()) { System.out.println("Server " + port.getName() +" is powered off."); } } // refJCodeShowServerStatus . } /** * Fixed test method for assisting show*SwitchReboot/Off/On * * @return 1, always port 1 */ private int getPortNumber() { return 1; } /** * Fixed test method for assisting show*SwitchReboot/Off/On * * @return true, always positive confirmation */ private boolean confirmAction() { return true; } /** * Example showing how to perform a reset switch reboot * * @throws RemoteException */ public void showResetSwitchReboot() throws RemoteException { // refJCodeShowResetSwitchReboot . // Prompt user for information. int portNumber = getPortNumber(); GetWebRebootPortRequest infoRequest = new GetWebRebootPortRequest(); infoRequest.setPortNumber(portNumber); GetWebRebootPortResponse infoResponse = wr.getWebRebootPort(infoRequest); WebRebootPort port = infoResponse.getWebRebootPort(); // Check that the server supports reset reboot. if (true == port.getServerResetSwitchSupported()) { System.out.println("You are about to reset switch reboot: " + port.getName()); // Prompt user for confirmation of reboot action. boolean confirm = confirmAction(); if (true == confirm) { ResetSwitchRebootRequest rebootRequest = new ResetSwitchRebootRequest(); rebootRequest.setPortNumber(port.getPortNumber()); ResetSwitchRebootResponse rebootResponse = wr.doResetSwitchReboot(rebootRequest); // Check if there was an error executing the command. CommandResult result = rebootResponse.getCommandResult(); if (result.isError()) { System.err.println(result.getResultMessage()); } } } // refJCodeShowResetSwitchReboot . } /** * Example showing how to perform a power switch reboot * * @throws RemoteException */ public void showPowerReboot() throws RemoteException { // refJCodeShowPowerSwitchReboot . // Prompt user for information. int portNumber = getPortNumber(); GetWebRebootPortRequest infoRequest = new GetWebRebootPortRequest(); infoRequest.setPortNumber(portNumber); GetWebRebootPortResponse infoResponse = wr.getWebRebootPort(infoRequest); WebRebootPort port = infoResponse.getWebRebootPort(); // Check that the server supports power operations. if (true == port.getServerPowerSwitchSupported()) { System.out.println("You are about to power switch reboot: " + port.getName()); // Prompt user for confirmation of reboot action. boolean confirm = confirmAction(); if (true == confirm) { PowerRebootRequest rebootRequest = new PowerRebootRequest(); rebootRequest.setPortNumber(port.getPortNumber()); PowerRebootResponse rebootResponse = wr.doPowerReboot(rebootRequest); // Check if there was an error executing the command. CommandResult result = rebootResponse.getCommandResult(); if (result.isError()) { System.err.println(result.getResultMessage()); } } } // refJCodeShowPowerSwitchReboot . } /** * Example showing how to perform a power off * * @throws RemoteException */ public void showPowerOff() throws RemoteException { // refJCodeShowPowerOff . // Prompt user for information. int portNumber = getPortNumber(); GetWebRebootPortRequest infoRequest = new GetWebRebootPortRequest(); infoRequest.setPortNumber(portNumber); GetWebRebootPortResponse infoResponse = wr.getWebRebootPort(infoRequest); WebRebootPort port = infoResponse.getWebRebootPort(); // Check that the server supports power operations. if (true == port.getServerPowerSwitchSupported()) { System.out.println("You are about to power-off: " + port.getName()); // Prompt user for confirmation of power toggle action. boolean confirm = confirmAction(); if (true == confirm) { PowerOffRequest powerOffRequest = new PowerOffRequest(); powerOffRequest.setPortNumber(port.getPortNumber()); PowerOffResponse powerOffResponse = wr.doPowerOff(powerOffRequest); // Check if there was an error executing the command. CommandResult result = powerOffResponse.getCommandResult(); if (result.isError()) { System.err.println(result.getResultMessage()); } } } // refJCodeShowPowerOff . PowerOffRequest powerOffRequest = new PowerOffRequest(); PowerOffResponse powerOffResponse = wr. doPowerOff // refJMethodDoPowerOff . (powerOffRequest); Assert.assertNotNull(powerOffResponse); } /** * Example showing how to perform a power on * * @throws RemoteException */ public void showPowerOn() throws RemoteException { // refJCodeShowPowerOn . // Prompt user for information. int portNumber = getPortNumber(); GetWebRebootPortRequest infoRequest = new GetWebRebootPortRequest(); infoRequest.setPortNumber(portNumber); GetWebRebootPortResponse infoResponse = wr.getWebRebootPort(infoRequest); WebRebootPort port = infoResponse.getWebRebootPort(); // Check that the server supports power operations. if (true == port.getServerPowerSwitchSupported()) { System.out.println("You are about to power-on: " + port.getName()); // Prompt user for confirmation of power toggle action. boolean confirm = confirmAction(); if (true == confirm) { PowerOnRequest powerOnRequest = new PowerOnRequest(); powerOnRequest.setPortNumber(port.getPortNumber()); PowerOnResponse powerOnResponse = wr.doPowerOn(powerOnRequest); // Check if there was an error executing the command. CommandResult result = powerOnResponse.getCommandResult(); if (result.isError()) { System.err.println(result.getResultMessage()); } } } // refJCodeShowPowerOn . PowerOnRequest powerOnRequest = new PowerOnRequest(); PowerOnResponse powerOnResponse = wr. doPowerOn // refJMethodDoPowerOn . (powerOnRequest); Assert.assertNotNull(powerOnResponse); } /** * Fixed test method for assisting showServerConfigurationEditing * * @return "New Server Name" */ private String getNewServerName() { return "New Server Name"; } /** * Example showing how to edit a server's name configuration * * @throws RemoteException */ public void showServerNameEditing() throws RemoteException { // Get the port's current name, so we can reset it when we are all done. GetWebRebootPortRequest getPortRequest = new GetWebRebootPortRequest(); getPortRequest.setPortNumber(getPortNumber()); GetWebRebootPortResponse getPortResponse = wr.getWebRebootPort(getPortRequest); assertFalse(getPortResponse.getCommandResult().isError()); String oldServerName = getPortResponse.getWebRebootPort().getName(); // refJCodeShowRenameServer . // Prompt user for information. int portNumber = getPortNumber(); String newServerName = getNewServerName(); EditWebRebootPortRequest editPortRequest = new EditWebRebootPortRequest(); editPortRequest.setPortNumber(portNumber); editPortRequest.setName(newServerName); EditWebRebootPortResponse editPortResponse = wr.editWebRebootPort(editPortRequest); // Check if there was an error executing the command. CommandResult result = editPortResponse.getCommandResult(); if (result.isError()) { System.err.println(result.getResultMessage()); } // refJCodeShowRenameServer . // Reset the port's name when we're done editPortRequest = new EditWebRebootPortRequest(); editPortRequest.setPortNumber(getPortNumber()); editPortRequest.setName(oldServerName); editPortResponse = wr. editWebRebootPort // refJMethodEditWebRebootPortConfiguration . (editPortRequest); } /** * Example showing how to edit a server's switch configuration * * @throws RemoteException */ public void showServerSwitchConfigurationEditing() throws RemoteException { // refJCodeShowEditSwitchConfiguration . // Prompt user for information. int portNumber = getPortNumber(); final boolean switchEnabledValue = true; final boolean switchDisabledValue = false; EditWebRebootPortRequest editSupportedSwitchesRequest = new EditWebRebootPortRequest(); editSupportedSwitchesRequest.setPortNumber(portNumber); editSupportedSwitchesRequest.setServerPowerSwitchSupported(switchDisabledValue); editSupportedSwitchesRequest.setServerResetSwitchSupported(switchEnabledValue); EditWebRebootPortResponse editSupportedSwitchesResponse = wr.editWebRebootPort (editSupportedSwitchesRequest); // Check if there was an error executing the command. CommandResult result = editSupportedSwitchesResponse.getCommandResult(); if (result.isError()) { System.err.println(result.getResultMessage()); } // refJCodeShowEditSwitchConfiguration . } /** * Example showing how to edit the network configuration * * @throws RemoteException * @throws InterruptedException */ public void showNetworkConfigurationEditing() throws RemoteException, InterruptedException { // Pre-load the example variables with "safe" data GetNetworkConfigurationRequest initRequest = new GetNetworkConfigurationRequest(); GetNetworkConfigurationResponse initResponse = wr.getNetworkConfiguration(initRequest); String newIpAddress = initResponse.getIpAddress(); String newSubnetMask = initResponse.getSubnetMask(); boolean dhcpDisabledValue = initResponse.getDhcpEnabled(); // This is incorrect, but done so for the example to make sense // refJCodeShowEditNetworkConfiguration . // // First, we'll retrieve the existing configuration and print it // to the console. // GetNetworkConfigurationRequest getRequest = new GetNetworkConfigurationRequest(); GetNetworkConfigurationResponse getResponse = wr.getNetworkConfiguration(getRequest); // Check if there was an error executing the command. CommandResult getResult = getResponse.getCommandResult(); if (getResult.isError()) { System.err.println(getResult.getResultMessage()); } // Print network configuration details to the console System.out.println("IP Address: " + getResponse.getIpAddress() ); System.out.println("Subnet Mask: " + getResponse.getSubnetMask() ); System.out.println("DHCP Enabled?: " + getResponse.getDhcpEnabled().toString() ); // // Second, we'll edit the configuration to use a static IP address // EditNetworkConfigurationRequest editRequest = new EditNetworkConfigurationRequest(); // Disable DHCP (i.e., setDhcpEnabled to FALSE). editRequest.setDhcpEnabled(dhcpDisabledValue); // Set the new network information. editRequest.setIpAddress(newIpAddress); editRequest.setSubnetMask(newSubnetMask); EditNetworkConfigurationResponse editResponse = wr.editNetworkConfiguration(editRequest); // Check if there was an error executing the command. CommandResult editResult = editResponse.getCommandResult(); if (editResult.isError()) { System.err.println(editResult.getResultMessage()); } // At this point, the WebReboot Enterprise will begin to reboot with new // new network configuration. Your application will need to re-establish // the connection to the web service at the new address. Thread.sleep(60000); // Wait for the WRE to reboot, then reconnect... // refJCodeShowEditNetworkConfiguration . } /** * Example showing how to edit the administrative password * * @throws RemoteException */ public void showPasswordEditing() throws RemoteException { String newPassword = "password"; // refJCodeShowEditPassword . EditPasswordRequest request = new EditPasswordRequest(); // Set the new password request.setPassword(newPassword); EditPasswordResponse response = wr.editPassword(request); // Check if there was an error executing the command. CommandResult result = response.getCommandResult(); if (result.isError()) { System.err.println(result.getResultMessage()); } // refJCodeShowEditPassword . } /** * Example showing how to get the system information from a running WebReboot Enterprise * * @throws RemoteException */ public void showSystemInformation() throws RemoteException { // refJCodeShowSystemInformation . GetSystemInformationRequest request = new GetSystemInformationRequest(); GetSystemInformationResponse response = wr.getSystemInformation(request); // Check if there was an error executing the command. CommandResult result = response.getCommandResult(); if (result.isError()) { System.err.println(result.getResultMessage()); } //Print system information details to console System.out.println("Number of ports: " + response.getNumberOfPorts() ); System.out.println("Firmware version: " + response.getFirmwareVersion() ); System.out.println("API version: " + response.getApiVersion() ); System.out.println("Configuration version: " + response.getConfigurationFileVersion() ); // refJCodeShowSystemInformation . } /** * Example showing how to edit the logging capabilities of the WRE * * @throws RemoteException */ public void showLogConfigurationEditing() throws RemoteException { // Save the current configuration so we can reset it when we are done GetLogConfigurationRequest initRequest = new GetLogConfigurationRequest(); GetLogConfigurationResponse initResponse = wr.getLogConfiguration(initRequest); Assert.assertNotNull(initResponse); String syslogServerIpAddress = initResponse.getLogConfiguration().getSyslogServerIpAddress(); LogLevelEnum syslogLogLevel = initResponse.getLogConfiguration().getSyslogLogLevel(); // refJCodeShowEditLogConfiguration . // // First, we'll retrieve the existing log configuration and print it // to the console. // GetLogConfigurationRequest getRequest = new GetLogConfigurationRequest(); GetLogConfigurationResponse getResponse = wr.getLogConfiguration(getRequest); // Check if there was an error executing the command. CommandResult getResult = getResponse.getCommandResult(); if (getResult.isError()) { System.err.println(getResult.getResultMessage()); } // Get the aggregate element surrounding the log configuration details LogConfiguration logConfig = getResponse.getLogConfiguration(); // Print log configuration details to console System.out.println("Log level of local file: " + logConfig.getLocalFileLogLevel().toString() ); System.out.println("Log level of chassis error LED: " + logConfig.getChassisErrorLedLogLevel().toString() ); System.out.println("Log level of remote Syslog server: " + logConfig.getSyslogLogLevel().toString() ); System.out.println("IP address of remote Syslog server: " + logConfig.getSyslogServerIpAddress() ); // // Second, we'll edit the configuration to log all messages (i.e., a log // level of 'DEBUG') to a Syslog server at a static IP address. // EditLogConfigurationRequest editRequest = new EditLogConfigurationRequest(); // Set the IP address for the Syslog server editRequest.setSyslogServerIpAddress("10.8.0.1"); // Set the log level to 'DEBUG' so all messages get logged, no matter how // trivial they may be editRequest.setSyslogLogLevel(LogLevelEnum.DEBUG); EditLogConfigurationResponse editResponse = wr.editLogConfiguration(editRequest); // Check if there was an error executing the command. CommandResult editResult = editResponse.getCommandResult(); if (editResult.isError()) { System.err.println(editResult.getResultMessage()); } // refJCodeShowEditLogConfiguration . // Reset the log information when we are done EditLogConfigurationRequest resetRequest = new EditLogConfigurationRequest(); resetRequest.setSyslogLogLevel(syslogLogLevel); resetRequest.setSyslogServerIpAddress(syslogServerIpAddress); EditLogConfigurationResponse resetResponse = wr.editLogConfiguration(resetRequest); Assert.assertNotNull(resetResponse); } }