Search in sources :

Example 6 with NetworkDomainVO

use of com.cloud.network.dao.NetworkDomainVO in project cosmic by MissionCriticalCloud.

the class NetworkModelImpl method isNetworkAvailableInDomain.

@Override
public boolean isNetworkAvailableInDomain(final long networkId, final long domainId) {
    final Long networkDomainId;
    final Network network = getNetwork(networkId);
    if (network.getGuestType() != Network.GuestType.Shared) {
        s_logger.trace("Network id=" + networkId + " is not shared");
        return false;
    }
    final NetworkDomainVO networkDomainMap = _networkDomainDao.getDomainNetworkMapByNetworkId(networkId);
    if (networkDomainMap == null) {
        s_logger.trace("Network id=" + networkId + " is shared, but not domain specific");
        return true;
    } else {
        networkDomainId = networkDomainMap.getDomainId();
    }
    if (domainId == networkDomainId.longValue()) {
        return true;
    }
    if (networkDomainMap.subdomainAccess) {
        final Set<Long> parentDomains = _domainMgr.getDomainParentIds(domainId);
        if (parentDomains.contains(networkDomainId)) {
            return true;
        }
    }
    return false;
}
Also used : NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO)

Example 7 with NetworkDomainVO

use of com.cloud.network.dao.NetworkDomainVO in project cloudstack by apache.

the class NetworkOrchestrator method destroyNetwork.

@Override
@DB
public boolean destroyNetwork(final long networkId, final ReservationContext context, final boolean forced) {
    final Account callerAccount = context.getAccount();
    NetworkVO network = _networksDao.findById(networkId);
    if (network == null) {
        s_logger.debug("Unable to find network with id: " + networkId);
        return false;
    }
    // Make sure that there are no user vms in the network that are not Expunged/Error
    final List<UserVmVO> userVms = _userVmDao.listByNetworkIdAndStates(networkId);
    for (final UserVmVO vm : userVms) {
        if (!(vm.getState() == VirtualMachine.State.Expunging && vm.getRemoved() != null)) {
            s_logger.warn("Can't delete the network, not all user vms are expunged. Vm " + vm + " is in " + vm.getState() + " state");
            return false;
        }
    }
    // Don't allow to delete network via api call when it has vms assigned to it
    final int nicCount = getActiveNicsInNetwork(networkId);
    if (nicCount > 0) {
        s_logger.debug("The network id=" + networkId + " has active Nics, but shouldn't.");
        // at this point we have already determined that there are no active user vms in network
        // if the op_networks table shows active nics, it's a bug in releasing nics updating op_networks
        _networksDao.changeActiveNicsBy(networkId, -1 * nicCount);
    }
    // In Basic zone, make sure that there are no non-removed console proxies and SSVMs using the network
    final DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
    if (zone.getNetworkType() == NetworkType.Basic) {
        final List<VMInstanceVO> systemVms = _vmDao.listNonRemovedVmsByTypeAndNetwork(network.getId(), Type.ConsoleProxy, Type.SecondaryStorageVm);
        if (systemVms != null && !systemVms.isEmpty()) {
            s_logger.warn("Can't delete the network, not all consoleProxy/secondaryStorage vms are expunged");
            return false;
        }
    }
    cleanupPersistentnNetworkResources(network);
    // Shutdown network first
    shutdownNetwork(networkId, context, false);
    // get updated state for the network
    network = _networksDao.findById(networkId);
    if (network.getState() != Network.State.Allocated && network.getState() != Network.State.Setup && !forced) {
        s_logger.debug("Network is not not in the correct state to be destroyed: " + network.getState());
        return false;
    }
    boolean success = true;
    if (!cleanupNetworkResources(networkId, callerAccount, context.getCaller().getId())) {
        s_logger.warn("Unable to delete network id=" + networkId + ": failed to cleanup network resources");
        return false;
    }
    // get providers to destroy
    final List<Provider> providersToDestroy = getNetworkProviders(network.getId());
    for (final NetworkElement element : networkElements) {
        if (providersToDestroy.contains(element.getProvider())) {
            try {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Sending destroy to " + element);
                }
                if (!element.destroy(network, context)) {
                    success = false;
                    s_logger.warn("Unable to complete destroy of the network: failed to destroy network element " + element.getName());
                }
            } catch (final ResourceUnavailableException e) {
                s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e);
                success = false;
            } catch (final ConcurrentOperationException e) {
                s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e);
                success = false;
            } catch (final Exception e) {
                s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e);
                success = false;
            }
        }
    }
    if (success) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Network id=" + networkId + " is destroyed successfully, cleaning up corresponding resources now.");
        }
        final NetworkVO networkFinal = network;
        try {
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(final TransactionStatus status) {
                    final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, networkFinal.getGuruName());
                    if (!guru.trash(networkFinal, _networkOfferingDao.findById(networkFinal.getNetworkOfferingId()))) {
                        throw new CloudRuntimeException("Failed to trash network.");
                    }
                    if (!deleteVlansInNetwork(networkFinal.getId(), context.getCaller().getId(), callerAccount)) {
                        s_logger.warn("Failed to delete network " + networkFinal + "; was unable to cleanup corresponding ip ranges");
                        throw new CloudRuntimeException("Failed to delete network " + networkFinal + "; was unable to cleanup corresponding ip ranges");
                    } else {
                        // commit transaction only when ips and vlans for the network are released successfully
                        try {
                            stateTransitTo(networkFinal, Event.DestroyNetwork);
                        } catch (final NoTransitionException e) {
                            s_logger.debug(e.getMessage());
                        }
                        if (_networksDao.remove(networkFinal.getId())) {
                            final NetworkDomainVO networkDomain = _networkDomainDao.getDomainNetworkMapByNetworkId(networkFinal.getId());
                            if (networkDomain != null) {
                                _networkDomainDao.remove(networkDomain.getId());
                            }
                            final NetworkAccountVO networkAccount = _networkAccountDao.getAccountNetworkMapByNetworkId(networkFinal.getId());
                            if (networkAccount != null) {
                                _networkAccountDao.remove(networkAccount.getId());
                            }
                            networkDetailsDao.removeDetails(networkFinal.getId());
                        }
                        final NetworkOffering ntwkOff = _entityMgr.findById(NetworkOffering.class, networkFinal.getNetworkOfferingId());
                        final boolean updateResourceCount = resourceCountNeedsUpdate(ntwkOff, networkFinal.getAclType());
                        if (updateResourceCount) {
                            _resourceLimitMgr.decrementResourceCount(networkFinal.getAccountId(), ResourceType.network, networkFinal.getDisplayNetwork());
                        }
                    }
                }
            });
            if (_networksDao.findById(network.getId()) == null) {
                // remove its related ACL permission
                final Pair<Class<?>, Long> networkMsg = new Pair<Class<?>, Long>(Network.class, networkFinal.getId());
                _messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, networkMsg);
            }
            return true;
        } catch (final CloudRuntimeException e) {
            s_logger.error("Failed to delete network", e);
            return false;
        }
    }
    return success;
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NetworkElement(com.cloud.network.element.NetworkElement) ConfigDriveNetworkElement(com.cloud.network.element.ConfigDriveNetworkElement) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO) Pair(com.cloud.utils.Pair) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NetworkOffering(com.cloud.offering.NetworkOffering) NetworkGuru(com.cloud.network.guru.NetworkGuru) VMInstanceVO(com.cloud.vm.VMInstanceVO) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) ConfigurationException(javax.naming.ConfigurationException) DnsServiceProvider(com.cloud.network.element.DnsServiceProvider) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) DhcpServiceProvider(com.cloud.network.element.DhcpServiceProvider) LoadBalancingServiceProvider(com.cloud.network.element.LoadBalancingServiceProvider) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) Provider(com.cloud.network.Network.Provider) DataCenter(com.cloud.dc.DataCenter) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) NetworkAccountVO(com.cloud.network.dao.NetworkAccountVO) DB(com.cloud.utils.db.DB)

Example 8 with NetworkDomainVO

use of com.cloud.network.dao.NetworkDomainVO in project cloudstack by apache.

the class NetworkMigrationManagerImpl method makeCopyOfNetwork.

@Override
public long makeCopyOfNetwork(Network network, NetworkOffering networkOffering, Long vpcId) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Making a copy of network with uuid " + network.getUuid() + " and id " + network.getId() + " for migration.");
    }
    long originalNetworkId = network.getId();
    NetworkDomainVO domainNetworkMapByNetworkId = _networkDomainDao.getDomainNetworkMapByNetworkId(originalNetworkId);
    AccountVO networkAccount = _accountDao.findById(network.getAccountId());
    boolean subdomainAccess = true;
    if (domainNetworkMapByNetworkId != null) {
        subdomainAccess = domainNetworkMapByNetworkId.isSubdomainAccess();
    }
    DataCenterDeployment plan = new DataCenterDeployment(network.getDataCenterId(), null, null, null, null, network.getPhysicalNetworkId());
    List<? extends Network> networks = _networkMgr.setupNetwork(networkAccount, networkOffering, network, plan, network.getName(), network.getDisplayText(), true, network.getDomainId(), network.getAclType(), subdomainAccess, vpcId, true);
    _resourceLimitMgr.incrementResourceCount(network.getAccountId(), Resource.ResourceType.network, network.isDisplay());
    long networkCopyId;
    if (networks == null || networks.isEmpty()) {
        throw new CloudRuntimeException("Fail to create a network");
    } else {
        DataCenter zone = _dcDao.findById(network.getDataCenterId());
        String guestNetworkCidr = zone.getGuestNetworkCidr();
        if (networks.get(0).getGuestType() == Network.GuestType.Isolated && networks.get(0).getTrafficType() == Networks.TrafficType.Guest) {
            Network networkCopy = networks.get(0);
            for (final Network nw : networks) {
                if (nw.getCidr() != null && nw.getCidr().equals(guestNetworkCidr)) {
                    networkCopy = nw;
                }
            }
            networkCopyId = networkCopy.getId();
        } else {
            // For shared network
            networkCopyId = networks.get(0).getId();
        }
    }
    // Update the related network
    NetworkVO originalNetwork = _networksDao.findById(originalNetworkId);
    originalNetwork.setRelated(networkCopyId);
    _networksDao.update(originalNetworkId, originalNetwork);
    NetworkVO copiedNetwork = _networksDao.findById(networkCopyId);
    copiedNetwork.setRelated(originalNetworkId);
    copiedNetwork.setDisplayNetwork(false);
    copiedNetwork.setBroadcastUri(network.getBroadcastUri());
    copiedNetwork.setState(network.getState());
    _networksDao.update(networkCopyId, copiedNetwork);
    copyNetworkDetails(originalNetworkId, networkCopyId);
    copyFirewallRulesToNewNetwork(network, networkCopyId);
    assignUserNicsToNewNetwork(originalNetworkId, networkCopyId);
    assignRouterNicsToNewNetwork(network.getId(), networkCopyId);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Succesfully created a copy of network  " + originalNetwork.getName() + "(" + originalNetwork.getUuid() + ") id is " + originalNetwork.getId() + " for migration. The network copy has uuid " + network.getUuid() + " and id " + network.getId());
    }
    return networkCopyId;
}
Also used : RouterNetworkVO(com.cloud.network.dao.RouterNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) DataCenter(com.cloud.dc.DataCenter) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO) AccountVO(com.cloud.user.AccountVO)

Example 9 with NetworkDomainVO

use of com.cloud.network.dao.NetworkDomainVO in project cloudstack by apache.

the class NetworkModelImpl method isNetworkAvailableInDomain.

@Override
public boolean isNetworkAvailableInDomain(long networkId, long domainId) {
    Long networkDomainId = null;
    Network network = getNetwork(networkId);
    if (network.getGuestType() != GuestType.Shared) {
        s_logger.trace("Network id=" + networkId + " is not shared");
        return false;
    }
    NetworkDomainVO networkDomainMap = _networkDomainDao.getDomainNetworkMapByNetworkId(networkId);
    if (networkDomainMap == null) {
        s_logger.trace("Network id=" + networkId + " is shared, but not domain specific");
        return true;
    } else {
        networkDomainId = networkDomainMap.getDomainId();
    }
    if (domainId == networkDomainId.longValue()) {
        return true;
    }
    if (networkDomainMap.subdomainAccess) {
        Set<Long> parentDomains = _domainMgr.getDomainParentIds(domainId);
        if (parentDomains.contains(networkDomainId)) {
            return true;
        }
    }
    return false;
}
Also used : NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO)

Example 10 with NetworkDomainVO

use of com.cloud.network.dao.NetworkDomainVO in project cloudstack by apache.

the class NetworkServiceImpl method listDomainLevelNetworks.

private List<NetworkVO> listDomainLevelNetworks(SearchCriteria<NetworkVO> sc, Filter searchFilter, long domainId, boolean parentDomainsOnly) {
    List<Long> networkIds = new ArrayList<Long>();
    Set<Long> allowedDomains = _domainMgr.getDomainParentIds(domainId);
    List<NetworkDomainVO> maps = _networkDomainDao.listDomainNetworkMapByDomain(allowedDomains.toArray());
    for (NetworkDomainVO map : maps) {
        if (map.getDomainId() == domainId && parentDomainsOnly) {
            continue;
        }
        boolean subdomainAccess = (map.isSubdomainAccess() != null) ? map.isSubdomainAccess() : getAllowSubdomainAccessGlobal();
        if (map.getDomainId() == domainId || subdomainAccess) {
            networkIds.add(map.getNetworkId());
        }
    }
    if (!networkIds.isEmpty()) {
        SearchCriteria<NetworkVO> domainSC = _networksDao.createSearchCriteria();
        domainSC.addAnd("id", SearchCriteria.Op.IN, networkIds.toArray());
        domainSC.addAnd("aclType", SearchCriteria.Op.EQ, ACLType.Domain.toString());
        sc.addAnd("id", SearchCriteria.Op.SC, domainSC);
        return _networksDao.search(sc, searchFilter);
    } else {
        return new ArrayList<NetworkVO>();
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO)

Aggregations

NetworkDomainVO (com.cloud.network.dao.NetworkDomainVO)12 NetworkVO (com.cloud.network.dao.NetworkVO)8 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)6 ArrayList (java.util.ArrayList)6 Pair (com.cloud.utils.Pair)5 DataCenter (com.cloud.dc.DataCenter)3 Domain (com.cloud.domain.Domain)3 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)2 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 NetworkAccountVO (com.cloud.network.dao.NetworkAccountVO)2 Account (com.cloud.user.Account)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 HashSet (java.util.HashSet)2 ACLType (com.cloud.acl.ControlledEntity.ACLType)1 AgentManager (com.cloud.agent.AgentManager)1 Listener (com.cloud.agent.Listener)1 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)1 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)1 Answer (com.cloud.agent.api.Answer)1