@PostConstruct protected void init() { AllFieldsSearch = createSearchBuilder(); AllFieldsSearch.and("instance", AllFieldsSearch.entity().getInstanceId(), Op.EQ); AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ); AllFieldsSearch.and("gateway", AllFieldsSearch.entity().getIPv4Gateway(), Op.EQ); AllFieldsSearch.and("vmType", AllFieldsSearch.entity().getVmType(), Op.EQ); AllFieldsSearch.and("address", AllFieldsSearch.entity().getIPv4Address(), Op.LIKE); AllFieldsSearch.and("isDefault", AllFieldsSearch.entity().isDefaultNic(), Op.EQ); AllFieldsSearch.and("broadcastUri", AllFieldsSearch.entity().getBroadcastUri(), Op.EQ); AllFieldsSearch.and("secondaryip", AllFieldsSearch.entity().getSecondaryIp(), Op.EQ); AllFieldsSearch.and("nicid", AllFieldsSearch.entity().getId(), Op.EQ); AllFieldsSearch.and("strategy", AllFieldsSearch.entity().getReservationStrategy(), Op.EQ); AllFieldsSearch.and("reserverName",AllFieldsSearch.entity().getReserver(),Op.EQ); AllFieldsSearch.and("macAddress", AllFieldsSearch.entity().getMacAddress(), Op.EQ); AllFieldsSearch.done(); IpSearch.select(null, Func.DISTINCT, IpSearch.entity().getIPv4Address()); IpSearch.and("network", IpSearch.entity().getNetworkId(), Op.EQ); IpSearch.and("address", IpSearch.entity().getIPv4Address(), Op.NNULL); IpSearch.done(); NonReleasedSearch.and("instance", NonReleasedSearch.entity().getInstanceId(), Op.EQ); NonReleasedSearch.and("network", NonReleasedSearch.entity().getNetworkId(), Op.EQ); NonReleasedSearch.and("state", NonReleasedSearch.entity().getState(), Op.NOTIN); NonReleasedSearch.done(); deviceIdSearch.select(null, Func.DISTINCT, deviceIdSearch.entity().getDeviceId()); deviceIdSearch.and("instance", deviceIdSearch.entity().getInstanceId(), Op.EQ); deviceIdSearch.done();
public VspNic buildVspNic(NicVO nic) { return buildVspNic(nic.getUuid(), nic.getMacAddress(), nic.getIPv4Address(), nic.getNetworkId(), null); }
private boolean isLastNicInSubnet(final NicVO nic) { if (_nicDao.listByNetworkIdTypeAndGatewayAndBroadcastUri(nic.getNetworkId(), VirtualMachine.Type.User, nic.getIPv4Gateway(), nic.getBroadcastUri()).size() > 1) { return false; } return true; }
/** * Swaps the UUID's of the given nics's * @param oldNic * @param newNic */ private void swapUuids(NicVO oldNic, NicVO newNic) { final String realUuid = oldNic.getUuid(); final String dummyUuid = newNic.getUuid(); oldNic.setUuid(dummyUuid.replace("-", "+")); newNic.setUuid(realUuid); _nicDao.persist(oldNic); _nicDao.persist(newNic); oldNic.setUuid(dummyUuid); _nicDao.persist(oldNic); }
protected NicTO toNicTO(final NicVO nic, final NicProfile profile, final NetworkVO config) { final NicTO to = new NicTO(); to.setDeviceId(nic.getDeviceId()); to.setBroadcastType(config.getBroadcastDomainType()); to.setType(config.getTrafficType()); to.setIp(nic.getIPv4Address()); to.setNetmask(nic.getIPv4Netmask()); to.setMac(nic.getMacAddress()); to.setDns1(profile.getIPv4Dns1()); to.setDns2(profile.getIPv4Dns2()); if (nic.getIPv4Gateway() != null) { to.setGateway(nic.getIPv4Gateway()); } else { to.setGateway(config.getGateway()); } if (nic.getVmType() != VirtualMachine.Type.User) { to.setPxeDisable(true); } to.setDefaultNic(nic.isDefaultNic()); to.setBroadcastUri(nic.getBroadcastUri()); to.setIsolationuri(nic.getIsolationUri()); if (profile != null) { to.setDns1(profile.getIPv4Dns1()); to.setDns2(profile.getIPv4Dns2()); } final Integer networkRate = _networkModel.getNetworkRate(config.getId(), null); to.setNetworkRateMbps(networkRate); to.setUuid(config.getUuid()); return to; }
if (nicVO.getVmType() != VirtualMachine.Type.User) { throw new InvalidParameterValueException("The nic is not belongs to user vm"); UserVm vm = _vmDao.findById(nicVO.getInstanceId()); if (vm == null) { throw new InvalidParameterValueException("There is no vm with the nic"); Network network = _networkDao.findById(nicVO.getNetworkId()); if (network == null) { throw new InvalidParameterValueException("There is no network with the nic"); ipaddr = _ipAddrMgr.allocateGuestIP(network, ipaddr); } catch (InsufficientAddressCapacityException e) { throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, for insufficient address capacity"); throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip"); IPAddressVO oldIP = _ipAddressDao.findByAssociatedVmId(vm.getId()); if (oldIP != null) { oldIP.setVmIp(nicVO.getIPv4Address()); _ipAddressDao.persist(oldIP); ipaddr = _ipAddrMgr.allocatePublicIpForGuestNic(network, podId, ipOwner, ipaddr); if (ipaddr == null) { throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip"); nicVO.setIPv4Gateway(vlan.getVlanGateway()); nicVO.setIPv4Netmask(vlan.getVlanNetmask());
public void createDhcpEntryCommand(final VirtualRouter router, final UserVm vm, final NicVO nic, final Commands cmds) { final DhcpEntryCommand dhcpCommand = new DhcpEntryCommand(nic.getMacAddress(), nic.getIPv4Address(), vm.getHostName(), nic.getIPv6Address(), _networkModel.getExecuteInSeqNtwkElmtCmd()); String gatewayIp = nic.getIPv4Gateway(); final DataCenterVO dcVo = _dcDao.findById(router.getDataCenterId()); dhcpCommand.setDefaultRouter(gatewayIp); dhcpCommand.setIp6Gateway(nic.getIPv6Gateway()); String ipaddress = null; final NicVO domrDefaultNic = findDefaultDnsIp(vm.getId()); if (domrDefaultNic != null) { ipaddress = domrDefaultNic.getIPv4Address(); } dhcpCommand.setDefaultDns(ipaddress); dhcpCommand.setDuid(NetUtils.getDuidLL(nic.getMacAddress())); dhcpCommand.setDefault(nic.isDefaultNic()); dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId())); dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, _routerControlHelper.getRouterIpInNetwork(nic.getNetworkId(), router.getId())); dhcpCommand.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString()); cmds.addCommand("dhcp", dhcpCommand); }
if (nic.getReservationStrategy() == Nic.ReservationStrategy.Start) { nic.setState(Nic.State.Reserving); nic.setReservationId(context.getReservationId()); _nicDao.update(nic.getId(), nic); URI broadcastUri = nic.getBroadcastUri(); if (broadcastUri == null) { broadcastUri = network.getBroadcastUri(); final URI isolationUri = nic.getIsolationUri(); nic.setIPv4Address(profile.getIPv4Address()); nic.setAddressFormat(profile.getFormat()); nic.setIPv6Address(profile.getIPv6Address()); nic.setIPv6Cidr(profile.getIPv6Cidr()); nic.setIPv6Gateway(profile.getIPv6Gateway()); nic.setMacAddress(profile.getMacAddress()); nic.setIsolationUri(profile.getIsolationUri()); nic.setBroadcastUri(profile.getBroadCastUri()); nic.setReserver(guru.getName()); nic.setState(Nic.State.Reserved); nic.setIPv4Netmask(profile.getIPv4Netmask()); nic.setIPv4Gateway(profile.getIPv4Gateway()); nic.setReservationStrategy(profile.getReservationStrategy()); profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vmProfile.getHypervisorType(), network)); guru.updateNicProfile(profile, network); nic.setState(Nic.State.Reserved);
NetworkVO guestNetwork = null; for (NicVO nic : nics) { NetworkVO network = _networkDao.findById(nic.getNetworkId()); long isDefault = (nic.isDefaultNic()) ? 1 : 0; UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), Long.toString(nic.getId()), network.getNetworkOfferingId(), null, isDefault, VirtualMachine.class.getName(), vm.getUuid(), vm.isDisplay()); if (network.getTrafficType() == TrafficType.Guest) { originalIp = nic.getIPv4Address(); guestNic = nic; guestNetwork = network; if (nic.getBroadcastUri().getScheme().equals("pvlan")) { NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), 0, false, "pvlan-nic"); if (!setupVmForPvlan(true, hostId, nicProfile)) { return false; if (originalIp != null && !originalIp.equalsIgnoreCase(returnedIp)) { if (returnedIp != null && guestNic != null) { guestNic.setIPv4Address(returnedIp); ipChanged = true; guestNic.setIPv4Address(returnedIp); ipChanged = true; _nicDao.update(guestNic.getId(), guestNic); userVm.setPrivateIpAddress(guestNic.getIPv4Address()); _vmDao.update(userVm.getId(), userVm);
sc_nic.addAnd("macAddress", SearchCriteria.Op.EQ, vmNetworkStat.getMacAddress()); NicVO nic = _nicDao.search(sc_nic, null).get(0); List<VlanVO> vlan = _vlanDao.listVlansByNetworkId(nic.getNetworkId()); if (vlan == null || vlan.size() == 0 || vlan.get(0).getVlanType() != VlanType.DirectAttached) continue; // only get network statistics for DirectAttached network (shared networks in Basic zone and Advanced zone with/without SG) UserStatisticsVO previousvmNetworkStats = _userStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), nic.getNetworkId(), nic.getIPv4Address(), vmId, "UserVm"); if (previousvmNetworkStats == null) { previousvmNetworkStats = new UserStatisticsVO(userVm.getAccountId(), userVm.getDataCenterId(), nic.getIPv4Address(), vmId, "UserVm", nic.getNetworkId()); _userStatsDao.persist(previousvmNetworkStats); UserStatisticsVO vmNetworkStat_lock = _userStatsDao.lock(userVm.getAccountId(), userVm.getDataCenterId(), nic.getNetworkId(), nic.getIPv4Address(), vmId, "UserVm"); + " and nicId:" + nic.getId()); continue;
final Nic.State originalState = nic.getState(); final NetworkVO network = _networksDao.findById(nic.getNetworkId()); if (nic.getReservationStrategy() == Nic.ReservationStrategy.Start) { final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName()); nic.setState(Nic.State.Releasing); _nicDao.update(nic.getId(), nic); final NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkModel .isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vmProfile.getHypervisorType(), network)); if (guru.release(profile, vmProfile, nic.getReservationId())) { applyProfileToNicForRelease(nic, profile); nic.setState(Nic.State.Allocated); if (originalState == Nic.State.Reserved) { updateNic(nic, network.getId(), -1); } else { _nicDao.update(nic.getId(), nic); nic.setState(Nic.State.Allocated); updateNic(nic, network.getId(), -1);
protected String getInternalLbControlIp(final long internalLbVmId) { String controlIpAddress = null; final List<NicVO> nics = _nicDao.listByVmId(internalLbVmId); for (final NicVO nic : nics) { final Network ntwk = _ntwkModel.getNetwork(nic.getNetworkId()); if (ntwk.getTrafficType() == TrafficType.Control) { controlIpAddress = nic.getIPv4Address(); } } if (controlIpAddress == null) { s_logger.warn("Unable to find Internal LB control ip in its attached NICs!. Internal LB vm: " + internalLbVmId); final DomainRouterVO internalLbVm = _internalLbVmDao.findById(internalLbVmId); return internalLbVm.getPrivateIpAddress(); } return controlIpAddress; }
NetworkVO network = _networkDao.findById(nic.getNetworkId()); if (network == null) { throw new InvalidParameterValueException("unable to find a network with id " + nic.getNetworkId()); if (nic.getInstanceId() != vmId) { throw new InvalidParameterValueException(nic + " is not a nic on " + vmInstance); if (nic.isDefaultNic()) { throw new CloudRuntimeException("refusing to set default nic because chosen nic is already the default"); Integer chosenID = nic.getDeviceId(); Integer existingID = existing.getDeviceId(); nic.setDefaultNic(true); nic.setDeviceId(existingID); existingVO.setDefaultNic(false); existingVO.setDeviceId(chosenID); nic.setDefaultNic(false); nic.setDeviceId(chosenID); existingVO.setDefaultNic(true); existingVO.setDeviceId(existingID); } else if (newdefault.getId() == nic.getNetworkId()) { s_logger.debug("successfully set default network to " + network + " for " + vmInstance); String nicIdString = Long.toString(nic.getId()); long newNetworkOfferingId = network.getNetworkOfferingId(); UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vmInstance.getAccountId(), vmInstance.getDataCenterId(), vmInstance.getId(),
TopologyData.Port p = topo.new Port(); p.setAttachmentInfo(netData.new AttachmentInfo(nic.getUuid(),nic.getMacAddress())); VMInstanceVO vm = _vmDao.findById(nic.getInstanceId()); HostVO host = _hostDao.findById(vm.getHostId()); ipList.add(new AttachmentData().getAttachment().new IpAddress(nic.getIPv4Address())); p.setIpAddresses(ipList); p.setId(nic.getUuid()); p.setMac(nic.getMacAddress());
@Override public boolean prepareCreateTemplate(Long pxeServerId, UserVm vm, String templateUrl) { List<NicVO> nics = _nicDao.listByVmId(vm.getId()); if (nics.size() != 1) { throw new CloudRuntimeException("Wrong nic number " + nics.size() + " of vm " + vm.getId()); } /* use last host id when VM stopped */ Long hostId = (vm.getHostId() == null ? vm.getLastHostId() : vm.getHostId()); HostVO host = _hostDao.findById(hostId); DataCenterVO dc = _dcDao.findById(host.getDataCenterId()); NicVO nic = nics.get(0); String mask = nic.getIPv4Netmask(); String mac = nic.getMacAddress(); String ip = nic.getIPv4Address(); String gateway = nic.getIPv4Gateway(); String dns = dc.getDns1(); if (dns == null) { dns = dc.getDns2(); } try { PrepareCreateTemplateCommand cmd = new PrepareCreateTemplateCommand(ip, mac, mask, gateway, dns, templateUrl); Answer ans = _agentMgr.send(pxeServerId, cmd); return ans.getResult(); } catch (Exception e) { s_logger.debug("Prepare for creating baremetal template failed", e); return false; } }
private NicVO getPlaceholderNic(Network network) { List<NicVO> guestIps = _nicDao.listByNetworkId(network.getId()); for (NicVO guestIp : guestIps) { // only external firewall and external load balancer will create NicVO with PlaceHolder reservation strategy if (guestIp.getReservationStrategy().equals(ReservationStrategy.PlaceHolder) && guestIp.getVmType() == null && guestIp.getReserver() == null && !guestIp.getIPv4Address().equals(network.getGateway())) { return guestIp; } } return null; }
@Override public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) { UserVmVO userVm = _vmDao.findById(profile.getId()); List<NicVO> nics = _nicDao.listByVmId(userVm.getId()); for (NicVO nic : nics) { NetworkVO network = _networkDao.findById(nic.getNetworkId()); if (network.getTrafficType() == TrafficType.Guest || network.getTrafficType() == TrafficType.Public) { userVm.setPrivateIpAddress(nic.getIPv4Address()); userVm.setPrivateMacAddress(nic.getMacAddress()); _vmDao.update(userVm.getId(), userVm); } } List<VolumeVO> volumes = _volsDao.findByInstance(userVm.getId()); VmDiskStatisticsVO diskstats = null; for (VolumeVO volume : volumes) { diskstats = _vmDiskStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId()); if (diskstats == null) { diskstats = new VmDiskStatisticsVO(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId()); _vmDiskStatsDao.persist(diskstats); } } finalizeCommandsOnStart(cmds, profile); return true; }
/** * Recreates UserVmSearch depending on network type, as nics on L2 networks have no ip addresses * @param network network */ private void recreateUserVmSeach(NetworkVO network) { if (network != null) { SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder(); nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); nicSearch.and("removed", nicSearch.entity().getRemoved(), SearchCriteria.Op.NULL); if (!Network.GuestType.L2.equals(network.getGuestType())) { nicSearch.and().op("ip4Address", nicSearch.entity().getIPv4Address(), SearchCriteria.Op.NNULL); nicSearch.or("ip6Address", nicSearch.entity().getIPv6Address(), SearchCriteria.Op.NNULL); nicSearch.cp(); } UserVmSearch = createSearchBuilder(); UserVmSearch.and("states", UserVmSearch.entity().getState(), SearchCriteria.Op.IN); UserVmSearch.join("nicSearch", nicSearch, UserVmSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER); UserVmSearch.done(); } }
@Override public String getIpOfNetworkElementInVirtualNetwork(long accountId, long dataCenterId) { List<NetworkVO> virtualNetworks = _networksDao.listByZoneAndGuestType(accountId, dataCenterId, GuestType.Isolated, false); if (virtualNetworks.isEmpty()) { s_logger.trace("Unable to find default Virtual network account id=" + accountId); return null; } NetworkVO virtualNetwork = virtualNetworks.get(0); NicVO networkElementNic = _nicDao.findByNetworkIdAndType(virtualNetwork.getId(), Type.DomainRouter); if (networkElementNic != null) { return networkElementNic.getIPv4Address(); } else { s_logger.warn("Unable to set find network element for the network id=" + virtualNetwork.getId()); return null; } }
if (nic.getReservationStrategy() == Nic.ReservationStrategy.Start && nic.getState() != Nic.State.Allocated) { releaseNic(vm, nic.getId()); } catch (final Exception ex) { s_logger.warn("Failed to release nic: " + nic.toString() + " as part of remove operation due to", ex); nic.setState(Nic.State.Deallocating); _nicDao.update(nic.getId(), nic); final NetworkVO network = _networksDao.findById(nic.getNetworkId()); final NicProfile profile = new NicProfile(nic, network, null, null, null, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag( vm.getHypervisorType(), network)); if (nic.getReservationStrategy() == Nic.ReservationStrategy.Create) { final List<Provider> providersToImplement = getNetworkProviders(network.getId()); for (final NetworkElement element : networkElements) { element.release(network, profile, vm, null); } catch (final ConcurrentOperationException ex) { s_logger.warn("release failed during the nic " + nic.toString() + " removeNic due to ", ex); } catch (final ResourceUnavailableException ex) { s_logger.warn("release failed during the nic " + nic.toString() + " removeNic due to ", ex); _nicDao.remove(nic.getId()); s_logger.debug("Removed nic id=" + nic.getId()); if (!removeVmSecondaryIpsOfNic(nic.getId())) { s_logger.debug("Removing nic " + nic.getId() + " secondary ip addreses failed");