use of com.cloud.legacymodel.network.vpc.Vpc in project cosmic by MissionCriticalCloud.
the class ListVPCsCmdByAdmin method execute.
@Override
public void execute() {
final Pair<List<? extends Vpc>, Integer> vpcs = _vpcService.listVpcs(getId(), getVpcName(), getDisplayText(), getSupportedServices(), getCidr(), getVpcOffId(), getState(), getAccountName(), getDomainId(), getKeyword(), getStartIndex(), getPageSizeVal(), getZoneId(), isRecursive(), listAll(), getRestartRequired(), getTags(), getProjectId(), getDisplay(), getComplianceStatus());
final ListResponse<VpcResponse> response = new ListResponse<>();
final List<VpcResponse> vpcResponses = new ArrayList<>();
for (final Vpc vpc : vpcs.first()) {
final VpcResponse offeringResponse = _responseGenerator.createVpcResponse(ResponseView.Full, vpc);
vpcResponses.add(offeringResponse);
}
response.setResponses(vpcResponses, vpcs.second());
response.setResponseName(getCommandName());
setResponseObject(response);
}
use of com.cloud.legacymodel.network.vpc.Vpc in project cosmic by MissionCriticalCloud.
the class CreateVPCCmd method execute.
@Override
public void execute() {
Vpc vpc = null;
try {
if (isStart()) {
_vpcService.startVpc(getEntityId(), true);
} else {
s_logger.debug("Not starting VPC as " + ApiConstants.START + "=false was passed to the API");
}
vpc = _entityMgr.findById(Vpc.class, getEntityId());
} catch (final ResourceUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
} catch (final ConcurrentOperationException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
} catch (final InsufficientCapacityException ex) {
s_logger.info(ex.toString());
throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, ex.getMessage());
}
if (vpc != null) {
final VpcResponse response = _responseGenerator.createVpcResponse(ResponseView.Restricted, vpc);
response.setResponseName(getCommandName());
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create VPC");
}
}
use of com.cloud.legacymodel.network.vpc.Vpc in project cosmic by MissionCriticalCloud.
the class VirtualNetworkApplianceManagerImpl method createRedundantRouterArgs.
protected StringBuilder createRedundantRouterArgs(final NicProfile nic, final DomainRouterVO router) {
final StringBuilder buf = new StringBuilder();
final long networkId = nic.getNetworkId();
_networkDao.findById(networkId);
final List<DomainRouterVO> routers;
final Long vpcId = router.getVpcId();
if (vpcId != null) {
final Vpc vpc = _vpcDao.findById(vpcId);
routers = _routerDao.listByVpcId(vpcId);
if (vpc.isRedundant()) {
buf.append(" redundant_router=1");
}
long advertInt = vpc.getAdvertInterval();
if (advertInt <= 0) {
advertInt = NumbersUtil.parseLong(_configDao.getValue(Config.RedundantRouterVrrpInterval.key()), 1);
}
buf.append(" advert_int=").append(advertInt);
String unicastSubnet = vpc.getUnicastSubnet();
if (unicastSubnet == null || unicastSubnet.isEmpty() || !NetUtils.isValidIp4Cidr(unicastSubnet)) {
unicastSubnet = _configDao.getValue(Config.RedundantRouterUnicastSubnet.key());
if (unicastSubnet == null || unicastSubnet.isEmpty() || !NetUtils.isValidIp4Cidr(unicastSubnet)) {
unicastSubnet = "100.100.0.0/24";
}
}
buf.append(" unicast_subnet=").append(unicastSubnet);
AdvertMethod advertMethod = vpc.getAdvertMethod();
if (advertMethod == null) {
try {
String advertMethodConfigValue = _configDao.getValue(Config.RedundantRouterAdvertMethod.key());
advertMethod = AdvertMethod.valueOf(advertMethodConfigValue);
} catch (final IllegalArgumentException ex) {
advertMethod = AdvertMethod.MULTICAST;
}
}
buf.append(" advert_method=").append(advertMethod);
buf.append(" router_id=").append(vpcId);
buf.append(" unicast_id=").append(router.getRouterUnicastId());
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.MASTER.toString();
router.setRedundantState(RedundantState.MASTER);
} else {
final DomainRouterVO router0 = routers.get(0);
if (router.getId() == router0.getId()) {
redundantState = RedundantState.MASTER.toString();
router.setRedundantState(RedundantState.MASTER);
}
}
// @TODO Remove this
buf.append(" redundant_state=").append(redundantState);
return buf;
}
use of com.cloud.legacymodel.network.vpc.Vpc in project cosmic by MissionCriticalCloud.
the class VpcVirtualNetworkApplianceManagerImpl method finalizeVirtualMachineProfile.
@Override
public boolean finalizeVirtualMachineProfile(final VirtualMachineProfile profile, final DeployDestination dest, final ReservationContext context) {
final DomainRouterVO domainRouterVO = _routerDao.findById(profile.getId());
final Long vpcId = domainRouterVO.getVpcId();
if (vpcId != null) {
if (domainRouterVO.getState() == State.Starting || domainRouterVO.getState() == State.Running) {
String defaultDns1 = null;
String defaultDns2 = null;
// remove public and guest nics as we will plug them later
final Iterator<NicProfile> it = profile.getNics().iterator();
while (it.hasNext()) {
final NicProfile nic = it.next();
final Network network = _networkDao.findById(nic.getNetworkId());
if (nic.getTrafficType() == TrafficType.Public || (TrafficType.Guest.equals(network.getTrafficType()) && !GuestType.Sync.equals(network.getGuestType()))) {
// save dns information
if (nic.getTrafficType() == TrafficType.Public) {
defaultDns1 = nic.getIPv4Dns1();
defaultDns2 = nic.getIPv4Dns2();
}
s_logger.debug("Removing nic " + nic + " of type " + nic.getTrafficType() + " from the nics passed on vm start. " + "The nic will be plugged later");
it.remove();
}
}
// add vpc cidr/dns/networkdomain to the boot load args
final StringBuilder buf = profile.getBootArgsBuilder();
final Vpc vpc = _entityMgr.findById(Vpc.class, vpcId);
buf.append(" vpccidr=" + vpc.getCidr() + " domain=" + vpc.getNetworkDomain());
buf.append(" dns1=").append(defaultDns1);
if (defaultDns2 != null) {
buf.append(" dns2=").append(defaultDns2);
}
}
}
return super.finalizeVirtualMachineProfile(profile, dest, context);
}
use of com.cloud.legacymodel.network.vpc.Vpc 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