CumulusTorSwitchService implements ISwitchService { @Override public SwitchOsInfo getSwitchOsInfo( SwitchNode switchNode ) { SwitchOsInfo osInfo = osInfoCache.get( switchNode ); if ( osInfo != null ) { return osInfo; } else { osInfo = new SwitchOsInfo(); } CumulusTorSwitchSession switchSession = (CumulusTorSwitchSession) getSession( switchNode ); boolean cacheResult = true; try { SshExecResult result = switchSession.executeEnhanced( CumulusConstants.GET_SWITCH_OS_COMMAND ); result.logIfError( logger ); if ( result.getExitCode() != 0 ) cacheResult = false; String osResult = result.getStdoutAsString(); Properties osProperties = new Properties(); osProperties.load( new StringReader( osResult ) ); osInfo.setOsName( osProperties.getProperty( "NAME" ).replaceAll( "^\"|\"$", "" ) ); osInfo.setOsVersion( osProperties.getProperty( "VERSION_ID" ).replaceAll( "^\"|\"$", "" ) ); } catch ( Exception e ) { logger.warn( "Exception received while gathering OS details.", e ); cacheResult = false; } try { SshExecResult result = switchSession.executeEnhanced( CumulusConstants.LAST_BOOT_TIME_COMMAND ); result.logIfError( logger ); if ( result.getExitCode() != 0 ) cacheResult = false; String lastBootResult = result.getStdoutAsString(); String[] lastBootResultArray = lastBootResult.split( "\n" ); Double secondsSinceEpochString = Double.parseDouble( lastBootResultArray[0] ); Double secondsSinceBoot = Double.parseDouble( lastBootResultArray[1] ); long msBootTime = Math.round( ( secondsSinceEpochString - secondsSinceBoot ) * 1000 ); Date lastBootDate = new Date( msBootTime ); osInfo.setLastBoot( lastBootDate ); } catch ( Exception e ) { logger.warn( "Exception received while getting last boot time.", e ); cacheResult = false; } try { String firmwareVersionCommand = CumulusConstants.GET_FIRMWARE_VER_COMMAND.replaceAll( "\\{password\\}", qr( switchNode.getPassword() ) ); SshExecResult result = switchSession.executeEnhanced( firmwareVersionCommand ); result.logIfError( logger ); if ( result.getExitCode() != 0 ) cacheResult = false; String firmwareVer = result.getStdoutAsString().trim(); osInfo.setFirmwareName( CumulusConstants.FIRMWARE_NAME ); osInfo.setFirmwareVersion( firmwareVer ); } catch ( Exception e ) { logger.warn( "Exception received while gathering firmware details.", e ); cacheResult = false; } if ( cacheResult ) osInfoCache.set( switchNode, osInfo ); return ( osInfo ); }  @Override String getSwitchType(); @Override List<SwitchType> getSupportedSwitchTypes(); @Override boolean discoverSwitch( SwitchNode switchNode ); @Override SwitchSession getSession( SwitchNode switchNode ); @Override boolean isPoweredOn( SwitchNode switchNode ); @Override SwitchOsInfo getSwitchOsInfo( SwitchNode switchNode ); @Override SwitchHardwareInfo getSwitchHardwareInfo( SwitchNode switchNode ); @Override boolean updateSwitchIpAddress( SwitchNode switchNode, String ipAddress, String netmask, String gateway ); @Override boolean setPassword( SwitchNode switchNode, String username, String newPassword ); @Override boolean updateSwitchTimeServer( SwitchNode switchNode, String timeServer ); @Override List<String> getSwitchPortList( SwitchNode switchNode ); @Override SwitchPort getSwitchPort( SwitchNode switchNode, String portName ); @Override PortStatus getSwitchPortStatus( SwitchNode switchNode, String portName ); @Override boolean setSwitchPortStatus( SwitchNode switchNode, String portName, PortStatus portStatus ); @Override boolean updateSwitchPort( SwitchNode switchNode, String portName, SwitchPort portInfo ); @Override SwitchVlan getSwitchVlan( SwitchNode switchNode, String vlanName ); @Override List<String> getSwitchVlans( SwitchNode switchNode ); @Override List<SwitchVlan> getSwitchVlansBulk( SwitchNode switchNode ); @Override boolean createVlan( SwitchNode switchNode, SwitchVlan vlan ); @Override boolean updateVlan( SwitchNode switchNode, String vlanName, SwitchVlan vlan ); @Override boolean deleteVlan( SwitchNode switchNode, String vlanName ); @Override List<String> getSwitchLacpGroups( SwitchNode switchNode ); @Override SwitchLacpGroup getSwitchLacpGroup( SwitchNode switchNode, String lacpGroupName ); @Override boolean createLacpGroup( SwitchNode switchNode, SwitchLacpGroup lacpGroup ); @Override boolean deleteLacpGroup( SwitchNode switchNode, String lacpGroupName ); @Override boolean reboot( SwitchNode switchNode ); @Override boolean upgrade( SwitchNode switchNode, SwitchUpgradeInfo upgradeInfo ); @Deprecated @Override boolean applyNetworkConfiguration( SwitchNode switchNode, SwitchNetworkConfiguration networkConfiguration ); @Override List<SwitchVxlan> getSwitchVxlans( SwitchNode switchNode ); @Override List<SwitchVxlan> getSwitchVxlansMatchingVlan( SwitchNode switchNode, String vlanName ); @Override boolean createVxlan( SwitchNode switchNode, SwitchVxlan vxlan ); @Override boolean deleteVxlan( SwitchNode switchNode, String vxlanName, String vlanName ); @Override void deletePortOrBondFromVlan( SwitchNode switchNode, String vlanId, String portOrBondName ); @Override void deletePortFromLacpGroup( SwitchNode switchNode, String lacpGroupName, String portName ); @Override List<SwitchPort> getSwitchPortListBulk( SwitchNode switchNode ); @Override boolean configureOspf( SwitchNode switchNode, SwitchOspfConfig ospf ); @Override SwitchOspfConfig getOspf( SwitchNode switchNode ); @Override boolean configureBgp( SwitchNode switchNode, SwitchBgpConfig bgp ); @Override SwitchBgpConfig getBgpConfiguration( SwitchNode switchNode ); @Override SwitchSensorInfo getSwitchSensorInfo( SwitchNode switchNode ); @Override boolean configureMclag( SwitchNode switchNode, SwitchMclagInfo mclag ); @Override SwitchMclagInfo getMclagStatus( SwitchNode switchNode ); @Override void applySwitchBulkConfigs( SwitchNode switchNode, List<PluginSwitchBulkConfig> switchBulkConfigs ); @Override void configureIpv4DefaultRoute( SwitchNode switchNode, String gateway, String portId ); @Override void deleteIpv4DefaultRoute( SwitchNode switchNode ); @Override List<ServerComponentEvent> getComponentSwitchEventList( ServiceHmsNode serviceNode,
                                                                   SwitchComponentEnum component ); @Override List<HmsApi> getSupportedHmsSwitchApi( ServiceHmsNode serviceNode ); @Override void setSwitchTime( SwitchNode switchNode, long time ); @Override void configureSnmp( SwitchNode switchNode, SwitchSnmpConfig config ); @Override SwitchSnmpConfig getSnmp( SwitchNode switchNode );  }
@Test public void testInvalidLogin() { String deviceIp = "10.28.197.242"; String deviceUser = "cumulus"; String devicePassword = "BadPassword"; SwitchNode switchNode = new SwitchNode( "S0", "SSH", deviceIp, new Integer( 22 ), deviceUser, devicePassword ); ISwitchService switchService = new CumulusTorSwitchService(); SwitchOsInfo switchOsInfo = null; try { switchOsInfo = switchService.getSwitchOsInfo( switchNode ); if ( switchOsInfo != null ) { System.out.println( "[Device IP : " + deviceIp + "] Found Switch OS information = " + switchOsInfo.getOsName() ); } else { System.out.println( "[Device IP : " + deviceIp + "] No information found for Switch OS." ); } } catch ( Exception excep ) { System.out.println( "Error while getting Switch OS information: " + excep.toString() ); } }
@Test public void testGetSwitchOsInfo() { SwitchOsInfo osInfo = null; try { osInfo = cumulusTorSwitchService_Obj.getSwitchOsInfo( switchNode_Obj ); assertNotNull( "OSInfo is null", osInfo ); if ( osInfo != null ) { logger.info( "[Device IP : " + switchNode_Obj.getIpAddress() + "] Found Switch OS information." ); logger.info( "OS info: " ); logger.info( "FirmwareName: " + osInfo.getFirmwareName() ); logger.info( "FirmwareVersion: " + osInfo.getFirmwareVersion() ); logger.info( "OsName: " + osInfo.getOsName() ); logger.info( "OsVersion: " + osInfo.getOsVersion() ); logger.info( "Class Name: " + osInfo.getClass().getName() ); logger.info( "LastBootTime: " + osInfo.getLastBoot().toString() ); } } catch ( Exception e ) { logger.info( "Error while getting Switch OS information: " + e.toString() ); if ( PRINT_STACK_TRACE ) logger.debug( e.getStackTrace() ); if ( FAIL_ON_EXCEPTION ) fail(); } }