use of com.cloud.legacymodel.communication.command.AggregationControlCommand in project cosmic by MissionCriticalCloud.
the class VirtualRoutingResource method executeRequest.
public Answer executeRequest(final NetworkElementCommand cmd) {
boolean aggregated = false;
final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
final Lock lock;
if (this._vrLockMap.containsKey(routerName)) {
lock = this._vrLockMap.get(routerName);
} else {
lock = new ReentrantLock();
this._vrLockMap.put(routerName, lock);
}
lock.lock();
try {
final ExecutionResult rc = this._vrDeployer.prepareCommand(cmd);
if (!rc.isSuccess()) {
s_logger.error("Failed to prepare VR command due to " + rc.getDetails());
return new Answer(cmd, false, rc.getDetails());
}
assert cmd.getRouterAccessIp() != null : "Why there is no access IP for VR?";
if (cmd.isQuery()) {
return executeQueryCommand(cmd);
}
if (cmd instanceof AggregationControlCommand) {
return execute((AggregationControlCommand) cmd);
}
if (this._vrAggregateCommandsSet.containsKey(routerName)) {
this._vrAggregateCommandsSet.get(routerName).add(cmd);
aggregated = true;
// TODO: Deal with group answer as well
return new Answer(cmd);
}
final List<ConfigItem> cfg = generateCommandCfg(cmd);
if (cfg == null) {
return Answer.createUnsupportedCommandAnswer(cmd);
}
return applyConfig(cmd, cfg);
} catch (final IllegalArgumentException e) {
return new Answer(cmd, false, e.getMessage());
} finally {
lock.unlock();
if (!aggregated) {
final ExecutionResult rc = this._vrDeployer.cleanupCommand(cmd);
if (!rc.isSuccess()) {
s_logger.error("Failed to cleanup VR command due to " + rc.getDetails());
}
}
}
}
use of com.cloud.legacymodel.communication.command.AggregationControlCommand in project cosmic by MissionCriticalCloud.
the class VirtualNetworkApplianceManagerImpl method finalizeCommandsOnStart.
@Override
public boolean finalizeCommandsOnStart(final Commands cmds, final VirtualMachineProfile profile) {
final DomainRouterVO router = _routerDao.findById(profile.getId());
final NicProfile controlNic = getControlNic(profile);
if (controlNic == null) {
s_logger.error("Control network doesn't exist for the router " + router);
return false;
}
finalizeSshAndVersionAndNetworkUsageOnStart(cmds, profile, router, controlNic);
// restart network if restartNetwork = false is not specified in profile
// parameters
final boolean reprogramGuestNtwks = profile.getParameter(Param.ReProgramGuestNetworks) == null || (Boolean) profile.getParameter(Param.ReProgramGuestNetworks);
final VirtualRouterProvider vrProvider = _vrProviderDao.findById(router.getElementId());
if (vrProvider == null) {
throw new CloudRuntimeException("Cannot find related virtual router provider of router: " + router.getHostName());
}
final Provider provider = Network.Provider.getProvider(vrProvider.getType().toString());
if (provider == null) {
throw new CloudRuntimeException("Cannot find related provider of virtual router provider: " + vrProvider.getType().toString());
}
final List<Long> routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId());
for (final Long guestNetworkId : routerGuestNtwkIds) {
final AggregationControlCommand startCmd = new AggregationControlCommand(Action.Start, router.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNetworkId, router.getId()));
cmds.addCommand(startCmd);
if (reprogramGuestNtwks) {
finalizeNetworkRulesForNetwork(cmds, router, provider, guestNetworkId);
}
final AggregationControlCommand finishCmd = new AggregationControlCommand(Action.Finish, router.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNetworkId, router.getId()));
cmds.addCommand(finishCmd);
}
return true;
}
use of com.cloud.legacymodel.communication.command.AggregationControlCommand in project cosmic by MissionCriticalCloud.
the class VirtualNetworkApplianceManagerImpl method aggregationExecution.
private boolean aggregationExecution(final AggregationControlCommand.Action action, final Network network, final List<DomainRouterVO> routers) throws ResourceUnavailableException {
int errors = 0;
for (final DomainRouterVO router : routers) {
final String routerControlIp = _routerControlHelper.getRouterControlIp(router.getId());
final String routerIpInNetwork = _routerControlHelper.getRouterIpInNetwork(network.getId(), router.getId());
if (routerIpInNetwork == null) {
// Nic hasn't been created in this router yet. Try to configure the next one.
s_logger.warn("The Network is not configured in the router " + router.getHostName() + " yet. Try the next router!");
errors++;
continue;
}
final AggregationControlCommand cmd = new AggregationControlCommand(action, router.getInstanceName(), routerControlIp, routerIpInNetwork);
final Commands cmds = new Commands(cmd);
if (!_nwHelper.sendCommandsToRouter(router, cmds)) {
return false;
}
}
if (errors == routers.size()) {
s_logger.error("aggregationExecution() on " + getClass().getName() + " failed! Network is not configured in any router.");
return false;
}
return true;
}
use of com.cloud.legacymodel.communication.command.AggregationControlCommand 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;
}
Aggregations