public static CreateOrUpdateBond fromBond(Bond bond) { CreateOrUpdateBond createOrUpdateBond = new CreateOrUpdateBond(); createOrUpdateBond.setId(bond.getId()); createOrUpdateBond.setName(bond.getName()); createOrUpdateBond.setSlaves(new HashSet<>(bond.getSlaves())); createOrUpdateBond.setBondOptions(bond.getBondOptions()); return createOrUpdateBond; }
public Bond toBond() { Bond bond = new Bond(); bond.setId(getId()); bond.setName(getName()); bond.setSlaves(new ArrayList<>(getSlaves())); bond.setBondOptions(getBondOptions()); return bond; }
@Override protected ToStringBuilder appendAttributes(ToStringBuilder tsb) { return super.appendAttributes(tsb) .append("macAddress", getMacAddress()) .append("bondOptions", getBondOptions()) .append("labels", getLabels()) .append("slaves", getSlaves()) .append("activeSlave", getActiveSlave()); }
public static void fillBondSlaves(Collection<? extends VdsNetworkInterface> nics) { Map<String, List<String>> bondToSlaves = getBondNameToBondSlaveNamesMap(nics); for (VdsNetworkInterface nic : nics) { if (nic instanceof Bond) { Bond bond = (Bond) nic; bond.setSlaves(bondToSlaves.containsKey(bond.getName()) ? bondToSlaves.get(bond.getName()) : new ArrayList<String>()); } } }
@Override public int hashCode() { return Objects.hash( super.hashCode(), getSlaves(), getActiveSlave() ); }
@Mapping(from = HostNic.class, to = Bond.class) public static Bond map(HostNic model, Bond template) { Bond entity = template == null ? new Bond() : template; if (model.isSetId()) { entity.setId(GuidUtils.asGuid(model.getId())); } if (model.isSetName()) { entity.setName(model.getName()); } if (model.isSetBonding()) { entity.setBonded(true); if (model.getBonding().isSetSlaves()) { entity.getSlaves().clear(); for (HostNic slave : model.getBonding().getSlaves().getHostNics()) { if (slave.isSetName()) { entity.getSlaves().add(slave.getName()); } } } if (model.getBonding().isSetOptions()) { entity.setBondOptions(calculateBondingOptionsString(model)); } } if (model.isSetQos()) { entity.setQos((HostNetworkQos) QosMapper.map(model.getQos(), null)); } return entity; }
/** * Create the correct type according to the row. * * @param rs The row representing the entity. * @return The instance of the correct type as represented by the row. */ private VdsNetworkInterface createInterface(ResultSet rs) throws SQLException { Integer vlanId = (Integer) rs.getObject("vlan_id"); Boolean isBond = (Boolean) rs.getObject("is_bond"); if (Boolean.TRUE.equals(isBond)) { Bond bond = new Bond(); bond.setBonded(isBond); bond.setBondOptions(rs.getString("bond_opts")); bond.setBondType((Integer) rs.getObject("bond_type")); bond.setActiveSlave(rs.getString("bond_active_slave")); return bond; } if (vlanId != null) { Vlan vlan = new Vlan(); vlan.setVlanId(vlanId); vlan.setBaseInterface(rs.getString("base_interface")); vlan.setBonded(isBond); return vlan; } Nic nic = new Nic(); nic.setBondName(rs.getString("bond_name")); nic.setBonded(isBond); return nic; } };
@Test public void testBondMapping() { HostNic model = new HostNic(); model.setId(Guid.newGuid().toString()); model.setName(RandomUtils.instance().nextString(10)); model.setBonding(new Bonding()); model.getBonding().setSlaves(new HostNics()); HostNic slaveA = new HostNic(); slaveA.setName(RandomUtils.instance().nextString(10)); model.getBonding().getSlaves().getHostNics().add(slaveA); Bond entity = HostNicMapper.map(model, null); assertNotNull(entity); assertEquals(model.getId(), entity.getId().toString()); assertEquals(model.getName(), entity.getName()); assertEquals(model.getBonding().getSlaves().getHostNics().size(), entity.getSlaves().size()); for (HostNic slave : model.getBonding().getSlaves().getHostNics()) { assertTrue(entity.getSlaves().contains(slave.getName())); } }
public boolean equalToBond(Bond bond) { return Objects.equals(getId(), bond.getId()) && Objects.equals(getName(), bond.getName()) && Objects.equals(getBondOptions(), bond.getBondOptions()) && bondsHasSameSlaves(bond); }
@Test public void testBreakingExistingBond() { CreateOrUpdateBond createOrUpdateBond = CreateOrUpdateBond.fromBond(existingBond); dataFromHostSetupNetworksModel.getBonds().add(createOrUpdateBond); when(bondNetworkInterfaceModelA.getItems()).thenReturn(Collections.emptyList()); when(bondNetworkInterfaceModelA.getCreateOrUpdateBond()).thenReturn(createOrUpdateBond); NetworkOperation.BREAK_BOND.getTarget().executeNetworkCommand( bondNetworkInterfaceModelA, null, dataFromHostSetupNetworksModel); assertThat(dataFromHostSetupNetworksModel.getNetworkAttachments().size(), is(0)); assertThat(dataFromHostSetupNetworksModel.getRemovedNetworkAttachments().size(), is(0)); assertThat(dataFromHostSetupNetworksModel.getBonds().size(), is(0)); assertThat(dataFromHostSetupNetworksModel.getRemovedBonds().size(), is(1)); Guid removedBondId = dataFromHostSetupNetworksModel.getRemovedBonds().iterator().next(); assertThat("id mismatch", removedBondId, is(existingBond.getId())); //$NON-NLS-1$ }
private boolean bondsHasSameSlaves(Bond existingNic) { Collection<String> slavesOfBondFromRequest = replaceNullWithEmptyList(getSlaves()); Collection<String> slavesOfExistingBond = replaceNullWithEmptyList(existingNic.getSlaves()); //bonds can be in any order, and I don't want to change this order during this check. return slavesOfBondFromRequest.size() == slavesOfExistingBond.size() && slavesOfBondFromRequest.containsAll(slavesOfExistingBond); }
protected HostNic addActiveSlaveLink(HostNic nic, VdsNetworkInterface iface, List<VdsNetworkInterface> ifaces) { if (iface instanceof Bond) { Bond bond = (Bond) iface; if(nic.getBonding() == null) { nic.setBonding(new Bonding()); } for (VdsNetworkInterface i : ifaces) { if (i.getName().equals(bond.getActiveSlave())) { nic.getBonding().setActiveSlave(slave(i.getId().toString())); break; } } } return nic; }
public Bond() { setBonded(true); }
entity = template; } else if (model.isSetBonding()) { entity = new Bond(); } else if (model.isSetVlan()) { entity = new Vlan();
public Bond(String name) { this(); setName(name); }
@Test public void testBreakingExistingBondWithNetworkAttached() { addBondToParamsAndModel(existingBond, bondNetworkInterfaceModelA, Collections.singletonList(logicalNetworkModelOfNetworkA)); NetworkAttachment networkAttachment = createAttachmentOnNetworkModelAndUpdateParams(bondNetworkInterfaceModelA, logicalNetworkModelOfNetworkA); NetworkOperation.BREAK_BOND.getTarget().executeNetworkCommand( bondNetworkInterfaceModelA, null, dataFromHostSetupNetworksModel); assertThat(dataFromHostSetupNetworksModel.getNetworkAttachments().size(), is(0)); assertThat(dataFromHostSetupNetworksModel.getRemovedNetworkAttachments().size(), is(1)); Guid removedNetworkAttachmentId = dataFromHostSetupNetworksModel.getRemovedNetworkAttachments().iterator().next(); assertThat("id mismatch", removedNetworkAttachmentId, is(networkAttachment.getId())); //$NON-NLS-1$ assertThat(dataFromHostSetupNetworksModel.getBonds().size(), is(0)); assertThat(dataFromHostSetupNetworksModel.getRemovedBonds().size(), is(1)); Guid removedBondId = dataFromHostSetupNetworksModel.getRemovedBonds().iterator().next(); assertThat("id mismatch", removedBondId, is(existingBond.getId())); //$NON-NLS-1$ }
@Test public void testRemoveSlaveFromBond() { existingBond.getSlaves().add(nicC.getName()); addBondToParamsAndModel(existingBond, bondNetworkInterfaceModelA, Collections.emptyList()); when(bondNetworkInterfaceModelA.getSlaves()) .thenReturn(Arrays.asList(networkInterfaceModelOfNicA, networkInterfaceModelOfNicB, networkInterfaceModelOfNicC)); NetworkAttachment networkAttachment = createAttachmentOnNetworkModelAndUpdateParams(bondNetworkInterfaceModelA, logicalNetworkModelOfNetworkA); when(networkInterfaceModelOfNicA.getBond()).thenReturn(bondNetworkInterfaceModelA); NetworkOperation.REMOVE_FROM_BOND.getTarget().executeNetworkCommand( networkInterfaceModelOfNicA, null, dataFromHostSetupNetworksModel); // related network attachment will be updated, not removed and created new one. assertThat(dataFromHostSetupNetworksModel.getNetworkAttachments().size(), is(1)); Guid networkAttachmentId = dataFromHostSetupNetworksModel.getNetworkAttachments().iterator().next().getId(); assertThat("id mismatch", networkAttachmentId, is(networkAttachment.getId())); //$NON-NLS-1$ assertThat(dataFromHostSetupNetworksModel.getRemovedNetworkAttachments().size(), is(0)); assertThat(dataFromHostSetupNetworksModel.getBonds().size(), is(1)); CreateOrUpdateBond newOrModifiedBond = dataFromHostSetupNetworksModel.getBonds().iterator().next(); assertBond(newOrModifiedBond, existingBondId, Arrays.asList(nicB, nicC)); assertThat(dataFromHostSetupNetworksModel.getRemovedBonds().size(), is(0)); }
.addValue("ad_partner_mac", nic.getAdPartnerMac()) .addValue("ad_aggregator_id", nic.getAdAggregatorId()) .addValue("bond_active_slave", nic.isBond() ? ((Bond)nic).getActiveSlave() : null) .addValue("reported_switch_type", nic.getReportedSwitchType() == null ? null : nic.getReportedSwitchType().getOptionValue());
@Test public void testRemoveSlaveFromBondWithTwoSlaves() { addBondToParamsAndModel(existingBond, bondNetworkInterfaceModelA, Collections.emptyList()); when(bondNetworkInterfaceModelA.getSlaves()) .thenReturn(Arrays.asList(networkInterfaceModelOfNicA, networkInterfaceModelOfNicB)); when(bondNetworkInterfaceModelA.getItems()).thenReturn(Collections.singletonList(logicalNetworkModelOfNetworkA)); NetworkAttachment networkAttachment = createAttachmentOnNetworkModelAndUpdateParams(bondNetworkInterfaceModelA, logicalNetworkModelOfNetworkA); when(networkInterfaceModelOfNicA.getBond()).thenReturn(bondNetworkInterfaceModelA); NetworkOperation.REMOVE_FROM_BOND.getTarget().executeNetworkCommand( networkInterfaceModelOfNicA, null, dataFromHostSetupNetworksModel); // related network attachment will be updated, not removed and created new one. assertThat(dataFromHostSetupNetworksModel.getNetworkAttachments().size(), is(0)); assertThat(dataFromHostSetupNetworksModel.getRemovedNetworkAttachments().size(), is(1)); Guid removedNetworkAttachmentId = dataFromHostSetupNetworksModel.getRemovedNetworkAttachments().iterator().next(); assertThat("id mismatch", removedNetworkAttachmentId, is(networkAttachment.getId())); //$NON-NLS-1$ assertThat(dataFromHostSetupNetworksModel.getBonds().size(), is(0)); assertThat(dataFromHostSetupNetworksModel.getRemovedBonds().size(), is(1)); Guid removedBondId = dataFromHostSetupNetworksModel.getRemovedBonds().iterator().next(); assertThat("id mismatch", removedBondId, is(existingBond.getId())); //$NON-NLS-1$ }