use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.
the class IpAddressManagerImpl method applyRules.
@Override
public boolean applyRules(final List<? extends FirewallRule> rules, final FirewallRule.Purpose purpose, final NetworkRuleApplier applier, final boolean continueOnError) throws ResourceUnavailableException {
if (rules == null || rules.size() == 0) {
s_logger.debug("There are no rules to forward to the network elements");
return true;
}
boolean success = true;
final Network network = _networksDao.findById(rules.get(0).getNetworkId());
final FirewallRuleVO.TrafficType trafficType = rules.get(0).getTrafficType();
final List<PublicIp> publicIps = new ArrayList<>();
if (!(rules.get(0).getPurpose() == FirewallRule.Purpose.Firewall && trafficType == FirewallRule.TrafficType.Egress)) {
// get the list of public ip's owned by the network
final List<IPAddressVO> userIps = _ipAddressDao.listByAssociatedNetwork(network.getId(), null);
if (userIps != null && !userIps.isEmpty()) {
for (final IPAddressVO userIp : userIps) {
final PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
publicIps.add(publicIp);
}
}
}
// the network so as to ensure IP is associated before applying rules (in add state)
if (checkIfIpAssocRequired(network, false, publicIps)) {
applyIpAssociations(network, false, continueOnError, publicIps);
}
try {
applier.applyRules(network, purpose, rules);
} catch (final ResourceUnavailableException e) {
if (!continueOnError) {
throw e;
}
s_logger.warn("Problems with applying " + purpose + " rules but pushing on", e);
success = false;
}
// This IPAssoc ensures, public IP is dis-associated after last active rule is revoked.
if (checkIfIpAssocRequired(network, true, publicIps)) {
applyIpAssociations(network, true, continueOnError, publicIps);
}
return success;
}
use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.
the class Ipv6AddressManagerImpl method assignDirectIp6Address.
@Override
public UserIpv6Address assignDirectIp6Address(final long dcId, final Account owner, final Long networkId, final String requestedIp6) throws InsufficientAddressCapacityException {
final Network network = _networkDao.findById(networkId);
if (network == null) {
return null;
}
final List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId);
if (vlans == null) {
s_logger.debug("Cannot find related vlan attached to network " + networkId);
return null;
}
String ip = null;
Vlan ipVlan = null;
if (requestedIp6 == null) {
if (!_networkModel.isIP6AddressAvailableInNetwork(networkId)) {
throw new InsufficientAddressCapacityException("There is no more address available in the network " + network.getName(), DataCenter.class, network.getDataCenterId());
}
for (final Vlan vlan : vlans) {
if (!_networkModel.isIP6AddressAvailableInVlan(vlan.getId())) {
continue;
}
ip = NetUtils.getIp6FromRange(vlan.getIp6Range());
int count = 0;
while (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) != null) {
ip = NetUtils.getNextIp6InRange(ip, vlan.getIp6Range());
count++;
// It's an arbitrate number to prevent the infinite loop
if (count > _ipv6RetryMax) {
ip = null;
break;
}
}
if (ip != null) {
ipVlan = vlan;
}
}
if (ip == null) {
throw new InsufficientAddressCapacityException("Cannot find a usable IP in the network " + network.getName() + " after " + _ipv6RetryMax + "(network.ipv6.search.retry.max) times retry!", DataCenter.class, network.getDataCenterId());
}
} else {
for (final Vlan vlan : vlans) {
if (NetUtils.isIp6InRange(requestedIp6, vlan.getIp6Range())) {
ipVlan = vlan;
break;
}
}
if (ipVlan == null) {
throw new CloudRuntimeException("Requested IPv6 is not in the predefined range!");
}
ip = requestedIp6;
if (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) != null) {
throw new CloudRuntimeException("The requested IP is already taken!");
}
}
final Zone zone = zoneRepository.findById(dcId).orElse(null);
final Long mac = zone.getMacAddress();
final Long nextMac = mac + 1;
zone.setMacAddress(nextMac);
zoneRepository.save(zone);
final String macAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(mac));
final UserIpv6AddressVO ipVO = new UserIpv6AddressVO(ip, dcId, macAddress, ipVlan.getId());
ipVO.setPhysicalNetworkId(network.getPhysicalNetworkId());
ipVO.setSourceNetworkId(networkId);
ipVO.setState(UserIpv6Address.State.Allocated);
ipVO.setDomainId(owner.getDomainId());
ipVO.setAccountId(owner.getAccountId());
_ipv6Dao.persist(ipVO);
return ipVO;
}
use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.
the class NetworkModelImpl method isIP6AddressAvailableInNetwork.
@Override
public boolean isIP6AddressAvailableInNetwork(final long networkId) {
final Network network = _networksDao.findById(networkId);
if (network == null) {
return false;
}
if (network.getIp6Gateway() == null) {
return false;
}
final List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId);
for (final Vlan vlan : vlans) {
if (isIP6AddressAvailableInVlan(vlan.getId())) {
return true;
}
}
return false;
}
use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method updateDefaultNicForVirtualMachine.
@Override
@ActionEvent(eventType = EventTypes.EVENT_NIC_UPDATE, eventDescription = "Creating Nic", async = true)
public UserVm updateDefaultNicForVirtualMachine(final UpdateDefaultNicForVMCmd cmd) throws InvalidParameterValueException, CloudRuntimeException {
final Long vmId = cmd.getVmId();
final Long nicId = cmd.getNicId();
final Account caller = CallContext.current().getCallingAccount();
final UserVmVO vmInstance = _vmDao.findById(vmId);
if (vmInstance == null) {
throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
}
// Check that Vm does not have VM Snapshots
if (_vmSnapshotDao.findByVm(vmId).size() > 0) {
throw new InvalidParameterValueException("NIC cannot be updated for VM with VM Snapshots");
}
NicVO nic = _nicDao.findById(nicId);
if (nic == null) {
throw new InvalidParameterValueException("unable to find a nic with id " + nicId);
}
final NetworkVO network = _networkDao.findById(nic.getNetworkId());
if (network == null) {
throw new InvalidParameterValueException("unable to find a network with id " + nic.getNetworkId());
}
// Perform permission check on VM
_accountMgr.checkAccess(caller, null, true, vmInstance);
// Verify that zone is not Basic
final Zone zone = zoneRepository.findById(vmInstance.getDataCenterId()).orElse(null);
if (zone.getNetworkType() == NetworkType.Basic) {
throw new CloudRuntimeException("Zone " + vmInstance.getDataCenterId() + ", has a NetworkType of Basic. Can't change default NIC on a Basic Network");
}
// no need to check permissions for network, we'll enumerate the ones they already have access to
final Network existingdefaultnet = _networkModel.getDefaultNetworkForVm(vmId);
// check to see if nic is attached to VM
if (nic.getInstanceId() != vmId) {
throw new InvalidParameterValueException(nic + " is not a nic on " + vmInstance);
}
// if current default equals chosen new default, Throw an exception
if (nic.isDefaultNic()) {
throw new CloudRuntimeException("refusing to set default nic because chosen nic is already the default");
}
// make sure the VM is Running or Stopped
if (vmInstance.getState() != State.Running && vmInstance.getState() != State.Stopped) {
throw new CloudRuntimeException("refusing to set default " + vmInstance + " is not Running or Stopped");
}
NicProfile existing = null;
final List<NicProfile> nicProfiles = _networkMgr.getNicProfiles(vmInstance);
for (final NicProfile nicProfile : nicProfiles) {
if (nicProfile.isDefaultNic() && existingdefaultnet != null && nicProfile.getNetworkId() == existingdefaultnet.getId()) {
existing = nicProfile;
}
}
if (existing == null) {
s_logger.warn("Failed to update default nic, no nic profile found for existing default network");
throw new CloudRuntimeException("Failed to find a nic profile for the existing default network. This is bad and probably means some sort of configuration corruption");
}
NicVO existingVO = _nicDao.findById(existing.id);
nic.setDefaultNic(true);
existingVO.setDefaultNic(false);
nic = _nicDao.persist(nic);
existingVO = _nicDao.persist(existingVO);
Network newdefault = _networkModel.getDefaultNetworkForVm(vmId);
if (newdefault == null) {
nic.setDefaultNic(false);
existingVO.setDefaultNic(true);
nic = _nicDao.persist(nic);
_nicDao.persist(existingVO);
newdefault = _networkModel.getDefaultNetworkForVm(vmId);
if (newdefault.getId() == existingdefaultnet.getId()) {
throw new CloudRuntimeException("Setting a default nic failed, and we had no default nic, but we were able to set it back to the original");
}
throw new CloudRuntimeException("Failed to change default nic to " + nic + " and now we have no default");
} else if (newdefault.getId() == nic.getNetworkId()) {
s_logger.debug("successfully set default network to " + network + " for " + vmInstance);
return _vmDao.findById(vmInstance.getId());
}
throw new CloudRuntimeException("something strange happened, new default network(" + newdefault.getId() + ") is not null, and is not equal to the network(" + nic.getNetworkId() + ") of the chosen nic");
}
use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method setupVmForPvlan.
@Override
public boolean setupVmForPvlan(final boolean add, final Long hostId, final NicProfile nic) {
if (!nic.getBroadCastUri().getScheme().equals("pvlan")) {
return false;
}
String op = "add";
if (!add) {
op = "delete";
}
final Network network = _networkDao.findById(nic.getNetworkId());
final Host host = _hostDao.findById(hostId);
final String networkTag = _networkModel.getNetworkTag(host.getHypervisorType(), network);
final PvlanSetupCommand cmd = PvlanSetupCommand.createVmSetup(op, nic.getBroadCastUri(), networkTag, nic.getMacAddress());
final Answer answer;
try {
answer = _agentMgr.send(hostId, cmd);
} catch (final OperationTimedoutException e) {
s_logger.warn("Timed Out", e);
return false;
} catch (final AgentUnavailableException e) {
s_logger.warn("Agent Unavailable ", e);
return false;
}
boolean result = true;
if (answer == null || !answer.getResult()) {
result = false;
}
return result;
}
Aggregations