Search in sources :

Example 26 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class IpAddressManagerImpl method applyRules.

@Override
public boolean applyRules(final List<? extends FirewallRule> rules, final FirewallRule.Purpose purpose, final NetworkRuleApplier applier, final boolean continueOnError) throws ResourceUnavailableException {
    if (rules == null || rules.size() == 0) {
        s_logger.debug("There are no rules to forward to the network elements");
        return true;
    }
    boolean success = true;
    final Network network = _networksDao.findById(rules.get(0).getNetworkId());
    final FirewallRuleVO.TrafficType trafficType = rules.get(0).getTrafficType();
    final List<PublicIp> publicIps = new ArrayList<>();
    if (!(rules.get(0).getPurpose() == FirewallRule.Purpose.Firewall && trafficType == FirewallRule.TrafficType.Egress)) {
        // get the list of public ip's owned by the network
        final List<IPAddressVO> userIps = _ipAddressDao.listByAssociatedNetwork(network.getId(), null);
        if (userIps != null && !userIps.isEmpty()) {
            for (final IPAddressVO userIp : userIps) {
                final PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
                publicIps.add(publicIp);
            }
        }
    }
    // the network so as to ensure IP is associated before applying rules (in add state)
    if (checkIfIpAssocRequired(network, false, publicIps)) {
        applyIpAssociations(network, false, continueOnError, publicIps);
    }
    try {
        applier.applyRules(network, purpose, rules);
    } catch (final ResourceUnavailableException e) {
        if (!continueOnError) {
            throw e;
        }
        s_logger.warn("Problems with applying " + purpose + " rules but pushing on", e);
        success = false;
    }
    // This IPAssoc ensures, public IP is dis-associated after last active rule is revoked.
    if (checkIfIpAssocRequired(network, true, publicIps)) {
        applyIpAssociations(network, true, continueOnError, publicIps);
    }
    return success;
}
Also used : PublicIp(com.cloud.network.addr.PublicIp) Network(com.cloud.legacymodel.network.Network) ArrayList(java.util.ArrayList) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) IPAddressVO(com.cloud.network.dao.IPAddressVO) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO)

Example 27 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class Ipv6AddressManagerImpl method assignDirectIp6Address.

@Override
public UserIpv6Address assignDirectIp6Address(final long dcId, final Account owner, final Long networkId, final String requestedIp6) throws InsufficientAddressCapacityException {
    final Network network = _networkDao.findById(networkId);
    if (network == null) {
        return null;
    }
    final List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId);
    if (vlans == null) {
        s_logger.debug("Cannot find related vlan attached to network " + networkId);
        return null;
    }
    String ip = null;
    Vlan ipVlan = null;
    if (requestedIp6 == null) {
        if (!_networkModel.isIP6AddressAvailableInNetwork(networkId)) {
            throw new InsufficientAddressCapacityException("There is no more address available in the network " + network.getName(), DataCenter.class, network.getDataCenterId());
        }
        for (final Vlan vlan : vlans) {
            if (!_networkModel.isIP6AddressAvailableInVlan(vlan.getId())) {
                continue;
            }
            ip = NetUtils.getIp6FromRange(vlan.getIp6Range());
            int count = 0;
            while (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) != null) {
                ip = NetUtils.getNextIp6InRange(ip, vlan.getIp6Range());
                count++;
                // It's an arbitrate number to prevent the infinite loop
                if (count > _ipv6RetryMax) {
                    ip = null;
                    break;
                }
            }
            if (ip != null) {
                ipVlan = vlan;
            }
        }
        if (ip == null) {
            throw new InsufficientAddressCapacityException("Cannot find a usable IP in the network " + network.getName() + " after " + _ipv6RetryMax + "(network.ipv6.search.retry.max) times retry!", DataCenter.class, network.getDataCenterId());
        }
    } else {
        for (final Vlan vlan : vlans) {
            if (NetUtils.isIp6InRange(requestedIp6, vlan.getIp6Range())) {
                ipVlan = vlan;
                break;
            }
        }
        if (ipVlan == null) {
            throw new CloudRuntimeException("Requested IPv6 is not in the predefined range!");
        }
        ip = requestedIp6;
        if (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) != null) {
            throw new CloudRuntimeException("The requested IP is already taken!");
        }
    }
    final Zone zone = zoneRepository.findById(dcId).orElse(null);
    final Long mac = zone.getMacAddress();
    final Long nextMac = mac + 1;
    zone.setMacAddress(nextMac);
    zoneRepository.save(zone);
    final String macAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(mac));
    final UserIpv6AddressVO ipVO = new UserIpv6AddressVO(ip, dcId, macAddress, ipVlan.getId());
    ipVO.setPhysicalNetworkId(network.getPhysicalNetworkId());
    ipVO.setSourceNetworkId(networkId);
    ipVO.setState(UserIpv6Address.State.Allocated);
    ipVO.setDomainId(owner.getDomainId());
    ipVO.setAccountId(owner.getAccountId());
    _ipv6Dao.persist(ipVO);
    return ipVO;
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Zone(com.cloud.db.model.Zone) Network(com.cloud.legacymodel.network.Network) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) Vlan(com.cloud.legacymodel.dc.Vlan) VlanVO(com.cloud.dc.VlanVO)

Example 28 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class NetworkModelImpl method isIP6AddressAvailableInNetwork.

@Override
public boolean isIP6AddressAvailableInNetwork(final long networkId) {
    final Network network = _networksDao.findById(networkId);
    if (network == null) {
        return false;
    }
    if (network.getIp6Gateway() == null) {
        return false;
    }
    final List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId);
    for (final Vlan vlan : vlans) {
        if (isIP6AddressAvailableInVlan(vlan.getId())) {
            return true;
        }
    }
    return false;
}
Also used : Network(com.cloud.legacymodel.network.Network) Vlan(com.cloud.legacymodel.dc.Vlan) VlanVO(com.cloud.dc.VlanVO)

Example 29 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method updateDefaultNicForVirtualMachine.

@Override
@ActionEvent(eventType = EventTypes.EVENT_NIC_UPDATE, eventDescription = "Creating Nic", async = true)
public UserVm updateDefaultNicForVirtualMachine(final UpdateDefaultNicForVMCmd cmd) throws InvalidParameterValueException, CloudRuntimeException {
    final Long vmId = cmd.getVmId();
    final Long nicId = cmd.getNicId();
    final Account caller = CallContext.current().getCallingAccount();
    final UserVmVO vmInstance = _vmDao.findById(vmId);
    if (vmInstance == null) {
        throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
    }
    // Check that Vm does not have VM Snapshots
    if (_vmSnapshotDao.findByVm(vmId).size() > 0) {
        throw new InvalidParameterValueException("NIC cannot be updated for VM with VM Snapshots");
    }
    NicVO nic = _nicDao.findById(nicId);
    if (nic == null) {
        throw new InvalidParameterValueException("unable to find a nic with id " + nicId);
    }
    final NetworkVO network = _networkDao.findById(nic.getNetworkId());
    if (network == null) {
        throw new InvalidParameterValueException("unable to find a network with id " + nic.getNetworkId());
    }
    // Perform permission check on VM
    _accountMgr.checkAccess(caller, null, true, vmInstance);
    // Verify that zone is not Basic
    final Zone zone = zoneRepository.findById(vmInstance.getDataCenterId()).orElse(null);
    if (zone.getNetworkType() == NetworkType.Basic) {
        throw new CloudRuntimeException("Zone " + vmInstance.getDataCenterId() + ", has a NetworkType of Basic. Can't change default NIC on a Basic Network");
    }
    // no need to check permissions for network, we'll enumerate the ones they already have access to
    final Network existingdefaultnet = _networkModel.getDefaultNetworkForVm(vmId);
    // check to see if nic is attached to VM
    if (nic.getInstanceId() != vmId) {
        throw new InvalidParameterValueException(nic + " is not a nic on  " + vmInstance);
    }
    // if current default equals chosen new default, Throw an exception
    if (nic.isDefaultNic()) {
        throw new CloudRuntimeException("refusing to set default nic because chosen nic is already the default");
    }
    // make sure the VM is Running or Stopped
    if (vmInstance.getState() != State.Running && vmInstance.getState() != State.Stopped) {
        throw new CloudRuntimeException("refusing to set default " + vmInstance + " is not Running or Stopped");
    }
    NicProfile existing = null;
    final List<NicProfile> nicProfiles = _networkMgr.getNicProfiles(vmInstance);
    for (final NicProfile nicProfile : nicProfiles) {
        if (nicProfile.isDefaultNic() && existingdefaultnet != null && nicProfile.getNetworkId() == existingdefaultnet.getId()) {
            existing = nicProfile;
        }
    }
    if (existing == null) {
        s_logger.warn("Failed to update default nic, no nic profile found for existing default network");
        throw new CloudRuntimeException("Failed to find a nic profile for the existing default network. This is bad and probably means some sort of configuration corruption");
    }
    NicVO existingVO = _nicDao.findById(existing.id);
    nic.setDefaultNic(true);
    existingVO.setDefaultNic(false);
    nic = _nicDao.persist(nic);
    existingVO = _nicDao.persist(existingVO);
    Network newdefault = _networkModel.getDefaultNetworkForVm(vmId);
    if (newdefault == null) {
        nic.setDefaultNic(false);
        existingVO.setDefaultNic(true);
        nic = _nicDao.persist(nic);
        _nicDao.persist(existingVO);
        newdefault = _networkModel.getDefaultNetworkForVm(vmId);
        if (newdefault.getId() == existingdefaultnet.getId()) {
            throw new CloudRuntimeException("Setting a default nic failed, and we had no default nic, but we were able to set it back to the original");
        }
        throw new CloudRuntimeException("Failed to change default nic to " + nic + " and now we have no default");
    } else if (newdefault.getId() == nic.getNetworkId()) {
        s_logger.debug("successfully set default network to " + network + " for " + vmInstance);
        return _vmDao.findById(vmInstance.getId());
    }
    throw new CloudRuntimeException("something strange happened, new default network(" + newdefault.getId() + ") is not null, and is not equal to the network(" + nic.getNetworkId() + ") of the chosen nic");
}
Also used : Account(com.cloud.legacymodel.user.Account) NetworkVO(com.cloud.network.dao.NetworkVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Zone(com.cloud.db.model.Zone) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PhysicalNetwork(com.cloud.network.PhysicalNetwork) Network(com.cloud.legacymodel.network.Network) ActionEvent(com.cloud.event.ActionEvent)

Example 30 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method setupVmForPvlan.

@Override
public boolean setupVmForPvlan(final boolean add, final Long hostId, final NicProfile nic) {
    if (!nic.getBroadCastUri().getScheme().equals("pvlan")) {
        return false;
    }
    String op = "add";
    if (!add) {
        op = "delete";
    }
    final Network network = _networkDao.findById(nic.getNetworkId());
    final Host host = _hostDao.findById(hostId);
    final String networkTag = _networkModel.getNetworkTag(host.getHypervisorType(), network);
    final PvlanSetupCommand cmd = PvlanSetupCommand.createVmSetup(op, nic.getBroadCastUri(), networkTag, nic.getMacAddress());
    final Answer answer;
    try {
        answer = _agentMgr.send(hostId, cmd);
    } catch (final OperationTimedoutException e) {
        s_logger.warn("Timed Out", e);
        return false;
    } catch (final AgentUnavailableException e) {
        s_logger.warn("Agent Unavailable ", e);
        return false;
    }
    boolean result = true;
    if (answer == null || !answer.getResult()) {
        result = false;
    }
    return result;
}
Also used : GetVmDiskStatsAnswer(com.cloud.legacymodel.communication.answer.GetVmDiskStatsAnswer) GetVmStatsAnswer(com.cloud.legacymodel.communication.answer.GetVmStatsAnswer) RestoreVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) PhysicalNetwork(com.cloud.network.PhysicalNetwork) Network(com.cloud.legacymodel.network.Network) Host(com.cloud.legacymodel.dc.Host) PvlanSetupCommand(com.cloud.legacymodel.communication.command.PvlanSetupCommand)

Aggregations

Network (com.cloud.legacymodel.network.Network)160 ArrayList (java.util.ArrayList)57 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)56 Account (com.cloud.legacymodel.user.Account)46 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)42 NetworkOffering (com.cloud.offering.NetworkOffering)36 PhysicalNetwork (com.cloud.network.PhysicalNetwork)34 IPAddressVO (com.cloud.network.dao.IPAddressVO)32 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)30 NetworkVO (com.cloud.network.dao.NetworkVO)28 List (java.util.List)28 Zone (com.cloud.db.model.Zone)27 DB (com.cloud.utils.db.DB)27 NicProfile (com.cloud.vm.NicProfile)26 Nic (com.cloud.legacymodel.network.Nic)21 DataCenter (com.cloud.legacymodel.dc.DataCenter)20 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)20 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)19 DomainRouterVO (com.cloud.vm.DomainRouterVO)18 ActionEvent (com.cloud.event.ActionEvent)17