use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.
the class VirtualNetworkApplianceManagerImpl method createRedundantRouterArgs.
protected StringBuilder createRedundantRouterArgs(final NicProfile nic, final DomainRouterVO router) {
final StringBuilder buf = new StringBuilder();
final boolean isRedundant = router.getIsRedundantRouter();
if (isRedundant) {
buf.append(" redundant_router=1");
final int advertInt = NumbersUtil.parseInt(_configDao.getValue(Config.RedundantRouterVrrpInterval.key()), 1);
buf.append(" advert_int=").append(advertInt);
final Long vpcId = router.getVpcId();
final List<DomainRouterVO> routers;
if (vpcId != null) {
routers = _routerDao.listByVpcId(vpcId);
// For a redundant VPC router, both shall have the same router id. It will be used by the VRRP virtural_router_id attribute.
// So we use the VPC id to avoid group problems.
buf.append(" router_id=").append(vpcId);
// Will build the routers password based on the VPC ID and UUID.
final Vpc vpc = _vpcDao.findById(vpcId);
try {
final MessageDigest digest = MessageDigest.getInstance("SHA-512");
final byte[] rawDigest = vpc.getUuid().getBytes(Charset.defaultCharset());
digest.update(rawDigest);
final BigInteger password = new BigInteger(1, digest.digest());
buf.append(" router_password=").append(password);
} catch (final NoSuchAlgorithmException e) {
s_logger.error("Failed to pssword! Will use the plan B instead.");
buf.append(" router_password=").append(vpc.getUuid());
}
} else {
routers = _routerDao.listByNetworkAndRole(nic.getNetworkId(), Role.VIRTUAL_ROUTER);
}
String redundantState = RedundantState.BACKUP.toString();
router.setRedundantState(RedundantState.BACKUP);
if (routers.size() == 0) {
redundantState = RedundantState.PRIMARY.toString();
router.setRedundantState(RedundantState.PRIMARY);
} else {
final DomainRouterVO router0 = routers.get(0);
if (router.getId() == router0.getId()) {
redundantState = RedundantState.PRIMARY.toString();
router.setRedundantState(RedundantState.PRIMARY);
}
}
buf.append(" redundant_state=").append(redundantState);
}
return buf;
}
use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.
the class VirtualNetworkApplianceManagerImpl method getRouterAlerts.
protected void getRouterAlerts() {
try {
final List<DomainRouterVO> routers = _routerDao.listByStateAndManagementServer(VirtualMachine.State.Running, mgmtSrvrId);
s_logger.debug("Found " + routers.size() + " running routers. ");
for (final DomainRouterVO router : routers) {
final String serviceMonitoringFlag = SetServiceMonitor.valueIn(router.getDataCenterId());
// Monitor service is not enabled in the corresponding Zone
if (!Boolean.parseBoolean(serviceMonitoringFlag)) {
continue;
}
String controlIP = _routerControlHelper.getRouterControlIp(router.getId());
if (controlIP != null && !controlIP.equals("0.0.0.0")) {
OpRouterMonitorServiceVO opRouterMonitorServiceVO = _opRouterMonitorServiceDao.findById(router.getId());
GetRouterAlertsCommand command = null;
if (opRouterMonitorServiceVO == null) {
// To
command = new GetRouterAlertsCommand(new String("1970-01-01 00:00:00"));
// avoid
// sending
// null
// value
} else {
command = new GetRouterAlertsCommand(opRouterMonitorServiceVO.getLastAlertTimestamp());
}
command.setAccessDetail(NetworkElementCommand.ROUTER_IP, controlIP);
try {
final Answer origAnswer = _agentMgr.easySend(router.getHostId(), command);
GetRouterAlertsAnswer answer = null;
if (origAnswer == null) {
s_logger.warn("Unable to get alerts from router " + router.getHostName());
continue;
}
if (origAnswer instanceof GetRouterAlertsAnswer) {
answer = (GetRouterAlertsAnswer) origAnswer;
} else {
s_logger.warn("Unable to get alerts from router " + router.getHostName());
continue;
}
if (!answer.getResult()) {
s_logger.warn("Unable to get alerts from router " + router.getHostName() + " " + answer.getDetails());
continue;
}
final String[] alerts = answer.getAlerts();
if (alerts != null) {
final String lastAlertTimeStamp = answer.getTimeStamp();
final SimpleDateFormat sdfrmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdfrmt.setLenient(false);
try {
sdfrmt.parse(lastAlertTimeStamp);
} catch (final ParseException e) {
s_logger.warn("Invalid last alert timestamp received while collecting alerts from router: " + router.getInstanceName());
continue;
}
for (final String alert : alerts) {
_alertMgr.sendAlert(AlertType.ALERT_TYPE_DOMAIN_ROUTER, router.getDataCenterId(), router.getPodIdToDeployIn(), "Monitoring Service on VR " + router.getInstanceName(), alert);
}
if (opRouterMonitorServiceVO == null) {
opRouterMonitorServiceVO = new OpRouterMonitorServiceVO(router.getId(), router.getHostName(), lastAlertTimeStamp);
_opRouterMonitorServiceDao.persist(opRouterMonitorServiceVO);
} else {
opRouterMonitorServiceVO.setLastAlertTimestamp(lastAlertTimeStamp);
_opRouterMonitorServiceDao.update(opRouterMonitorServiceVO.getId(), opRouterMonitorServiceVO);
}
}
} catch (final Exception e) {
s_logger.warn("Error while collecting alerts from router: " + router.getInstanceName(), e);
continue;
}
}
}
} catch (final Exception e) {
s_logger.warn("Error while collecting alerts from router", e);
}
}
use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.
the class NetworkHelperImpl method handleSingleWorkingRedundantRouter.
@Override
public void handleSingleWorkingRedundantRouter(final List<? extends VirtualRouter> connectedRouters, final List<? extends VirtualRouter> disconnectedRouters, final String reason) throws ResourceUnavailableException {
if (connectedRouters.isEmpty() || disconnectedRouters.isEmpty()) {
return;
}
for (final VirtualRouter virtualRouter : connectedRouters) {
if (!virtualRouter.getIsRedundantRouter()) {
throw new ResourceUnavailableException("Who is calling this with non-redundant router or non-domain router?", DataCenter.class, virtualRouter.getDataCenterId());
}
}
for (final VirtualRouter virtualRouter : disconnectedRouters) {
if (!virtualRouter.getIsRedundantRouter()) {
throw new ResourceUnavailableException("Who is calling this with non-redundant router or non-domain router?", DataCenter.class, virtualRouter.getDataCenterId());
}
}
final DomainRouterVO connectedRouter = (DomainRouterVO) connectedRouters.get(0);
DomainRouterVO disconnectedRouter = (DomainRouterVO) disconnectedRouters.get(0);
if (s_logger.isDebugEnabled()) {
s_logger.debug("About to stop the router " + disconnectedRouter.getInstanceName() + " due to: " + reason);
}
final String title = "Virtual router " + disconnectedRouter.getInstanceName() + " would be stopped after connecting back, due to " + reason;
final String context = "Virtual router (name: " + disconnectedRouter.getInstanceName() + ", id: " + disconnectedRouter.getId() + ") would be stopped after connecting back, due to: " + reason;
_alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER, disconnectedRouter.getDataCenterId(), disconnectedRouter.getPodIdToDeployIn(), title, context);
disconnectedRouter.setStopPending(true);
disconnectedRouter = _routerDao.persist(disconnectedRouter);
}
use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.
the class NetworkHelperImpl method destroyRouter.
@Override
public VirtualRouter destroyRouter(final long routerId, final Account caller, final Long callerUserId) throws ResourceUnavailableException, ConcurrentOperationException {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Attempting to destroy router " + routerId);
}
final DomainRouterVO router = _routerDao.findById(routerId);
if (router == null) {
return null;
}
_accountMgr.checkAccess(caller, null, true, router);
_itMgr.expunge(router.getUuid());
_routerHealthCheckResultDao.expungeHealthChecks(router.getId());
_routerDao.remove(router.getId());
return router;
}
use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.
the class NetworkHelperImpl method deployRouter.
@Override
public DomainRouterVO deployRouter(final RouterDeploymentDefinition routerDeploymentDefinition, final boolean startRouter) throws InsufficientAddressCapacityException, InsufficientServerCapacityException, InsufficientCapacityException, StorageUnavailableException, ResourceUnavailableException {
final ServiceOfferingVO routerOffering = _serviceOfferingDao.findById(routerDeploymentDefinition.getServiceOfferingId());
final Account owner = routerDeploymentDefinition.getOwner();
// Router is the network element, we don't know the hypervisor type yet.
// Try to allocate the domR twice using diff hypervisors, and when
// failed both times, throw the exception up
final List<HypervisorType> hypervisors = getHypervisors(routerDeploymentDefinition);
int allocateRetry = 0;
int startRetry = 0;
DomainRouterVO router = null;
for (final Iterator<HypervisorType> iter = hypervisors.iterator(); iter.hasNext(); ) {
final HypervisorType hType = iter.next();
try {
final long id = _routerDao.getNextInSequence(Long.class, "id");
if (s_logger.isDebugEnabled()) {
s_logger.debug(String.format("Allocating the VR with id=%s in datacenter %s with the hypervisor type %s", id, routerDeploymentDefinition.getDest().getDataCenter(), hType));
}
final String templateName = retrieveTemplateName(hType, routerDeploymentDefinition.getDest().getDataCenter().getId());
final VMTemplateVO template = _templateDao.findRoutingTemplate(hType, templateName);
if (template == null) {
s_logger.debug(hType + " won't support system vm, skip it");
continue;
}
final boolean offerHA = routerOffering.isOfferHA();
// routerDeploymentDefinition.getVpc().getId() ==> do not use
// VPC because it is not a VPC offering.
final Long vpcId = routerDeploymentDefinition.getVpc() != null ? routerDeploymentDefinition.getVpc().getId() : null;
long userId = CallContext.current().getCallingUserId();
if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
final List<UserVO> userVOs = _userDao.listByAccount(owner.getAccountId());
if (!userVOs.isEmpty()) {
userId = userVOs.get(0).getId();
}
}
router = new DomainRouterVO(id, routerOffering.getId(), routerDeploymentDefinition.getVirtualProvider().getId(), VirtualMachineName.getRouterName(id, s_vmInstanceName), template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), userId, routerDeploymentDefinition.isRedundant(), RedundantState.UNKNOWN, offerHA, false, vpcId);
router.setDynamicallyScalable(template.isDynamicallyScalable());
router.setRole(Role.VIRTUAL_ROUTER);
router = _routerDao.persist(router);
reallocateRouterNetworks(routerDeploymentDefinition, router, template, null);
router = _routerDao.findById(router.getId());
} catch (final InsufficientCapacityException ex) {
if (allocateRetry < 2 && iter.hasNext()) {
s_logger.debug("Failed to allocate the VR with hypervisor type " + hType + ", retrying one more time");
continue;
} else {
throw ex;
}
} finally {
allocateRetry++;
}
if (startRouter) {
try {
router = startVirtualRouter(router, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount(), routerDeploymentDefinition.getParams());
break;
} catch (final InsufficientCapacityException ex) {
if (startRetry < 2 && iter.hasNext()) {
s_logger.debug("Failed to start the VR " + router + " with hypervisor type " + hType + ", " + "destroying it and recreating one more time");
// destroy the router
destroyRouter(router.getId(), _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM), User.UID_SYSTEM);
continue;
} else {
throw ex;
}
} finally {
startRetry++;
}
} else {
// return stopped router
return router;
}
}
return router;
}
Aggregations