Search in sources :

Example 11 with Site2SiteVpnConnectionVO

use of com.cloud.network.dao.Site2SiteVpnConnectionVO in project cosmic by MissionCriticalCloud.

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<>();
        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.isIpPresent(gw.getGatewayIp())) {
                        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) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) GetDomRVersionAnswer(com.cloud.agent.api.GetDomRVersionAnswer) CheckS2SVpnConnectionsAnswer(com.cloud.agent.api.CheckS2SVpnConnectionsAnswer) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) 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 12 with Site2SiteVpnConnectionVO

use of com.cloud.network.dao.Site2SiteVpnConnectionVO in project cosmic by MissionCriticalCloud.

the class Site2SiteVpnManagerImpl method stopVpnConnection.

@DB
private void stopVpnConnection(final Long id) throws ResourceUnavailableException {
    final Site2SiteVpnConnectionVO conn = _vpnConnectionDao.acquireInLockTable(id);
    if (conn == null) {
        throw new CloudRuntimeException("Unable to acquire lock on " + conn);
    }
    try {
        conn.setState(State.Disconnected);
        _vpnConnectionDao.persist(conn);
        boolean result = true;
        for (final Site2SiteVpnServiceProvider element : _s2sProviders) {
            result = result & element.stopSite2SiteVpn(conn);
        }
    } finally {
        _vpnConnectionDao.releaseFromLockTable(conn.getId());
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Site2SiteVpnServiceProvider(com.cloud.network.element.Site2SiteVpnServiceProvider) Site2SiteVpnConnectionVO(com.cloud.network.dao.Site2SiteVpnConnectionVO) DB(com.cloud.utils.db.DB)

Example 13 with Site2SiteVpnConnectionVO

use of com.cloud.network.dao.Site2SiteVpnConnectionVO in project cosmic by MissionCriticalCloud.

the class Site2SiteVpnManagerImpl method updateVpnConnection.

@Override
@ActionEvent(eventType = EventTypes.EVENT_S2S_VPN_CONNECTION_UPDATE, eventDescription = "creating s2s vpn gateway", async = true)
public Site2SiteVpnConnection updateVpnConnection(final long id, final String customId, final Boolean forDisplay) {
    final Account caller = CallContext.current().getCallingAccount();
    final Site2SiteVpnConnectionVO conn = _vpnConnectionDao.findById(id);
    if (conn == null) {
        throw new InvalidParameterValueException("Fail to find site to site VPN connection " + id);
    }
    _accountMgr.checkAccess(caller, null, false, conn);
    if (customId != null) {
        conn.setUuid(customId);
    }
    if (forDisplay != null) {
        conn.setDisplay(forDisplay);
    }
    _vpnConnectionDao.update(id, conn);
    return _vpnConnectionDao.findById(id);
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) Site2SiteVpnConnectionVO(com.cloud.network.dao.Site2SiteVpnConnectionVO) ActionEvent(com.cloud.event.ActionEvent)

Example 14 with Site2SiteVpnConnectionVO

use of com.cloud.network.dao.Site2SiteVpnConnectionVO in project cosmic by MissionCriticalCloud.

the class Site2SiteVpnManagerImpl method resetVpnConnection.

@Override
@ActionEvent(eventType = EventTypes.EVENT_S2S_VPN_CONNECTION_RESET, eventDescription = "reseting s2s vpn connection", create = true)
public Site2SiteVpnConnection resetVpnConnection(final ResetVpnConnectionCmd cmd) throws ResourceUnavailableException {
    CallContext.current().setEventDetails(" Id: " + cmd.getId());
    final Account caller = CallContext.current().getCallingAccount();
    final Long id = cmd.getId();
    Site2SiteVpnConnectionVO conn = _vpnConnectionDao.findById(id);
    if (conn == null) {
        throw new InvalidParameterValueException("Fail to find site to site VPN connection " + id + " to reset!");
    }
    _accountMgr.checkAccess(caller, null, false, conn);
    if (conn.getState() == State.Pending) {
        throw new InvalidParameterValueException("VPN connection " + id + " cannot be reseted when state is Pending!");
    }
    if (conn.getState() == State.Connected || conn.getState() == State.Error) {
        stopVpnConnection(id);
    }
    startVpnConnection(id);
    conn = _vpnConnectionDao.findById(id);
    return conn;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) Site2SiteVpnConnectionVO(com.cloud.network.dao.Site2SiteVpnConnectionVO) ActionEvent(com.cloud.event.ActionEvent)

Example 15 with Site2SiteVpnConnectionVO

use of com.cloud.network.dao.Site2SiteVpnConnectionVO in project cosmic by MissionCriticalCloud.

the class Site2SiteVpnManagerImpl method updateCustomerGateway.

@Override
@ActionEvent(eventType = EventTypes.EVENT_S2S_VPN_CUSTOMER_GATEWAY_UPDATE, eventDescription = "update s2s vpn customer gateway", create = true)
public Site2SiteCustomerGateway updateCustomerGateway(final UpdateVpnCustomerGatewayCmd cmd) {
    CallContext.current().setEventDetails(" Id: " + cmd.getId());
    final Account caller = CallContext.current().getCallingAccount();
    final Long id = cmd.getId();
    final Site2SiteCustomerGatewayVO gw = _customerGatewayDao.findById(id);
    if (gw == null) {
        throw new InvalidParameterValueException("Find to find customer gateway with id " + id);
    }
    _accountMgr.checkAccess(caller, null, false, gw);
    final String name = cmd.getName();
    if (name != null) {
        final long accountId = gw.getAccountId();
        Site2SiteCustomerGatewayVO existedGw = _customerGatewayDao.findByNameAndAccountId(name, accountId);
        if (existedGw != null && existedGw.getId() != gw.getId()) {
            throw new InvalidParameterValueException("The customer gateway with name " + name + " already existed!");
        }
        gw.setName(name);
    }
    final String gatewayIp = cmd.getGatewayIp();
    if (gatewayIp != null) {
        if (!NetUtils.isValidIp4(gatewayIp)) {
            throw new InvalidParameterValueException("The customer gateway ip " + gatewayIp + " is invalid!");
        }
        gw.setGatewayIp(gatewayIp);
    }
    final String ikePolicy = cmd.getIkePolicy();
    if (ikePolicy != null) {
        if (!NetUtils.isValidS2SVpnPolicy("ike", ikePolicy)) {
            throw new InvalidParameterValueException("The customer gateway IKE policy" + ikePolicy + " is invalid!");
        }
        gw.setIkePolicy(ikePolicy);
    }
    final String espPolicy = cmd.getEspPolicy();
    if (espPolicy != null) {
        if (!NetUtils.isValidS2SVpnPolicy("esp", espPolicy)) {
            throw new InvalidParameterValueException("The customer gateway ESP policy" + espPolicy + " is invalid!");
        }
        gw.setEspPolicy(espPolicy);
    }
    final Long ikeLifetime = cmd.getIkeLifetime();
    if (ikeLifetime != null) {
        if (ikeLifetime > 86400) {
            throw new InvalidParameterValueException("The IKE lifetime " + ikeLifetime + " of vpn connection is invalid!");
        }
        gw.setIkeLifetime(ikeLifetime);
    }
    final Long espLifetime = cmd.getEspLifetime();
    if (espLifetime != null) {
        if (espLifetime > 86400) {
            throw new InvalidParameterValueException("The ESP lifetime " + espLifetime + " of vpn connection is invalid!");
        }
        gw.setEspLifetime(espLifetime);
    }
    final List<String> peerList = cmd.getPeerCidrList();
    if (peerList != null) {
        final String guestCidrList = getPeerCidrListString(peerList);
        checkCustomerGatewayCidrList(guestCidrList);
        gw.setGuestCidrList(guestCidrList);
    }
    gw.setIpsecPsk(cmd.getIpsecPsk());
    gw.setDpd(cmd.getDpd());
    gw.setEncap(cmd.getEncap());
    _customerGatewayDao.persist(gw);
    final List<Site2SiteVpnConnectionVO> conns = _vpnConnectionDao.listByCustomerGatewayId(gw.getId());
    conns.forEach(conn -> {
        for (final Site2SiteVpnServiceProvider element : _s2sProviders) {
            try {
                element.refreshSite2SiteVpn(conn);
            } catch (ResourceUnavailableException e) {
                s_logger.warn("Unable to refresh site-to-site VPN connection: " + conn.getUuid());
                return;
            }
        }
    });
    return gw;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) Site2SiteCustomerGatewayVO(com.cloud.network.dao.Site2SiteCustomerGatewayVO) Site2SiteVpnServiceProvider(com.cloud.network.element.Site2SiteVpnServiceProvider) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) Site2SiteVpnConnectionVO(com.cloud.network.dao.Site2SiteVpnConnectionVO) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

Site2SiteVpnConnectionVO (com.cloud.network.dao.Site2SiteVpnConnectionVO)21 ActionEvent (com.cloud.event.ActionEvent)11 Account (com.cloud.user.Account)11 DB (com.cloud.utils.db.DB)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)6 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)6 Site2SiteVpnServiceProvider (com.cloud.network.element.Site2SiteVpnServiceProvider)5 ArrayList (java.util.ArrayList)5 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)4 Site2SiteCustomerGateway (com.cloud.network.Site2SiteCustomerGateway)4 Site2SiteCustomerGatewayVO (com.cloud.network.dao.Site2SiteCustomerGatewayVO)4 CheckS2SVpnConnectionsCommand (com.cloud.agent.api.CheckS2SVpnConnectionsCommand)3 HostVO (com.cloud.host.HostVO)3 Site2SiteVpnConnection (com.cloud.network.Site2SiteVpnConnection)3 DomainRouterVO (com.cloud.vm.DomainRouterVO)3 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)2 Answer (com.cloud.agent.api.Answer)2 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)2 CheckS2SVpnConnectionsAnswer (com.cloud.agent.api.CheckS2SVpnConnectionsAnswer)2