use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class DirectPodBasedNetworkGuru method allocate.
@Override
public NicProfile allocate(final Network network, NicProfile nic, final VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
final DataCenterVO dc = _dcDao.findById(network.getDataCenterId());
ReservationStrategy rsStrategy = ReservationStrategy.Start;
_dcDao.loadDetails(dc);
final String dhcpStrategy = dc.getDetail(ZoneConfig.DhcpStrategy.key());
if ("external".equalsIgnoreCase(dhcpStrategy)) {
rsStrategy = ReservationStrategy.Create;
}
if (nic != null && nic.getRequestedIPv4() != null) {
throw new CloudRuntimeException("Does not support custom ip allocation at this time: " + nic);
}
if (nic == null) {
nic = new NicProfile(rsStrategy, null, null, null, null);
} else if (nic.getIPv4Address() == null) {
nic.setReservationStrategy(ReservationStrategy.Start);
} else {
nic.setReservationStrategy(ReservationStrategy.Create);
}
if (rsStrategy == ReservationStrategy.Create) {
final String mac = _networkModel.getNextAvailableMacAddressInNetwork(network.getId());
nic.setMacAddress(mac);
}
return nic;
}
use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class NetworkModelImpl method generateVmData.
@Override
public List<String[]> generateVmData(final String userData, final String serviceOffering, final String zoneName, final String vmName, final long vmId, final String publicKey, final String password, final Boolean isWindows, final Network network) {
final List<String[]> vmData = new ArrayList<>();
if (userData != null) {
vmData.add(new String[] { "userdata", "user-data", new String(Base64.decodeBase64(userData), StringUtils.getPreferredCharset()) });
}
String vmNameFQDN = vmName;
if (network != null) {
vmNameFQDN = vmName + "." + network.getNetworkDomain();
}
vmData.add(new String[] { "metadata", "service-offering", StringUtils.unicodeEscape(serviceOffering) });
vmData.add(new String[] { "metadata", "availability-zone", StringUtils.unicodeEscape(zoneName) });
vmData.add(new String[] { "metadata", "local-hostname", StringUtils.unicodeEscape(vmNameFQDN) });
vmData.add(new String[] { "metadata", "instance-id", vmName });
vmData.add(new String[] { "metadata", "vm-id", String.valueOf(vmId) });
vmData.add(new String[] { "metadata", "public-keys", publicKey });
String cloudIdentifier = _configDao.getValue("cloud.identifier");
if (cloudIdentifier == null) {
cloudIdentifier = "";
} else {
cloudIdentifier = "CloudStack-{" + cloudIdentifier + "}";
}
vmData.add(new String[] { "metadata", "cloud-identifier", cloudIdentifier });
if (password != null && !password.isEmpty() && !password.equals("saved_password")) {
if (isWindows) {
final MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (final NoSuchAlgorithmException e) {
s_logger.error("Unexpected exception " + e.getMessage(), e);
throw new CloudRuntimeException("Unable to get MD5 MessageDigest", e);
}
md5.reset();
md5.update(password.getBytes(StringUtils.getPreferredCharset()));
final byte[] digest = md5.digest();
final BigInteger bigInt = new BigInteger(1, digest);
final String hashtext = bigInt.toString(16);
vmData.add(new String[] { "password", "vm-password-md5checksum", hashtext });
}
vmData.add(new String[] { "password", "vm-password", password });
}
return vmData;
}
use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class NetworkModelImpl method getIpToServices.
@Override
public Map<PublicIpAddress, Set<Service>> getIpToServices(final List<? extends PublicIpAddress> publicIps, final boolean postApplyRules, final boolean includingFirewall) {
final Map<PublicIpAddress, Set<Service>> ipToServices = new HashMap<>();
if (publicIps != null && !publicIps.isEmpty()) {
final Set<Long> networkSNAT = new HashSet<>();
for (final PublicIpAddress ip : publicIps) {
Set<Service> services = ipToServices.get(ip);
if (services == null) {
services = new HashSet<>();
}
if (ip.isSourceNat()) {
if (!networkSNAT.contains(ip.getAssociatedWithNetworkId())) {
services.add(Service.SourceNat);
networkSNAT.add(ip.getAssociatedWithNetworkId());
} else {
final CloudRuntimeException ex = new CloudRuntimeException("Multiple generic soure NAT IPs provided for network");
// see the IPAddressVO.java class.
final IPAddressVO ipAddr = ApiDBUtils.findIpAddressById(ip.getAssociatedWithNetworkId());
String ipAddrUuid = ip.getAssociatedWithNetworkId().toString();
if (ipAddr != null) {
ipAddrUuid = ipAddr.getUuid();
}
ex.addProxyObject(ipAddrUuid, "networkId");
throw ex;
}
}
ipToServices.put(ip, services);
// provider
if (ip.getState() == State.Allocating) {
continue;
}
// check if any active rules are applied on the public IP
Set<Purpose> purposes = getPublicIpPurposeInRules(ip, false, includingFirewall);
// Firewall rules didn't cover static NAT
if (ip.isOneToOneNat() && ip.getAssociatedWithVmId() != null) {
if (purposes == null) {
purposes = new HashSet<>();
}
purposes.add(Purpose.StaticNat);
}
if (purposes == null || purposes.isEmpty()) {
// since no active rules are there check if any rules are applied on the public IP but are in
// revoking state
purposes = getPublicIpPurposeInRules(ip, true, includingFirewall);
if (ip.isOneToOneNat()) {
if (purposes == null) {
purposes = new HashSet<>();
}
purposes.add(Purpose.StaticNat);
}
if (purposes == null || purposes.isEmpty()) {
// IP is not being used for any purpose so skip IPAssoc to network service provider
continue;
} else {
if (postApplyRules) {
// association with the provider
if (ip.isSourceNat()) {
s_logger.debug("Not releasing ip " + ip.getAddress().addr() + " as it is in use for SourceNat");
} else {
ip.setState(State.Releasing);
}
} else {
if (ip.getState() == State.Releasing) {
// rules are not revoked yet, so don't let the network service provider revoke the IP
// association
// mark IP is allocated so that IP association will not be removed from the provider
ip.setState(State.Allocated);
}
}
}
}
if (purposes.contains(Purpose.StaticNat)) {
services.add(Service.StaticNat);
}
if (purposes.contains(Purpose.LoadBalancing)) {
services.add(Service.Lb);
}
if (purposes.contains(Purpose.PortForwarding)) {
services.add(Service.PortForwarding);
}
if (purposes.contains(Purpose.Vpn)) {
services.add(Service.Vpn);
}
if (purposes.contains(Purpose.Firewall)) {
services.add(Service.Firewall);
}
if (services.isEmpty()) {
continue;
}
ipToServices.put(ip, services);
}
}
return ipToServices;
}
use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class NetworkServiceImpl method createPrivateNetwork.
@Override
@DB
public Network createPrivateNetwork(final String networkName, final String displayText, final long physicalNetworkId, final String broadcastUriString, final String startIp, String endIp, final String gateway, final String netmask, final long networkOwnerId, final Long vpcId, final Boolean sourceNat, final Long networkOfferingId) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
final Account owner = _accountMgr.getAccount(networkOwnerId);
// Get system network offering
NetworkOfferingVO ntwkOff = null;
if (networkOfferingId != null) {
ntwkOff = _networkOfferingDao.findById(networkOfferingId);
}
if (ntwkOff == null) {
ntwkOff = findSystemNetworkOffering(NetworkOffering.DefaultPrivateGatewayNetworkOffering);
}
// Validate physical network
final PhysicalNetwork pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
if (pNtwk == null) {
final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a physical network" + " having the given id");
ex.addProxyObject(String.valueOf(physicalNetworkId), "physicalNetworkId");
throw ex;
}
// if end ip is not specified, default it to startIp
if (!NetUtils.isValidIp4(startIp)) {
throw new InvalidParameterValueException("Invalid format for the ip address parameter");
}
if (endIp == null) {
endIp = startIp;
} else if (!NetUtils.isValidIp4(endIp)) {
throw new InvalidParameterValueException("Invalid format for the endIp address parameter");
}
if (!NetUtils.isValidIp4(gateway)) {
throw new InvalidParameterValueException("Invalid gateway");
}
if (!NetUtils.isValidIp4Netmask(netmask)) {
throw new InvalidParameterValueException("Invalid netmask");
}
final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask);
final URI uri = BroadcastDomainType.fromString(broadcastUriString);
final String uriString = uri.toString();
final BroadcastDomainType tiep = BroadcastDomainType.getSchemeValue(uri);
// TODO make a test for any supported scheme
if (!(tiep == BroadcastDomainType.Vlan || tiep == BroadcastDomainType.Lswitch)) {
throw new InvalidParameterValueException("unsupported type of broadcastUri specified: " + broadcastUriString);
}
final NetworkOfferingVO ntwkOffFinal = ntwkOff;
try {
return Transaction.execute(new TransactionCallbackWithException<Network, Exception>() {
@Override
public Network doInTransaction(final TransactionStatus status) throws ResourceAllocationException, InsufficientCapacityException {
// lock datacenter as we need to get mac address seq from there
final DataCenterVO dc = _dcDao.lockRow(pNtwk.getDataCenterId(), true);
// check if we need to create guest network
Network privateNetwork = _networksDao.getPrivateNetwork(uriString, cidr, networkOwnerId, pNtwk.getDataCenterId(), networkOfferingId);
if (privateNetwork == null) {
// create Guest network
privateNetwork = _networkMgr.createGuestNetwork(ntwkOffFinal.getId(), networkName, displayText, gateway, cidr, uriString, null, owner, null, pNtwk, pNtwk.getDataCenterId(), ACLType.Account, null, vpcId, null, null, true, null, dc.getDns1(), dc.getDns2(), null, null, null);
if (privateNetwork != null) {
s_logger.debug("Successfully created guest network " + privateNetwork);
} else {
throw new CloudRuntimeException("Creating guest network failed");
}
} else {
s_logger.debug("Private network already exists: " + privateNetwork);
// Do not allow multiple private gateways with same Vlan within a VPC
if (vpcId != null && vpcId.equals(privateNetwork.getVpcId())) {
throw new InvalidParameterValueException("Private network for the vlan: " + uriString + " and cidr " + cidr + " already exists " + "for Vpc " + vpcId + " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
}
}
if (vpcId != null) {
// add entry to private_ip_address table
PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkIdAndVpcId(privateNetwork.getId(), startIp, vpcId);
if (privateIp != null) {
throw new InvalidParameterValueException("Private ip address " + startIp + " already used for private gateway" + " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
}
final Long mac = dc.getMacAddress();
final Long nextMac = mac + 1;
dc.setMacAddress(nextMac);
privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac, vpcId, sourceNat);
_privateIpDao.persist(privateIp);
_dcDao.update(dc.getId(), dc);
}
s_logger.debug("Private network " + privateNetwork + " is created");
return privateNetwork;
}
});
} catch (final Exception e) {
ExceptionUtil.rethrowRuntime(e);
ExceptionUtil.rethrow(e, ResourceAllocationException.class);
ExceptionUtil.rethrow(e, InsufficientCapacityException.class);
throw new IllegalStateException(e);
}
}
use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class PrivateNetworkGuru method design.
@Override
public Network design(final NetworkOffering offering, final DeploymentPlan plan, final Network userSpecified, final Account owner) {
final PhysicalNetworkVO physnet = physicalNetworkDao.findById(plan.getPhysicalNetworkId());
final DataCenter dc = _entityMgr.findById(DataCenter.class, plan.getDataCenterId());
if (!canHandle(offering, dc, physnet)) {
return null;
}
final BroadcastDomainType broadcastType;
if (userSpecified != null && userSpecified.getBroadcastDomainType() != null) {
broadcastType = userSpecified.getBroadcastDomainType();
} else {
broadcastType = BroadcastDomainType.Vlan;
}
final NetworkVO network = new NetworkVO(offering.getTrafficType(), DHCPMode.Static, broadcastType, offering.getId(), State.Allocated, plan.getDataCenterId(), plan.getPhysicalNetworkId(), offering.getRedundantRouter());
if (userSpecified != null) {
if (!GuestType.Private.equals(offering.getGuestType()) && ((userSpecified.getCidr() == null && userSpecified.getGateway() != null) || (userSpecified.getCidr() != null && userSpecified.getGateway() == null))) {
throw new InvalidParameterValueException("CIDR and gateway must be specified together or the CIDR must represents the gateway.");
}
if (userSpecified.getCidr() != null) {
network.setCidr(userSpecified.getCidr());
network.setGateway(userSpecified.getGateway());
} else {
throw new InvalidParameterValueException("Can't design network " + network + "; netmask/gateway or cidr must be passed in");
}
if (offering.getSpecifyVlan()) {
network.setBroadcastUri(userSpecified.getBroadcastUri());
network.setState(State.Setup);
}
} else {
throw new CloudRuntimeException("Can't design network " + network + "; netmask/gateway or cidr must be passed in");
}
return network;
}
Aggregations