Search in sources :

Example 91 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class SetGuestNetworkConfigItem method generateConfig.

@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
    final SetupGuestNetworkCommand command = (SetupGuestNetworkCommand) cmd;
    final NicTO nic = command.getNic();
    final String routerGIP = command.getAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP);
    final String gateway = command.getAccessDetail(NetworkElementCommand.GUEST_NETWORK_GATEWAY);
    final String cidr = Long.toString(NetUtils.getCidrSize(nic.getNetmask()));
    final String netmask = nic.getNetmask();
    final String domainName = command.getNetworkDomain();
    String dns = command.getDefaultDns1();
    if (dns == null || dns.isEmpty()) {
        dns = command.getDefaultDns2();
    } else {
        final String dns2 = command.getDefaultDns2();
        if (dns2 != null && !dns2.isEmpty()) {
            dns += "," + dns2;
        }
    }
    final GuestNetwork guestNetwork = new GuestNetwork(command.isAdd(), nic.getMac(), "eth" + nic.getDeviceId(), routerGIP, netmask, gateway, cidr, dns, domainName);
    return generateConfigItems(guestNetwork);
}
Also used : GuestNetwork(com.cloud.agent.resource.virtualnetwork.model.GuestNetwork) SetupGuestNetworkCommand(com.cloud.agent.api.SetupGuestNetworkCommand) NicTO(com.cloud.agent.api.to.NicTO)

Example 92 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class SetNetworkAclConfigItem method generateConfig.

@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
    final SetNetworkACLCommand command = (SetNetworkACLCommand) cmd;
    final String privateGw = cmd.getAccessDetail(NetworkElementCommand.VPC_PRIVATE_GATEWAY);
    final String[][] rules = command.generateFwRules();
    final String[] aclRules = rules[0];
    final NicTO nic = command.getNic();
    final String dev = "eth" + nic.getDeviceId();
    final String netmask = Long.toString(NetUtils.getCidrSize(nic.getNetmask()));
    final List<AclRule> ingressRules = new ArrayList<AclRule>();
    final List<AclRule> egressRules = new ArrayList<AclRule>();
    for (int i = 0; i < aclRules.length; i++) {
        AclRule aclRule;
        final String[] ruleParts = aclRules[i].split(":");
        switch(ruleParts[1].toLowerCase()) {
            case "icmp":
                aclRule = new IcmpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "tcp":
                aclRule = new TcpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "udp":
                aclRule = new UdpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "all":
                aclRule = new AllAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]));
                break;
            default:
                // So, let's catch the exception and continue in the loop.
                try {
                    aclRule = new ProtocolAclRule(ruleParts[5], false, Integer.parseInt(ruleParts[1]));
                } catch (final Exception e) {
                    s_logger.warn("Problem occured when reading the entries in the ruleParts array. Actual array size is '" + ruleParts.length + "', but trying to read from index 5.");
                    continue;
                }
        }
        if ("Ingress".equals(ruleParts[0])) {
            ingressRules.add(aclRule);
        } else {
            egressRules.add(aclRule);
        }
    }
    final NetworkACL networkACL = new NetworkACL(dev, nic.getMac(), privateGw != null, nic.getIp(), netmask, ingressRules.toArray(new AclRule[ingressRules.size()]), egressRules.toArray(new AclRule[egressRules.size()]));
    return generateConfigItems(networkACL);
}
Also used : AllAclRule(com.cloud.agent.resource.virtualnetwork.model.AllAclRule) ArrayList(java.util.ArrayList) SetNetworkACLCommand(com.cloud.agent.api.routing.SetNetworkACLCommand) IcmpAclRule(com.cloud.agent.resource.virtualnetwork.model.IcmpAclRule) NetworkACL(com.cloud.agent.resource.virtualnetwork.model.NetworkACL) ProtocolAclRule(com.cloud.agent.resource.virtualnetwork.model.ProtocolAclRule) TcpAclRule(com.cloud.agent.resource.virtualnetwork.model.TcpAclRule) UdpAclRule(com.cloud.agent.resource.virtualnetwork.model.UdpAclRule) TcpAclRule(com.cloud.agent.resource.virtualnetwork.model.TcpAclRule) AclRule(com.cloud.agent.resource.virtualnetwork.model.AclRule) UdpAclRule(com.cloud.agent.resource.virtualnetwork.model.UdpAclRule) IcmpAclRule(com.cloud.agent.resource.virtualnetwork.model.IcmpAclRule) ProtocolAclRule(com.cloud.agent.resource.virtualnetwork.model.ProtocolAclRule) AllAclRule(com.cloud.agent.resource.virtualnetwork.model.AllAclRule) NicTO(com.cloud.agent.api.to.NicTO)

Example 93 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class VirtualMachineManagerImpl method orchestrateAddVmToNetwork.

private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Network network, final NicProfile requested) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    final CallContext cctx = CallContext.current();
    s_logger.debug("Adding vm " + vm + " to network " + network + "; requested nic profile " + requested);
    final VMInstanceVO vmVO = _vmDao.findById(vm.getId());
    final ReservationContext context = new ReservationContextImpl(null, null, cctx.getCallingUser(), cctx.getCallingAccount());
    final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
    final DataCenter dc = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
    final Host host = _hostDao.findById(vm.getHostId());
    final DeployDestination dest = new DeployDestination(dc, null, null, host);
    //check vm state
    if (vm.getState() == State.Running) {
        //1) allocate and prepare nic
        final NicProfile nic = _networkMgr.createNicForVm(network, requested, context, vmProfile, true);
        //2) Convert vmProfile to vmTO
        final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
        final VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
        //3) Convert nicProfile to NicTO
        final NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType());
        //4) plug the nic to the vm
        s_logger.debug("Plugging nic for vm " + vm + " in network " + network);
        boolean result = false;
        try {
            result = plugNic(network, nicTO, vmTO, context, dest);
            if (result) {
                s_logger.debug("Nic is plugged successfully for vm " + vm + " in network " + network + ". Vm  is a part of network now");
                final long isDefault = nic.isDefaultNic() ? 1 : 0;
                // insert nic's Id into DB as resource_name
                if (VirtualMachine.Type.User.equals(vmVO.getType())) {
                    //Log usage event for user Vms only
                    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vmVO.getAccountId(), vmVO.getDataCenterId(), vmVO.getId(), Long.toString(nic.getId()), network.getNetworkOfferingId(), null, isDefault, VirtualMachine.class.getName(), vmVO.getUuid(), vm.isDisplay());
                }
                return nic;
            } else {
                s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network);
                return null;
            }
        } finally {
            if (!result) {
                s_logger.debug("Removing nic " + nic + " from vm " + vmProfile.getVirtualMachine() + " as nic plug failed on the backend");
                _networkMgr.removeNic(vmProfile, _nicsDao.findById(nic.getId()));
            }
        }
    } else if (vm.getState() == State.Stopped) {
        //1) allocate nic
        return _networkMgr.createNicForVm(network, requested, context, vmProfile, false);
    } else {
        s_logger.warn("Unable to add vm " + vm + " to network  " + network);
        throw new ResourceUnavailableException("Unable to add vm " + vm + " to network, is not in the right state", DataCenter.class, vm.getDataCenterId());
    }
}
Also used : Host(com.cloud.host.Host) CallContext(org.apache.cloudstack.context.CallContext) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) DataCenter(com.cloud.dc.DataCenter) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) DeployDestination(com.cloud.deploy.DeployDestination) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) NicTO(com.cloud.agent.api.to.NicTO)

Example 94 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class VirtualMachineManagerImpl method orchestrateRemoveVmFromNetwork.

@DB
private boolean orchestrateRemoveVmFromNetwork(final VirtualMachine vm, final Network network, final URI broadcastUri) throws ConcurrentOperationException, ResourceUnavailableException {
    final CallContext cctx = CallContext.current();
    final VMInstanceVO vmVO = _vmDao.findById(vm.getId());
    final ReservationContext context = new ReservationContextImpl(null, null, cctx.getCallingUser(), cctx.getCallingAccount());
    final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
    final DataCenter dc = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
    final Host host = _hostDao.findById(vm.getHostId());
    final DeployDestination dest = new DeployDestination(dc, null, null, host);
    final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
    final VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
    Nic nic = null;
    if (broadcastUri != null) {
        nic = _nicsDao.findByNetworkIdInstanceIdAndBroadcastUri(network.getId(), vm.getId(), broadcastUri.toString());
    } else {
        nic = _networkModel.getNicInNetwork(vm.getId(), network.getId());
    }
    if (nic == null) {
        s_logger.warn("Could not get a nic with " + network);
        return false;
    }
    // don't delete default NIC on a user VM
    if (nic.isDefaultNic() && vm.getType() == VirtualMachine.Type.User) {
        s_logger.warn("Failed to remove nic from " + vm + " in " + network + ", nic is default.");
        throw new CloudRuntimeException("Failed to remove nic from " + vm + " in " + network + ", nic is default.");
    }
    //Lock on nic is needed here
    final Nic lock = _nicsDao.acquireInLockTable(nic.getId());
    if (lock == null) {
        //check if nic is still there. Return if it was released already
        if (_nicsDao.findById(nic.getId()) == null) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Not need to remove the vm " + vm + " from network " + network + " as the vm doesn't have nic in this network");
            }
            return true;
        }
        throw new ConcurrentOperationException("Unable to lock nic " + nic.getId());
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Lock is acquired for nic id " + lock.getId() + " as a part of remove vm " + vm + " from network " + network);
    }
    try {
        final NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkModel.getNetworkRate(network.getId(), vm.getId()), _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network));
        //1) Unplug the nic
        if (vm.getState() == State.Running) {
            final NicTO nicTO = toNicTO(nicProfile, vmProfile.getVirtualMachine().getHypervisorType());
            s_logger.debug("Un-plugging nic for vm " + vm + " from network " + network);
            final boolean result = unplugNic(network, nicTO, vmTO, context, dest);
            if (result) {
                s_logger.debug("Nic is unplugged successfully for vm " + vm + " in network " + network);
            } else {
                s_logger.warn("Failed to unplug nic for the vm " + vm + " from network " + network);
                return false;
            }
        } else if (vm.getState() != State.Stopped) {
            s_logger.warn("Unable to remove vm " + vm + " from network  " + network);
            throw new ResourceUnavailableException("Unable to remove vm " + vm + " from network, is not in the right state", DataCenter.class, vm.getDataCenterId());
        }
        //2) Release the nic
        _networkMgr.releaseNic(vmProfile, nic);
        s_logger.debug("Successfully released nic " + nic + "for vm " + vm);
        //3) Remove the nic
        _networkMgr.removeNic(vmProfile, nic);
        return true;
    } finally {
        if (lock != null) {
            _nicsDao.releaseFromLockTable(lock.getId());
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Lock is released for nic id " + lock.getId() + " as a part of remove vm " + vm + " from network " + network);
            }
        }
    }
}
Also used : Host(com.cloud.host.Host) CallContext(org.apache.cloudstack.context.CallContext) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) DataCenter(com.cloud.dc.DataCenter) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) DeployDestination(com.cloud.deploy.DeployDestination) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) NicTO(com.cloud.agent.api.to.NicTO) DB(com.cloud.utils.db.DB)

Example 95 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class VirtualMachineManagerImpl method toNicTO.

@Override
public NicTO toNicTO(final NicProfile nic, final HypervisorType hypervisorType) {
    final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(hypervisorType);
    final NicTO nicTO = hvGuru.toNicTO(nic);
    return nicTO;
}
Also used : HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

NicTO (com.cloud.agent.api.to.NicTO)99 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)42 Answer (com.cloud.agent.api.Answer)31 Test (org.junit.Test)30 InternalErrorException (com.cloud.exception.InternalErrorException)28 LibvirtException (org.libvirt.LibvirtException)27 ArrayList (java.util.ArrayList)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 ConfigurationException (javax.naming.ConfigurationException)23 Connect (org.libvirt.Connect)23 URISyntaxException (java.net.URISyntaxException)22 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)21 IOException (java.io.IOException)20 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)19 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)19 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)19 Connection (com.xensource.xenapi.Connection)18 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)14 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)13 URI (java.net.URI)12