Search in sources :

Example 56 with DomainRouterVO

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

the class VirtualNetworkApplianceManagerImpl method rebootRouters.

private List<Long> rebootRouters(final List<DomainRouterVO> routers) {
    final List<Long> jobIds = new ArrayList<Long>();
    for (final DomainRouterVO router : routers) {
        if (!_nwHelper.checkRouterVersion(router)) {
            s_logger.debug("Upgrading template for router: " + router.getId());
            final Map<String, String> params = new HashMap<String, String>();
            params.put("ctxUserId", "1");
            params.put("ctxAccountId", "" + router.getAccountId());
            final RebootRouterCmd cmd = new RebootRouterCmd();
            ComponentContext.inject(cmd);
            params.put("id", "" + router.getId());
            params.put("ctxStartEventId", "1");
            final AsyncJobVO job = new AsyncJobVO("", User.UID_SYSTEM, router.getAccountId(), RebootRouterCmd.class.getName(), ApiGsonHelper.getBuilder().create().toJson(params), router.getId(), cmd.getInstanceType() != null ? cmd.getInstanceType().toString() : null, null);
            job.setDispatcher(_asyncDispatcher.getName());
            final long jobId = _asyncMgr.submitAsyncJob(job);
            jobIds.add(jobId);
        } else {
            s_logger.debug("Router: " + router.getId() + " is already at the latest version. No upgrade required");
        }
    }
    return jobIds;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RebootRouterCmd(org.apache.cloudstack.api.command.admin.router.RebootRouterCmd) DomainRouterVO(com.cloud.vm.DomainRouterVO) AsyncJobVO(org.apache.cloudstack.framework.jobs.impl.AsyncJobVO)

Example 57 with DomainRouterVO

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

the class VirtualNetworkApplianceManagerImpl method upgradeRouterTemplate.

@Override
public List<Long> upgradeRouterTemplate(final UpgradeRouterTemplateCmd cmd) {
    List<DomainRouterVO> routers = new ArrayList<DomainRouterVO>();
    int params = 0;
    final Long routerId = cmd.getId();
    if (routerId != null) {
        params++;
        final DomainRouterVO router = _routerDao.findById(routerId);
        if (router != null) {
            routers.add(router);
        }
    }
    final Long domainId = cmd.getDomainId();
    if (domainId != null) {
        final String accountName = cmd.getAccount();
        // List by account, if account Name is specified along with domainId
        if (accountName != null) {
            final Account account = _accountMgr.getActiveAccountByName(accountName, domainId);
            if (account == null) {
                throw new InvalidParameterValueException("Account :" + accountName + " does not exist in domain: " + domainId);
            }
            routers = _routerDao.listRunningByAccountId(account.getId());
        } else {
            // List by domainId, account name not specified
            routers = _routerDao.listRunningByDomain(domainId);
        }
        params++;
    }
    final Long clusterId = cmd.getClusterId();
    if (clusterId != null) {
        params++;
        routers = _routerDao.listRunningByClusterId(clusterId);
    }
    final Long podId = cmd.getPodId();
    if (podId != null) {
        params++;
        routers = _routerDao.listRunningByPodId(podId);
    }
    final Long zoneId = cmd.getZoneId();
    if (zoneId != null) {
        params++;
        routers = _routerDao.listRunningByDataCenter(zoneId);
    }
    if (params > 1) {
        throw new InvalidParameterValueException("Multiple parameters not supported. Specify only one among routerId/zoneId/podId/clusterId/accountId/domainId");
    }
    if (routers != null) {
        return rebootRouters(routers);
    }
    return null;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 58 with DomainRouterVO

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

the class VirtualNetworkApplianceManagerImpl method updateSite2SiteVpnConnectionState.

@DB
protected void updateSite2SiteVpnConnectionState(final List<DomainRouterVO> routers) {
    for (final DomainRouterVO router : routers) {
        final List<Site2SiteVpnConnectionVO> conns = _s2sVpnMgr.getConnectionsForRouter(router);
        if (conns == null || conns.isEmpty()) {
            continue;
        }
        if (router.getIsRedundantRouter() && router.getRedundantState() != RedundantState.MASTER) {
            continue;
        }
        if (router.getState() != VirtualMachine.State.Running) {
            for (final Site2SiteVpnConnectionVO conn : conns) {
                if (conn.getState() != Site2SiteVpnConnection.State.Error) {
                    conn.setState(Site2SiteVpnConnection.State.Disconnected);
                    _s2sVpnConnectionDao.persist(conn);
                }
            }
            continue;
        }
        final List<String> ipList = new ArrayList<String>();
        for (final Site2SiteVpnConnectionVO conn : conns) {
            if (conn.getState() != Site2SiteVpnConnection.State.Connected && conn.getState() != Site2SiteVpnConnection.State.Disconnected) {
                continue;
            }
            final Site2SiteCustomerGateway gw = _s2sCustomerGatewayDao.findById(conn.getCustomerGatewayId());
            ipList.add(gw.getGatewayIp());
        }
        final String privateIP = router.getPrivateIpAddress();
        final HostVO host = _hostDao.findById(router.getHostId());
        if (host == null || host.getState() != Status.Up) {
            continue;
        } else if (host.getManagementServerId() != ManagementServerNode.getManagementServerId()) {
            /* Only cover hosts managed by this management server */
            continue;
        } else if (privateIP != null) {
            final CheckS2SVpnConnectionsCommand command = new CheckS2SVpnConnectionsCommand(ipList);
            command.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
            command.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
            command.setWait(30);
            final Answer origAnswer = _agentMgr.easySend(router.getHostId(), command);
            CheckS2SVpnConnectionsAnswer answer = null;
            if (origAnswer instanceof CheckS2SVpnConnectionsAnswer) {
                answer = (CheckS2SVpnConnectionsAnswer) origAnswer;
            } else {
                s_logger.warn("Unable to update router " + router.getHostName() + "'s VPN connection status");
                continue;
            }
            if (!answer.getResult()) {
                s_logger.warn("Unable to update router " + router.getHostName() + "'s VPN connection status");
                continue;
            }
            for (final Site2SiteVpnConnectionVO conn : conns) {
                final Site2SiteVpnConnectionVO lock = _s2sVpnConnectionDao.acquireInLockTable(conn.getId());
                if (lock == null) {
                    throw new CloudRuntimeException("Unable to acquire lock for site to site vpn connection id " + conn.getId());
                }
                try {
                    if (conn.getState() != Site2SiteVpnConnection.State.Connected && conn.getState() != Site2SiteVpnConnection.State.Disconnected) {
                        continue;
                    }
                    final Site2SiteVpnConnection.State oldState = conn.getState();
                    final Site2SiteCustomerGateway gw = _s2sCustomerGatewayDao.findById(conn.getCustomerGatewayId());
                    if (answer.isConnected(gw.getGatewayIp())) {
                        conn.setState(Site2SiteVpnConnection.State.Connected);
                    } else {
                        conn.setState(Site2SiteVpnConnection.State.Disconnected);
                    }
                    _s2sVpnConnectionDao.persist(conn);
                    if (oldState != conn.getState()) {
                        final String title = "Site-to-site Vpn Connection to " + gw.getName() + " just switch from " + oldState + " to " + conn.getState();
                        final String context = "Site-to-site Vpn Connection to " + gw.getName() + " on router " + router.getHostName() + "(id: " + router.getId() + ") " + " just switch from " + oldState + " to " + conn.getState();
                        s_logger.info(context);
                        _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER, router.getDataCenterId(), router.getPodIdToDeployIn(), title, context);
                    }
                } finally {
                    _s2sVpnConnectionDao.releaseFromLockTable(lock.getId());
                }
            }
        }
    }
}
Also used : CheckS2SVpnConnectionsAnswer(com.cloud.agent.api.CheckS2SVpnConnectionsAnswer) ArrayList(java.util.ArrayList) Site2SiteVpnConnectionVO(com.cloud.network.dao.Site2SiteVpnConnectionVO) HostVO(com.cloud.host.HostVO) ManagementServerHostVO(com.cloud.cluster.ManagementServerHostVO) Site2SiteVpnConnection(com.cloud.network.Site2SiteVpnConnection) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) GetDomRVersionAnswer(com.cloud.agent.api.GetDomRVersionAnswer) CheckS2SVpnConnectionsAnswer(com.cloud.agent.api.CheckS2SVpnConnectionsAnswer) GetRouterAlertsAnswer(com.cloud.agent.api.GetRouterAlertsAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CheckS2SVpnConnectionsCommand(com.cloud.agent.api.CheckS2SVpnConnectionsCommand) DomainRouterVO(com.cloud.vm.DomainRouterVO) Site2SiteCustomerGateway(com.cloud.network.Site2SiteCustomerGateway) DB(com.cloud.utils.db.DB)

Example 59 with DomainRouterVO

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

the class VirtualNetworkApplianceManagerImpl method rebootRouter.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ROUTER_REBOOT, eventDescription = "rebooting router Vm", async = true)
public VirtualRouter rebootRouter(final long routerId, final boolean reprogramNetwork) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    final Account caller = CallContext.current().getCallingAccount();
    // verify parameters
    final DomainRouterVO router = _routerDao.findById(routerId);
    if (router == null) {
        throw new InvalidParameterValueException("Unable to find domain router with id " + routerId + ".");
    }
    _accountMgr.checkAccess(caller, null, true, router);
    // Can reboot domain router only in Running state
    if (router == null || router.getState() != VirtualMachine.State.Running) {
        s_logger.warn("Unable to reboot, virtual router is not in the right state " + router.getState());
        throw new ResourceUnavailableException("Unable to reboot domR, it is not in right state " + router.getState(), DataCenter.class, router.getDataCenterId());
    }
    final UserVO user = _userDao.findById(CallContext.current().getCallingUserId());
    s_logger.debug("Stopping and starting router " + router + " as a part of router reboot");
    if (stop(router, false, user, caller) != null) {
        return startRouter(routerId, reprogramNetwork);
    } else {
        throw new CloudRuntimeException("Failed to reboot router " + router);
    }
}
Also used : Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) DomainRouterVO(com.cloud.vm.DomainRouterVO) ActionEvent(com.cloud.event.ActionEvent)

Example 60 with DomainRouterVO

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

the class VirtualNetworkApplianceManagerImpl method removeDhcpSupportForSubnet.

@Override
public boolean removeDhcpSupportForSubnet(final Network network, final List<DomainRouterVO> routers) throws ResourceUnavailableException {
    if (routers == null || routers.isEmpty()) {
        s_logger.warn("Failed to add/remove VPN users: no router found for account and zone");
        throw new ResourceUnavailableException("Unable to assign ip addresses, domR doesn't exist for network " + network.getId(), DataCenter.class, network.getDataCenterId());
    }
    for (final DomainRouterVO router : routers) {
        if (router.getState() != VirtualMachine.State.Running) {
            s_logger.warn("Failed to add/remove VPN users: router not in running state");
            throw new ResourceUnavailableException("Unable to assign ip addresses, domR is not in right state " + router.getState(), DataCenter.class, network.getDataCenterId());
        }
        final Commands cmds = new Commands(Command.OnError.Continue);
        final List<NicIpAliasVO> revokedIpAliasVOs = _nicIpAliasDao.listByNetworkIdAndState(network.getId(), NicIpAlias.State.revoked);
        s_logger.debug("Found" + revokedIpAliasVOs.size() + "ip Aliases to revoke on the router as a part of dhcp configuration");
        final List<IpAliasTO> revokedIpAliasTOs = new ArrayList<IpAliasTO>();
        for (final NicIpAliasVO revokedAliasVO : revokedIpAliasVOs) {
            revokedIpAliasTOs.add(new IpAliasTO(revokedAliasVO.getIp4Address(), revokedAliasVO.getNetmask(), revokedAliasVO.getAliasCount().toString()));
        }
        final List<NicIpAliasVO> aliasVOs = _nicIpAliasDao.listByNetworkIdAndState(network.getId(), NicIpAlias.State.active);
        s_logger.debug("Found" + aliasVOs.size() + "ip Aliases to apply on the router as a part of dhcp configuration");
        final List<IpAliasTO> activeIpAliasTOs = new ArrayList<IpAliasTO>();
        for (final NicIpAliasVO aliasVO : aliasVOs) {
            activeIpAliasTOs.add(new IpAliasTO(aliasVO.getIp4Address(), aliasVO.getNetmask(), aliasVO.getAliasCount().toString()));
        }
        _commandSetupHelper.createDeleteIpAliasCommand(router, revokedIpAliasTOs, activeIpAliasTOs, network.getId(), cmds);
        _commandSetupHelper.configDnsMasq(router, network, cmds);
        final boolean result = _nwHelper.sendCommandsToRouter(router, cmds);
        if (result) {
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(final TransactionStatus status) {
                    for (final NicIpAliasVO revokedAliasVO : revokedIpAliasVOs) {
                        _nicIpAliasDao.expunge(revokedAliasVO.getId());
                    }
                }
            });
            return true;
        }
    }
    return false;
}
Also used : ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) Commands(com.cloud.agent.manager.Commands) ArrayList(java.util.ArrayList) IpAliasTO(com.cloud.agent.api.routing.IpAliasTO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NicIpAliasVO(com.cloud.vm.dao.NicIpAliasVO) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Aggregations

DomainRouterVO (com.cloud.vm.DomainRouterVO)148 ArrayList (java.util.ArrayList)39 DataCenterVO (com.cloud.dc.DataCenterVO)33 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)33 NetworkTopology (org.apache.cloudstack.network.topology.NetworkTopology)27 Test (org.junit.Test)26 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 Network (com.cloud.network.Network)20 NicProfile (com.cloud.vm.NicProfile)17 Account (com.cloud.user.Account)12 NetworkVO (com.cloud.network.dao.NetworkVO)11 Answer (com.cloud.agent.api.Answer)10 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)10 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)10 UserVmVO (com.cloud.vm.UserVmVO)9 Vpc (com.cloud.network.vpc.Vpc)8 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)8 UserVO (com.cloud.user.UserVO)8 HashMap (java.util.HashMap)8