Search in sources :

Example 51 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class BigSwitchBcfUtils method getTopology.

public TopologyData getTopology(long physicalNetworkId) {
    List<NetworkVO> networks;
    List<NicVO> nics;
    networks = _networkDao.listByPhysicalNetworkTrafficType(physicalNetworkId, TrafficType.Guest);
    TopologyData topo = new TopologyData();
    // handle external network first, only if NAT service is enabled
    if (networks != null) {
        if (!(networks.isEmpty()) && isNatEnabled()) {
            // get public net info - needed to set up source nat gateway
            NetworkVO pubNet = getPublicNetwork(physicalNetworkId);
            // locate subnet info
            SearchCriteria<VlanVO> sc = _vlanDao.createSearchCriteria();
            sc.setParameters("network_id", pubNet.getId());
            VlanVO vlanVO = _vlanDao.findOneBy(sc);
            // add tenant external network external
            TopologyData.Network network = topo.new Network();
            network.setId("external");
            network.setName("external");
            network.setTenantId("external");
            network.setTenantName("external");
            String pubVlan = null;
            try {
                pubVlan = BroadcastDomainType.getValue(vlanVO.getVlanTag());
                if (StringUtils.isNumeric(pubVlan)) {
                    network.setVlan(Integer.valueOf(pubVlan));
                } else {
                    // untagged
                    pubVlan = "0";
                }
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            topo.addNetwork(network);
        }
    }
    // routerMap used internally for multiple updates to same tenant's router
    // add back to topo.routers after loop
    HashMap<String, RouterData> routerMap = new HashMap<String, RouterData>();
    for (NetworkVO netVO : networks) {
        TopologyData.Network network = topo.new Network();
        network.setId(netVO.getUuid());
        network.setName(netVO.getName());
        Integer vlan = null;
        if (netVO.getBroadcastUri() != null) {
            String vlanStr = BroadcastDomainType.getValue(netVO.getBroadcastUri());
            if (StringUtils.isNumeric(vlanStr)) {
                vlan = Integer.valueOf(vlanStr);
            } else {
                // untagged
                vlan = 0;
            }
        }
        network.setVlan(vlan);
        network.setState(netVO.getState().name());
        nics = _nicDao.listByNetworkId(netVO.getId());
        List<Port> ports = new ArrayList<Port>();
        String tenantId = null;
        String tenantName = null;
        // if VPC network, assign BCF tenant id with vpc uuid
        Vpc vpc = null;
        if (netVO.getVpcId() != null) {
            vpc = _vpcDao.acquireInLockTable(netVO.getVpcId());
        }
        if (vpc != null) {
            tenantId = vpc.getUuid();
            tenantName = vpc.getName();
        } else {
            tenantId = netVO.getUuid();
            tenantName = netVO.getName();
        }
        for (NicVO nic : nics) {
            NetworkData netData = new NetworkData();
            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());
            // if host not found, ignore this nic
            if (host == null) {
                continue;
            }
            String hostname = host.getName();
            long zoneId = netVO.getDataCenterId();
            String vmwareVswitchLabel = _networkModel.getDefaultGuestTrafficLabel(zoneId, HypervisorType.VMware);
            String[] labelArray = null;
            String vswitchName = null;
            if (vmwareVswitchLabel != null) {
                labelArray = vmwareVswitchLabel.split(",");
                vswitchName = labelArray[0];
            }
            // hypervisor type:
            //   kvm: ivs port name
            //   vmware: specific portgroup naming convention
            String pgName = "";
            if (host.getHypervisorType() == HypervisorType.KVM) {
                pgName = hostname;
            } else if (host.getHypervisorType() == HypervisorType.VMware) {
                pgName = hostname + "-" + vswitchName;
            }
            p.setHostId(pgName);
            p.setSegmentInfo(netData.new SegmentInfo(BroadcastDomainType.Vlan.name(), vlan));
            p.setOwner(BigSwitchBcfApi.getCloudstackInstanceId());
            List<AttachmentData.Attachment.IpAddress> ipList = new ArrayList<AttachmentData.Attachment.IpAddress>();
            ipList.add(new AttachmentData().getAttachment().new IpAddress(nic.getIPv4Address()));
            p.setIpAddresses(ipList);
            p.setId(nic.getUuid());
            p.setMac(nic.getMacAddress());
            netData.getNetwork().setId(network.getId());
            netData.getNetwork().setName(network.getName());
            netData.getNetwork().setTenantId(tenantId);
            netData.getNetwork().setTenantName(tenantName);
            netData.getNetwork().setState(netVO.getState().name());
            p.setNetwork(netData.getNetwork());
            ports.add(p);
        }
        network.setTenantId(tenantId);
        network.setTenantName(tenantName);
        network.setPorts(ports);
        topo.addNetwork(network);
        // add router for network
        RouterData routerData;
        if (tenantId != null) {
            if (!routerMap.containsKey(tenantId)) {
                routerData = new RouterData(tenantId);
                routerMap.put(tenantId, routerData);
            } else {
                routerData = routerMap.get(tenantId);
            }
            routerData.getRouter().getAcls().addAll(listACLbyNetwork(netVO));
            if (vpc != null) {
                routerData.getRouter().addExternalGateway(getPublicIpByVpc(vpc));
            } else {
                routerData.getRouter().addExternalGateway(getPublicIpByNetwork(netVO));
            }
            RouterInterfaceData intf = new RouterInterfaceData(tenantId, netVO.getGateway(), netVO.getCidr(), netVO.getUuid(), netVO.getName());
            routerData.getRouter().addInterface(intf);
        }
    }
    for (RouterData rd : routerMap.values()) {
        topo.addRouter(rd.getRouter());
    }
    return topo;
}
Also used : HashMap(java.util.HashMap) Port(com.cloud.network.bigswitch.TopologyData.Port) ArrayList(java.util.ArrayList) Vpc(com.cloud.network.vpc.Vpc) URISyntaxException(java.net.URISyntaxException) Network(com.cloud.network.Network) VlanVO(com.cloud.dc.VlanVO) NicVO(com.cloud.vm.NicVO) NetworkVO(com.cloud.network.dao.NetworkVO) Port(com.cloud.network.bigswitch.TopologyData.Port) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostVO(com.cloud.host.HostVO)

Example 52 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class ManagementServerImpl method findSystemVMTypeById.

@Override
public VirtualMachine.Type findSystemVMTypeById(final long instanceId) {
    final VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(instanceId, VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm);
    if (systemVm == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a system vm of specified instanceId");
        ex.addProxyObject(String.valueOf(instanceId), "instanceId");
        throw ex;
    }
    return systemVm.getType();
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VMInstanceVO(com.cloud.vm.VMInstanceVO)

Example 53 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class ManagementServerImpl method startSystemVM.

@Override
@ActionEvent(eventType = "", eventDescription = "", async = true)
public VirtualMachine startSystemVM(final long vmId) {
    final VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(vmId, VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm);
    if (systemVm == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find a system vm with specified vmId");
        ex.addProxyObject(String.valueOf(vmId), "vmId");
        throw ex;
    }
    if (systemVm.getType() == VirtualMachine.Type.ConsoleProxy) {
        ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_PROXY_START, "starting console proxy Vm");
        return startConsoleProxy(vmId);
    } else if (systemVm.getType() == VirtualMachine.Type.SecondaryStorageVm) {
        ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_SSVM_START, "starting secondary storage Vm");
        return startSecondaryStorageVm(vmId);
    } else {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a system vm with specified vmId");
        ex.addProxyObject(systemVm.getUuid(), "vmId");
        throw ex;
    }
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VMInstanceVO(com.cloud.vm.VMInstanceVO) ActionEvent(com.cloud.event.ActionEvent)

Example 54 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class ManagementServerImpl method searchForSystemVm.

@Override
public Pair<List<? extends VirtualMachine>, Integer> searchForSystemVm(final ListSystemVMsCmd cmd) {
    final String type = cmd.getSystemVmType();
    final Long zoneId = _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), cmd.getZoneId());
    final Long id = cmd.getId();
    final String name = cmd.getSystemVmName();
    final String state = cmd.getState();
    final String keyword = cmd.getKeyword();
    final Long podId = cmd.getPodId();
    final Long hostId = cmd.getHostId();
    final Long storageId = cmd.getStorageId();
    final Filter searchFilter = new Filter(VMInstanceVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<VMInstanceVO> sb = _vmInstanceDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("hostName", sb.entity().getHostName(), SearchCriteria.Op.LIKE);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodIdToDeployIn(), SearchCriteria.Op.EQ);
    sb.and("hostId", sb.entity().getHostId(), SearchCriteria.Op.EQ);
    sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
    sb.and("nulltype", sb.entity().getType(), SearchCriteria.Op.IN);
    if (storageId != null) {
        final SearchBuilder<VolumeVO> volumeSearch = _volumeDao.createSearchBuilder();
        volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
        sb.join("volumeSearch", volumeSearch, sb.entity().getId(), volumeSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
    }
    final SearchCriteria<VMInstanceVO> sc = sb.create();
    if (keyword != null) {
        final SearchCriteria<VMInstanceVO> ssc = _vmInstanceDao.createSearchCriteria();
        ssc.addOr("hostName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("hostName", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (name != null) {
        sc.setParameters("hostName", name);
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (podId != null) {
        sc.setParameters("podId", podId);
    }
    if (hostId != null) {
        sc.setParameters("hostId", hostId);
    }
    if (type != null) {
        sc.setParameters("type", type);
    } else {
        sc.setParameters("nulltype", VirtualMachine.Type.SecondaryStorageVm, VirtualMachine.Type.ConsoleProxy);
    }
    if (storageId != null) {
        sc.setJoinParameters("volumeSearch", "poolId", storageId);
    }
    final Pair<List<VMInstanceVO>, Integer> result = _vmInstanceDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends VirtualMachine>, Integer>(result.first(), result.second());
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) Filter(com.cloud.utils.db.Filter) VMInstanceVO(com.cloud.vm.VMInstanceVO) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair)

Example 55 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class VolumeApiServiceImpl method takeVolumeSnapshotThroughJobQueue.

public Outcome<Snapshot> takeVolumeSnapshotThroughJobQueue(final Long vmId, final Long volumeId, final Long policyId, final Long snapshotId, final Long accountId, final boolean quiesceVm, final Snapshot.LocationType locationType) {
    final CallContext context = CallContext.current();
    final User callingUser = context.getCallingUser();
    final Account callingAccount = context.getCallingAccount();
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId());
    workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
    workJob.setCmd(VmWorkTakeVolumeSnapshot.class.getName());
    workJob.setAccountId(callingAccount.getId());
    workJob.setUserId(callingUser.getId());
    workJob.setStep(VmWorkJobVO.Step.Starting);
    workJob.setVmType(VirtualMachine.Type.Instance);
    workJob.setVmInstanceId(vm.getId());
    workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
    // save work context info (there are some duplications)
    VmWorkTakeVolumeSnapshot workInfo = new VmWorkTakeVolumeSnapshot(callingUser.getId(), accountId != null ? accountId : callingAccount.getId(), vm.getId(), VolumeApiServiceImpl.VM_WORK_JOB_HANDLER, volumeId, policyId, snapshotId, quiesceVm, locationType);
    workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
    _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobSnapshotOutcome(workJob, snapshotId);
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) VmWorkTakeVolumeSnapshot(com.cloud.vm.VmWorkTakeVolumeSnapshot) VMInstanceVO(com.cloud.vm.VMInstanceVO) CallContext(org.apache.cloudstack.context.CallContext) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)

Aggregations

VMInstanceVO (com.cloud.vm.VMInstanceVO)131 ArrayList (java.util.ArrayList)40 HostVO (com.cloud.host.HostVO)34 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)28 Account (com.cloud.user.Account)28 VolumeVO (com.cloud.storage.VolumeVO)24 Test (org.junit.Test)24 HostPodVO (com.cloud.dc.HostPodVO)15 HashMap (java.util.HashMap)14 NetworkVO (com.cloud.network.dao.NetworkVO)13 User (com.cloud.user.User)13 NicVO (com.cloud.vm.NicVO)13 VmWorkJobVO (org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)13 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)12 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)12 ActionEvent (com.cloud.event.ActionEvent)11 Random (java.util.Random)11 Answer (com.cloud.agent.api.Answer)10 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10