Search in sources :

Example 1 with VMOverviewTO

use of com.cloud.legacymodel.to.VMOverviewTO in project cosmic by MissionCriticalCloud.

the class AdvancedNetworkVisitor method visit.

@Override
public boolean visit(final DhcpEntryRules dhcp) throws ResourceUnavailableException {
    final VirtualRouter router = dhcp.getRouter();
    final Commands commands = new Commands(Command.OnError.Stop);
    final VMOverviewTO vmOverview = _commandSetupHelper.createVmOverviewFromRouter(router);
    final UpdateVmOverviewCommand updateVmOverviewCommand = _commandSetupHelper.createUpdateVmOverviewCommand(router, vmOverview);
    commands.addCommand(updateVmOverviewCommand);
    return _networkGeneralHelper.sendCommandsToRouter(router, commands);
}
Also used : VMOverviewTO(com.cloud.legacymodel.to.VMOverviewTO) Commands(com.cloud.agent.manager.Commands) VirtualRouter(com.cloud.legacymodel.network.VirtualRouter) UpdateVmOverviewCommand(com.cloud.legacymodel.communication.command.UpdateVmOverviewCommand)

Example 2 with VMOverviewTO

use of com.cloud.legacymodel.to.VMOverviewTO in project cosmic by MissionCriticalCloud.

the class BasicNetworkVisitor method visit.

@Override
public boolean visit(final UserdataPwdRules userdata) throws ResourceUnavailableException {
    final VirtualRouter router = userdata.getRouter();
    final Commands commands = new Commands(Command.OnError.Stop);
    final VirtualMachineProfile profile = userdata.getProfile();
    final NicVO nicVO = userdata.getNicVo();
    final DeployDestination destination = userdata.getDestination();
    if (router.getPodIdToDeployIn() == destination.getPod().getId()) {
        _commandSetupHelper.createPasswordCommand(router, profile, nicVO, commands);
        final VMOverviewTO vmOverview = _commandSetupHelper.createVmOverviewFromRouter(router);
        final UpdateVmOverviewCommand updateVmOverviewCommand = _commandSetupHelper.createUpdateVmOverviewCommand(router, vmOverview);
        commands.addCommand(updateVmOverviewCommand);
        return _networkGeneralHelper.sendCommandsToRouter(router, commands);
    }
    return true;
}
Also used : DeployDestination(com.cloud.deploy.DeployDestination) VMOverviewTO(com.cloud.legacymodel.to.VMOverviewTO) Commands(com.cloud.agent.manager.Commands) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) NicVO(com.cloud.vm.NicVO) VirtualRouter(com.cloud.legacymodel.network.VirtualRouter) UpdateVmOverviewCommand(com.cloud.legacymodel.communication.command.UpdateVmOverviewCommand)

Example 3 with VMOverviewTO

use of com.cloud.legacymodel.to.VMOverviewTO in project cosmic by MissionCriticalCloud.

the class CommandSetupHelper method createVmOverviewFromRouter.

public VMOverviewTO createVmOverviewFromRouter(final VirtualRouter router) {
    final VMOverviewTO vmOverviewTO = new VMOverviewTO();
    final Map<UserVmVO, List<NicVO>> vmsAndNicsMap = new HashMap<>();
    final List<NicVO> routerNics = _nicDao.listByVmId(router.getId());
    for (final NicVO routerNic : routerNics) {
        final Network network = _networkModel.getNetwork(routerNic.getNetworkId());
        if (TrafficType.Guest.equals(network.getTrafficType()) && !GuestType.Sync.equals(network.getGuestType())) {
            _userVmDao.listByNetworkIdAndStates(network.getId(), VirtualMachine.State.Starting, VirtualMachine.State.Running, VirtualMachine.State.Paused, VirtualMachine.State.Migrating, VirtualMachine.State.Stopping).forEach(vm -> {
                final NicVO nic = _nicDao.findByNtwkIdAndInstanceId(network.getId(), vm.getId());
                if (nic != null) {
                    if (!vmsAndNicsMap.containsKey(vm)) {
                        vmsAndNicsMap.put(vm, new ArrayList<NicVO>() {

                            {
                                add(nic);
                            }
                        });
                    } else {
                        vmsAndNicsMap.get(vm).add(nic);
                    }
                }
            });
        }
    }
    final List<VMOverviewTO.VMTO> vmsTO = new ArrayList<>();
    vmsAndNicsMap.forEach((vm, nics) -> {
        _userVmDao.loadDetails(vm);
        final VMOverviewTO.VMTO vmTO = new VMOverviewTO.VMTO(vm.getHostName());
        final List<VMOverviewTO.VMTO.InterfaceTO> interfacesTO = new ArrayList<>();
        final ServiceOfferingVO serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId());
        final Zone zone = zoneRepository.findById(router.getDataCenterId()).orElse(null);
        nics.forEach(nic -> {
            final VMOverviewTO.VMTO.InterfaceTO interfaceTO = new VMOverviewTO.VMTO.InterfaceTO(nic.getIPv4Address(), nic.getMacAddress(), nic.isDefaultNic());
            final NetworkVO networkVO = _networkDao.findById(nic.getNetworkId());
            final String vmNameFQDN = networkVO != null ? vm.getHostName() + "." + networkVO.getNetworkDomain() : vm.getHostName();
            final Map<String, String> metadata = interfaceTO.getMetadata();
            metadata.put("service-offering", StringUtils.unicodeEscape(serviceOffering.getDisplayText()));
            metadata.put("availability-zone", StringUtils.unicodeEscape(zone.getName()));
            metadata.put("local-ipv4", nic.getIPv4Address());
            metadata.put("local-hostname", StringUtils.unicodeEscape(vmNameFQDN));
            metadata.put("public-ipv4", router.getPublicIpAddress() != null ? router.getPublicIpAddress() : nic.getIPv4Address());
            metadata.put("public-hostname", router.getPublicIpAddress());
            metadata.put("instance-id", vm.getUuid() != null ? vm.getUuid() : vm.getInstanceName());
            metadata.put("vm-id", vm.getUuid() != null ? vm.getUuid() : String.valueOf(vm.getId()));
            metadata.put("public-keys", vm.getDetail("SSH.PublicKey"));
            final String cloudIdentifier = _configDao.getValue("cloud.identifier");
            metadata.put("cloud-identifier", cloudIdentifier != null ? "CloudStack-{" + cloudIdentifier + "}" : "");
            final Map<String, String> userData = interfaceTO.getUserData();
            userData.put("user-data", vm.getUserData());
            interfacesTO.add(interfaceTO);
        });
        vmTO.setInterfaces(interfacesTO.toArray(new VMOverviewTO.VMTO.InterfaceTO[interfacesTO.size()]));
        vmsTO.add(vmTO);
    });
    vmOverviewTO.setVms(vmsTO.toArray(new VMOverviewTO.VMTO[vmsTO.size()]));
    return vmOverviewTO;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) NetworkVO(com.cloud.network.dao.NetworkVO) HashMap(java.util.HashMap) Zone(com.cloud.db.model.Zone) ArrayList(java.util.ArrayList) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) VMOverviewTO(com.cloud.legacymodel.to.VMOverviewTO) Network(com.cloud.legacymodel.network.Network) ArrayList(java.util.ArrayList) List(java.util.List) NicVO(com.cloud.vm.NicVO)

Example 4 with VMOverviewTO

use of com.cloud.legacymodel.to.VMOverviewTO in project cosmic by MissionCriticalCloud.

the class VpcVirtualNetworkApplianceManagerImpl method finalizeCommandsOnStart.

@Override
public boolean finalizeCommandsOnStart(final Commands cmds, final VirtualMachineProfile profile) {
    final DomainRouterVO domainRouterVO = _routerDao.findById(profile.getId());
    final boolean isVpc = domainRouterVO.getVpcId() != null;
    if (!isVpc) {
        return super.finalizeCommandsOnStart(cmds, profile);
    }
    if (domainRouterVO.getState() == State.Starting || domainRouterVO.getState() == State.Running) {
        final List<Nic> nicsToExclude = new ArrayList<>();
        final List<Ip> ipsToExclude = new ArrayList<>();
        final List<StaticRouteProfile> staticRoutesToExclude = new ArrayList<>();
        // 1) FORM SSH CHECK COMMAND
        final NicProfile controlNic = getControlNic(profile);
        if (controlNic == null) {
            s_logger.error("Control network doesn't exist for the router " + domainRouterVO);
            return false;
        }
        finalizeSshAndVersionAndNetworkUsageOnStart(cmds, profile, domainRouterVO, controlNic);
        // 2) FORM PLUG NIC COMMANDS
        final List<Pair<Nic, Network>> syncNics = new ArrayList<>();
        final List<Pair<Nic, Network>> guestNics = new ArrayList<>();
        final List<Pair<Nic, Network>> publicNics = new ArrayList<>();
        final List<? extends Nic> routerNics = _nicDao.listByVmId(profile.getId());
        for (final Nic routerNic : routerNics) {
            final Network network = _networkModel.getNetwork(routerNic.getNetworkId());
            if (network.getTrafficType() == TrafficType.Guest) {
                final Pair<Nic, Network> guestNic = new Pair<>(routerNic, network);
                if (GuestType.Sync.equals(network.getGuestType())) {
                    syncNics.add(guestNic);
                } else {
                    guestNics.add(guestNic);
                }
            } else if (network.getTrafficType() == TrafficType.Public) {
                final Pair<Nic, Network> publicNic = new Pair<>(routerNic, network);
                publicNics.add(publicNic);
            }
        }
        final List<Command> usageCmds = new ArrayList<>();
        // 3) PREPARE PLUG NIC COMMANDS
        try {
            // add VPC router to sync networks
            for (final Pair<Nic, Network> nicNtwk : syncNics) {
                final Nic syncNic = nicNtwk.first();
                // plug sync nic
                final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, syncNic.getNetworkId(), null), domainRouterVO.getInstanceName(), domainRouterVO.getType());
                cmds.addCommand(plugNicCmd);
            }
            // add VPC router to public networks
            final List<PublicIp> sourceNat = new ArrayList<>(1);
            for (final Pair<Nic, Network> nicNtwk : publicNics) {
                final Nic publicNic = nicNtwk.first();
                final Network publicNtwk = nicNtwk.second();
                final IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(publicNtwk.getId(), publicNic.getIPv4Address());
                if (userIp.isSourceNat()) {
                    final PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
                    sourceNat.add(publicIp);
                    if (domainRouterVO.getPublicIpAddress() == null) {
                        final DomainRouterVO routerVO = _routerDao.findById(domainRouterVO.getId());
                        routerVO.setPublicIpAddress(publicNic.getIPv4Address());
                        routerVO.setPublicNetmask(publicNic.getIPv4Netmask());
                        routerVO.setPublicMacAddress(publicNic.getMacAddress());
                        _routerDao.update(routerVO.getId(), routerVO);
                    }
                }
                final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, publicNic.getNetworkId(), publicNic.getBroadcastUri().toString()), domainRouterVO.getInstanceName(), domainRouterVO.getType());
                cmds.addCommand(plugNicCmd);
                final VpcVO vpc = _vpcDao.findById(domainRouterVO.getVpcId());
                final NetworkUsageCommand netUsageCmd = new NetworkUsageCommand(domainRouterVO.getPrivateIpAddress(), domainRouterVO.getInstanceName(), true, publicNic.getIPv4Address(), vpc.getCidr());
                usageCmds.add(netUsageCmd);
                UserStatisticsVO stats = _userStatsDao.findBy(domainRouterVO.getAccountId(), domainRouterVO.getDataCenterId(), publicNtwk.getId(), publicNic.getIPv4Address(), domainRouterVO.getId(), domainRouterVO.getType().toString());
                if (stats == null) {
                    stats = new UserStatisticsVO(domainRouterVO.getAccountId(), domainRouterVO.getDataCenterId(), publicNic.getIPv4Address(), domainRouterVO.getId(), domainRouterVO.getType().toString(), publicNtwk.getId());
                    _userStatsDao.persist(stats);
                }
                _commandSetupHelper.createPublicIpACLsCommands(domainRouterVO, cmds);
            }
            // create ip assoc for source nat
            if (!sourceNat.isEmpty()) {
                _commandSetupHelper.findIpsToExclude(sourceNat, ipsToExclude);
            }
            // add VPC router to guest networks
            for (final Pair<Nic, Network> nicNtwk : guestNics) {
                final Nic guestNic = nicNtwk.first();
                // plug guest nic
                final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, guestNic.getNetworkId(), null), domainRouterVO.getInstanceName(), domainRouterVO.getType());
                cmds.addCommand(plugNicCmd);
                if (_networkModel.isPrivateGateway(guestNic.getNetworkId())) {
                    // set private network
                    final PrivateIpVO ipVO = _privateIpDao.findByIpAndSourceNetworkId(guestNic.getNetworkId(), guestNic.getIPv4Address());
                    final Long privateGwAclId = _vpcGatewayDao.getNetworkAclIdForPrivateIp(ipVO.getVpcId(), ipVO.getNetworkId(), ipVO.getIpAddress());
                    if (privateGwAclId != null) {
                        // set network acl on private gateway
                        final List<NetworkACLItemVO> networkACLs = _networkACLItemDao.listByACL(privateGwAclId);
                        s_logger.debug("Found " + networkACLs.size() + " network ACLs to apply as a part of VPC VR " + domainRouterVO + " start for private gateway ip = " + ipVO.getIpAddress());
                        _commandSetupHelper.createNetworkACLsCommands(networkACLs, domainRouterVO, cmds, ipVO.getNetworkId(), true);
                    }
                }
            }
        } catch (final Exception ex) {
            s_logger.warn("Failed to add router " + domainRouterVO + " to network due to exception ", ex);
            return false;
        }
        // 4) REPROGRAM GUEST NETWORK
        boolean reprogramGuestNtwks = profile.getParameter(Param.ReProgramGuestNetworks) == null || (Boolean) profile.getParameter(Param.ReProgramGuestNetworks);
        final VirtualRouterProvider vrProvider = _vrProviderDao.findById(domainRouterVO.getElementId());
        if (vrProvider == null) {
            throw new CloudRuntimeException("Cannot find related virtual router provider of router: " + domainRouterVO.getHostName());
        }
        final Provider provider = Provider.getProvider(vrProvider.getType().toString());
        if (provider == null) {
            throw new CloudRuntimeException("Cannot find related provider of virtual router provider: " + vrProvider.getType().toString());
        }
        boolean isDhcpSupported = false;
        for (final Pair<Nic, Network> nicNtwk : guestNics) {
            final Nic guestNic = nicNtwk.first();
            final AggregationControlCommand startCmd = new AggregationControlCommand(Action.Start, domainRouterVO.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNic.getNetworkId(), domainRouterVO.getId()));
            cmds.addCommand(startCmd);
            if (reprogramGuestNtwks) {
                finalizeIpAssocForNetwork(domainRouterVO, provider, guestNic.getNetworkId(), ipsToExclude);
                finalizeNetworkRulesForNetwork(cmds, domainRouterVO, provider, guestNic.getNetworkId());
            }
            isDhcpSupported = isDhcpSupported || _networkModel.isProviderSupportServiceInNetwork(guestNic.getNetworkId(), Service.Dhcp, provider);
            final AggregationControlCommand finishCmd = new AggregationControlCommand(Action.Finish, domainRouterVO.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNic.getNetworkId(), domainRouterVO.getId()));
            cmds.addCommand(finishCmd);
        }
        final NetworkOverviewTO networkOverview = _commandSetupHelper.createNetworkOverviewFromRouter(domainRouterVO, nicsToExclude, ipsToExclude, staticRoutesToExclude, null, null, null);
        final UpdateNetworkOverviewCommand updateNetworkOverviewCommand = _commandSetupHelper.createUpdateNetworkOverviewCommand(domainRouterVO, networkOverview);
        updateNetworkOverviewCommand.setPlugNics(true);
        cmds.addCommand(updateNetworkOverviewCommand);
        if (isDhcpSupported) {
            final VMOverviewTO vmOverview = _commandSetupHelper.createVmOverviewFromRouter(domainRouterVO);
            final UpdateVmOverviewCommand updateVmOverviewCommand = _commandSetupHelper.createUpdateVmOverviewCommand(domainRouterVO, vmOverview);
            cmds.addCommand(updateVmOverviewCommand);
        }
        // 5) RE-APPLY VR Configuration
        final Vpc vpc = _vpcDao.findById(domainRouterVO.getVpcId());
        _commandSetupHelper.createVRConfigCommands(vpc, domainRouterVO, cmds);
        // Add network usage commands
        cmds.addCommands(usageCmds);
    }
    return true;
}
Also used : Ip(com.cloud.legacymodel.network.Ip) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) Vpc(com.cloud.legacymodel.network.vpc.Vpc) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) NetworkACLItemVO(com.cloud.network.vpc.NetworkACLItemVO) StaticRouteProfile(com.cloud.legacymodel.network.vpc.StaticRouteProfile) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.cloud.legacymodel.network.Network) AggregationControlCommand(com.cloud.legacymodel.communication.command.AggregationControlCommand) VMOverviewTO(com.cloud.legacymodel.to.VMOverviewTO) NetworkOverviewTO(com.cloud.legacymodel.to.NetworkOverviewTO) PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) Pair(com.cloud.legacymodel.utils.Pair) PublicIp(com.cloud.network.addr.PublicIp) Nic(com.cloud.legacymodel.network.Nic) NetworkUsageCommand(com.cloud.legacymodel.communication.command.NetworkUsageCommand) UpdateNetworkOverviewCommand(com.cloud.legacymodel.communication.command.UpdateNetworkOverviewCommand) NicProfile(com.cloud.vm.NicProfile) ConfigurationException(javax.naming.ConfigurationException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Provider(com.cloud.legacymodel.network.Network.Provider) VirtualRouterProvider(com.cloud.network.VirtualRouterProvider) VpcVO(com.cloud.network.vpc.VpcVO) PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) Command(com.cloud.legacymodel.communication.command.Command) NetworkUsageCommand(com.cloud.legacymodel.communication.command.NetworkUsageCommand) AggregationControlCommand(com.cloud.legacymodel.communication.command.AggregationControlCommand) UpdateVmOverviewCommand(com.cloud.legacymodel.communication.command.UpdateVmOverviewCommand) UpdateNetworkOverviewCommand(com.cloud.legacymodel.communication.command.UpdateNetworkOverviewCommand) VirtualRouterProvider(com.cloud.network.VirtualRouterProvider) IPAddressVO(com.cloud.network.dao.IPAddressVO) DomainRouterVO(com.cloud.vm.DomainRouterVO) UserStatisticsVO(com.cloud.user.UserStatisticsVO) UpdateVmOverviewCommand(com.cloud.legacymodel.communication.command.UpdateVmOverviewCommand)

Example 5 with VMOverviewTO

use of com.cloud.legacymodel.to.VMOverviewTO in project cosmic by MissionCriticalCloud.

the class AdvancedNetworkVisitor method visit.

@Override
public boolean visit(final UserdataPwdRules userdata) throws ResourceUnavailableException {
    final VirtualRouter router = userdata.getRouter();
    final Commands commands = new Commands(Command.OnError.Stop);
    final VirtualMachineProfile profile = userdata.getProfile();
    final NicVO nicVO = userdata.getNicVo();
    _commandSetupHelper.createPasswordCommand(router, profile, nicVO, commands);
    final VMOverviewTO vmOverview = _commandSetupHelper.createVmOverviewFromRouter(router);
    final UpdateVmOverviewCommand updateVmOverviewCommand = _commandSetupHelper.createUpdateVmOverviewCommand(router, vmOverview);
    commands.addCommand(updateVmOverviewCommand);
    return _networkGeneralHelper.sendCommandsToRouter(router, commands);
}
Also used : VMOverviewTO(com.cloud.legacymodel.to.VMOverviewTO) Commands(com.cloud.agent.manager.Commands) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) NicVO(com.cloud.vm.NicVO) VirtualRouter(com.cloud.legacymodel.network.VirtualRouter) UpdateVmOverviewCommand(com.cloud.legacymodel.communication.command.UpdateVmOverviewCommand)

Aggregations

VMOverviewTO (com.cloud.legacymodel.to.VMOverviewTO)7 UpdateVmOverviewCommand (com.cloud.legacymodel.communication.command.UpdateVmOverviewCommand)6 Commands (com.cloud.agent.manager.Commands)5 VirtualRouter (com.cloud.legacymodel.network.VirtualRouter)5 NicVO (com.cloud.vm.NicVO)4 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)3 Network (com.cloud.legacymodel.network.Network)2 ArrayList (java.util.ArrayList)2 Zone (com.cloud.db.model.Zone)1 DeployDestination (com.cloud.deploy.DeployDestination)1 AggregationControlCommand (com.cloud.legacymodel.communication.command.AggregationControlCommand)1 Command (com.cloud.legacymodel.communication.command.Command)1 NetworkUsageCommand (com.cloud.legacymodel.communication.command.NetworkUsageCommand)1 PlugNicCommand (com.cloud.legacymodel.communication.command.PlugNicCommand)1 UpdateNetworkOverviewCommand (com.cloud.legacymodel.communication.command.UpdateNetworkOverviewCommand)1 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)1 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)1 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)1 Ip (com.cloud.legacymodel.network.Ip)1 Provider (com.cloud.legacymodel.network.Network.Provider)1