CumulusLacpGroupHelper { public boolean deleteSwitchPortFromLacpGroup( SwitchNode switchNode, String lacpGroupName, String port ) throws HmsOobNetworkException { SwitchLacpGroup lag = null; try { lag = getSwitchLacpGroup( switchNode, lacpGroupName ); } catch ( HmsObjectNotFoundException e ) { return true; } catch ( HmsException e ) { throw new HmsOobNetworkException( e.getMessage(), e, HmsOobNetworkErrorCode.GET_OPERATION_FAILED ); } if ( lag == null ) { throw new HmsOobNetworkException( "LACP group " + lacpGroupName + " could not be found.", null, HmsOobNetworkErrorCode.GET_OPERATION_FAILED ); } logger.info( "Deleting LACP group " + lacpGroupName + " on switch " + switchNode.getSwitchId() ); CumulusTorSwitchSession session = (CumulusTorSwitchSession) CumulusUtil.getSession( switchNode ); Configuration configuration = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); session.download( baos, CumulusConstants.INTERFACES_FILE ); configuration = Configuration.parse( new ByteArrayInputStream( baos.toByteArray() ) ); } catch ( Exception e ) { logger.error( "Exception received while reading or parsing interfaces file", e ); throw new HmsOobNetworkException( "Exception received while reading or parsing interfaces file", e, HmsOobNetworkErrorCode.DOWNLOAD_FAILED ); } deleteSwitchPortFromLacpGroup( lag, Arrays.asList( port ), configuration ); SshExecResult result; try { ByteArrayInputStream bais = new ByteArrayInputStream( configuration.getString().getBytes() ); CumulusUtil.uploadAsRoot( switchNode, bais, CumulusConstants.INTERFACES_FILE ); CumulusUtil.configurePersistenceDirectory( switchNode ); result = session.executeEnhanced( CumulusConstants.RELOAD_INTERFACES.replaceAll( "\\{password\\}", CumulusUtil.qr( switchNode.getPassword() ) ) ); result.logIfError( logger ); } catch ( HmsException e ) { throw new HmsOobNetworkException( e.getMessage(), e, HmsOobNetworkErrorCode.UPLOAD_FAILED ); } return true; } CumulusLacpGroupHelper( CumulusTorSwitchService service ); List<String> getSwitchLacpGroups( SwitchNode switchNode ); SwitchLacpGroup getSwitchLacpGroup( SwitchNode switchNode, String lacpGroupName ); boolean deleteLacpGroup( SwitchNode switchNode, String lacpGroupName ); Configuration deleteLacpGroup( SwitchLacpGroup lag, Configuration configuration ); boolean deleteSwitchPortFromLacpGroup( SwitchNode switchNode, String lacpGroupName, String port ); void deleteSwitchPortFromLacpGroup( SwitchLacpGroup lag, List<String> portList, Configuration configuration );  }
@Test public void deletePortFromLag() { CumulusLacpGroupHelper lacpHelper = new CumulusLacpGroupHelper( null ); Configuration configuration = Configuration.parse( interfacesFileWithBondAnd2Ports ); ConfigBlock cb = configuration.getConfigBlock( "bond47" ); Bond bond = (Bond) cb; SwitchLacpGroup lag = new SwitchLacpGroup(); lag.setName( cb.name ); lag.setPorts( Arrays.asList( "swp47", "swp48" ) ); try { lacpHelper.deleteSwitchPortFromLacpGroup( lag, Arrays.asList( "swp47" ), configuration ); } catch ( HmsException e ) { fail( "Failed to delete port from LACP group. Reason: " + e.getMessage() ); } int numMatch = 0; for ( SwitchPort switchPort : bond.slaves ) { if ( switchPort.name.equals( "swp47" ) ) fail( "Failed to delete port swp47 from LACP Group bond47" ); else if ( switchPort.name.equals( "swp48" ) ) ++numMatch; } if ( numMatch != 1 ) { fail( "Deleted all ports from the Bond bond47 incorrectly!!!" ); } SwitchPort portSwp47 = configuration.bridges.get( 0 ).getMemberByName( "swp47" ); if ( portSwp47 == null || configuration.bridges.get( 0 ).getMemberByName( "bond47" ) == null ) { fail( "VLAN aware bridge settings are not proper. Either the deleted port or the bond is absent from its configuration" ); } if ( portSwp47.vlans != cb.vlans && !portSwp47.vlans.contains( cb.vlans ) ) { fail( "VLANs of the parent LACP bond have not propagated to the deleted port" ); } if ( !checkCumulusConfigurationSanctity( configuration ) ) fail( "Corrupted the configuration because of which at least 1 port or a bond is present more than once." ); System.out.println( configuration.getString() ); }
@Test public void deleteAllPortsFromLag() { CumulusLacpGroupHelper lacpHelper = new CumulusLacpGroupHelper( null ); Configuration configuration = Configuration.parse( interfacesFileWithBondAnd2Ports ); ConfigBlock cb = configuration.getConfigBlock( "bond47" ); Bond bond = (Bond) cb; List<String> bondVlans = bond.vlans; SwitchLacpGroup lag = new SwitchLacpGroup(); lag.setName( cb.name ); lag.setPorts( Arrays.asList( "swp47", "swp48" ) ); System.out.println( configuration.getString() ); try { lacpHelper.deleteSwitchPortFromLacpGroup( lag, Arrays.asList( "swp47" ), configuration ); System.out.println( configuration.getString() ); lacpHelper.deleteSwitchPortFromLacpGroup( lag, Arrays.asList( "swp48" ), configuration ); } catch ( HmsException e ) { fail( "Failed to delete port from LACP group. Reason: " + e.getMessage() ); } cb = configuration.getConfigBlock( "bond47" ); if ( cb != null ) { fail( "Incorrectly retained LAG without any slave ports." ); } SwitchPort portSwp47 = configuration.bridges.get( 0 ).getMemberByName( "swp47" ); SwitchPort portSwp48 = configuration.bridges.get( 0 ).getMemberByName( "swp48" ); if ( portSwp47 == null || portSwp48 == null || configuration.bridges.get( 0 ).getMemberByName( "bond47" ) != null ) { fail( "VLAN aware bridge settings are not proper. Either deleted ports are absent or the LAG is present" ); } if ( portSwp47.vlans != bondVlans && !portSwp47.vlans.contains( bondVlans ) ) { fail( "VLANs of the parent LACP bond have not propagated to the deleted port" ); } if ( portSwp48.vlans != bondVlans && !portSwp48.vlans.contains( bondVlans ) ) { fail( "VLANs of the parent LACP bond have not propagated to the deleted port" ); } if ( !checkCumulusConfigurationSanctity( configuration ) ) fail( "Corrupted the configuration because of which at least 1 port or a bond is present more than once." ); System.out.println( configuration.getString() ); }