Search in sources :

Example 41 with NetworkOfferingVO

use of com.cloud.offerings.NetworkOfferingVO in project cloudstack by apache.

the class CreatePrivateNetworkTest method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    networkService._accountMgr = _accountMgr;
    networkService._networkOfferingDao = _networkOfferingDao;
    networkService._physicalNetworkDao = _physicalNetworkDao;
    networkService._dcDao = _dcDao;
    networkService._networksDao = _networkDao;
    networkService._networkMgr = _networkMgr;
    networkService._privateIpDao = _privateIpDao;
    Account account = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    when(networkService._accountMgr.getAccount(anyLong())).thenReturn(account);
    NetworkOfferingVO ntwkOff = new NetworkOfferingVO("offer", "fakeOffer", TrafficType.Guest, true, true, null, null, false, null, null, GuestType.Isolated, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false);
    when(networkService._networkOfferingDao.findById(anyLong())).thenReturn(ntwkOff);
    List<NetworkOfferingVO> netofferlist = new ArrayList<NetworkOfferingVO>();
    netofferlist.add(ntwkOff);
    when(networkService._networkOfferingDao.listSystemNetworkOfferings()).thenReturn(netofferlist);
    PhysicalNetworkVO physicalNetwork = new PhysicalNetworkVO(1L, 1L, "2-5", "200", 1L, null, "testphysicalnetwork");
    when(networkService._physicalNetworkDao.findById(anyLong())).thenReturn(physicalNetwork);
    DataCenterVO dc = new DataCenterVO(1L, "hut", "op de hei", null, null, null, null, "10.1.1.0/24", "unreal.net", 1L, NetworkType.Advanced, null, null);
    when(networkService._dcDao.lockRow(anyLong(), anyBoolean())).thenReturn(dc);
    when(networkService._networksDao.getPrivateNetwork(anyString(), anyString(), eq(1L), eq(1L), anyLong(), anyLong())).thenReturn(null);
    Network net = new NetworkVO(1L, TrafficType.Guest, Mode.None, BroadcastDomainType.Vlan, 1L, 1L, 1L, 1L, "bla", "fake", "eet.net", GuestType.Isolated, 1L, 1L, ACLType.Account, false, 1L, false);
    when(networkService._networkMgr.createGuestNetwork(eq(ntwkOff.getId()), eq("bla"), eq("fake"), eq("10.1.1.1"), eq("10.1.1.0/24"), nullable(String.class), nullable(Boolean.class), nullable(String.class), eq(account), nullable(Long.class), eq(physicalNetwork), eq(physicalNetwork.getDataCenterId()), eq(ACLType.Account), nullable(Boolean.class), eq(1L), nullable(String.class), nullable(String.class), nullable(Boolean.class), nullable(String.class), nullable(Network.PVlanType.class), nullable(String.class), nullable(String.class), nullable(String.class))).thenReturn(net);
    when(networkService._networkMgr.createPrivateNetwork(eq(ntwkOff.getId()), eq("bla"), eq("fake"), eq("10.1.1.1"), eq("10.1.1.0/24"), anyString(), anyBoolean(), eq(account), eq(physicalNetwork), eq(1L))).thenReturn(net);
    when(networkService._privateIpDao.findByIpAndSourceNetworkId(net.getId(), "10.1.1.2")).thenReturn(null);
    when(networkService._privateIpDao.findByIpAndSourceNetworkIdAndVpcId(eq(1L), anyString(), eq(1L))).thenReturn(null);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) NetworkVO(com.cloud.network.dao.NetworkVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) AccountVO(com.cloud.user.AccountVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) Matchers.anyLong(org.mockito.Matchers.anyLong) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Before(org.junit.Before)

Example 42 with NetworkOfferingVO

use of com.cloud.offerings.NetworkOfferingVO in project cloudstack by apache.

the class UserVmManagerImpl method updateNicIpForVirtualMachine.

@Override
public UserVm updateNicIpForVirtualMachine(UpdateVmNicIpCmd cmd) {
    Long nicId = cmd.getNicId();
    String ipaddr = cmd.getIpaddress();
    Account caller = CallContext.current().getCallingAccount();
    // check whether the nic belongs to user vm.
    NicVO nicVO = _nicDao.findById(nicId);
    if (nicVO == null) {
        throw new InvalidParameterValueException("There is no nic for the " + nicId);
    }
    if (nicVO.getVmType() != VirtualMachine.Type.User) {
        throw new InvalidParameterValueException("The nic is not belongs to user vm");
    }
    UserVm vm = _vmDao.findById(nicVO.getInstanceId());
    if (vm == null) {
        throw new InvalidParameterValueException("There is no vm with the nic");
    }
    Network network = _networkDao.findById(nicVO.getNetworkId());
    if (network == null) {
        throw new InvalidParameterValueException("There is no network with the nic");
    }
    // Don't allow to update vm nic ip if network is not in Implemented/Setup/Allocated state
    if (!(network.getState() == Network.State.Allocated || network.getState() == Network.State.Implemented || network.getState() == Network.State.Setup)) {
        throw new InvalidParameterValueException("Network is not in the right state to update vm nic ip. Correct states are: " + Network.State.Allocated + ", " + Network.State.Implemented + ", " + Network.State.Setup);
    }
    NetworkOfferingVO offering = _networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId());
    if (offering == null) {
        throw new InvalidParameterValueException("There is no network offering with the network");
    }
    if (!_networkModel.listNetworkOfferingServices(offering.getId()).isEmpty() && vm.getState() != State.Stopped) {
        InvalidParameterValueException ex = new InvalidParameterValueException("VM is not Stopped, unable to update the vm nic having the specified id");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    // verify permissions
    _accountMgr.checkAccess(caller, null, true, vm);
    Account ipOwner = _accountDao.findByIdIncludingRemoved(vm.getAccountId());
    // verify ip address
    s_logger.debug("Calling the ip allocation ...");
    DataCenter dc = _dcDao.findById(network.getDataCenterId());
    if (dc == null) {
        throw new InvalidParameterValueException("There is no dc with the nic");
    }
    if (dc.getNetworkType() == NetworkType.Advanced && network.getGuestType() == Network.GuestType.Isolated) {
        try {
            ipaddr = _ipAddrMgr.allocateGuestIP(network, ipaddr);
        } catch (InsufficientAddressCapacityException e) {
            throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, for insufficient address capacity");
        }
        if (ipaddr == null) {
            throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip");
        }
        if (nicVO.getIPv4Address() != null) {
            updatePublicIpDnatVmIp(vm.getId(), network.getId(), nicVO.getIPv4Address(), ipaddr);
            updateLoadBalancerRulesVmIp(vm.getId(), network.getId(), nicVO.getIPv4Address(), ipaddr);
            updatePortForwardingRulesVmIp(vm.getId(), network.getId(), nicVO.getIPv4Address(), ipaddr);
        }
    } else if (dc.getNetworkType() == NetworkType.Basic || network.getGuestType() == Network.GuestType.Shared) {
        // handle the basic networks here
        // for basic zone, need to provide the podId to ensure proper ip alloation
        Long podId = null;
        if (dc.getNetworkType() == NetworkType.Basic) {
            podId = vm.getPodIdToDeployIn();
            if (podId == null) {
                throw new InvalidParameterValueException("vm pod id is null in Basic zone; can't decide the range for ip allocation");
            }
        }
        try {
            ipaddr = _ipAddrMgr.allocatePublicIpForGuestNic(network, podId, ipOwner, ipaddr);
            if (ipaddr == null) {
                throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip");
            }
            final IPAddressVO newIp = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), ipaddr);
            final Vlan vlan = _vlanDao.findById(newIp.getVlanId());
            nicVO.setIPv4Gateway(vlan.getVlanGateway());
            nicVO.setIPv4Netmask(vlan.getVlanNetmask());
            final IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nicVO.getNetworkId(), nicVO.getIPv4Address());
            if (ip != null) {
                Transaction.execute(new TransactionCallbackNoReturn() {

                    @Override
                    public void doInTransactionWithoutResult(TransactionStatus status) {
                        _ipAddrMgr.markIpAsUnavailable(ip.getId());
                        _ipAddressDao.unassignIpAddress(ip.getId());
                    }
                });
            }
        } catch (InsufficientAddressCapacityException e) {
            s_logger.error("Allocating ip to guest nic " + nicVO.getUuid() + " failed, for insufficient address capacity");
            return null;
        }
    } else {
        throw new InvalidParameterValueException("UpdateVmNicIpCmd is not supported in L2 network");
    }
    s_logger.debug("Updating IPv4 address of NIC " + nicVO + " to " + ipaddr + "/" + nicVO.getIPv4Netmask() + " with gateway " + nicVO.getIPv4Gateway());
    nicVO.setIPv4Address(ipaddr);
    _nicDao.persist(nicVO);
    return vm;
}
Also used : Account(com.cloud.user.Account) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) Vlan(com.cloud.dc.Vlan) UserVm(com.cloud.uservm.UserVm) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 43 with NetworkOfferingVO

use of com.cloud.offerings.NetworkOfferingVO in project cosmic by MissionCriticalCloud.

the class NetworkOrchestrator method rollingRestartIsolatedNetwork.

private boolean rollingRestartIsolatedNetwork(final NetworkVO network, final List<DomainRouterVO> routers, final ReservationContext context) throws ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException {
    final Account caller = CallContext.current().getCallingAccount();
    final long callerUserId = CallContext.current().getCallingUserId();
    final int sleepTimeInMsAfterRouterStart = 10000;
    final int numberOfRoutersWhenSingle = 1;
    final int numberOfRoutersWhenRedundant = 2;
    // check the master and backup redundant state
    DomainRouterVO masterRouter = null;
    DomainRouterVO backupRouter = null;
    if (routers != null && routers.size() == numberOfRoutersWhenSingle) {
        masterRouter = routers.get(0);
    }
    if (routers != null && routers.size() == numberOfRoutersWhenRedundant) {
        final DomainRouterVO router1 = routers.get(0);
        final DomainRouterVO router2 = routers.get(1);
        if (router1.getRedundantState() == RedundantState.MASTER || router2.getRedundantState() == RedundantState.BACKUP) {
            masterRouter = router1;
            backupRouter = router2;
        } else if (router1.getRedundantState() == RedundantState.BACKUP || router2.getRedundantState() == RedundantState.MASTER) {
            masterRouter = router2;
            backupRouter = router1;
        } else {
            // both routers are in UNKNOWN state or in the same state. Order doesn't matter.
            masterRouter = router1;
            backupRouter = router2;
        }
    }
    final NetworkOfferingVO offering = _networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId());
    final DeployDestination dest = new DeployDestination(_zoneRepository.findOne(network.getDataCenterId()), null, null, null);
    final List<Provider> providersToImplement = getNetworkProviders(network.getId());
    // destroy backup router
    if (backupRouter != null) {
        _routerService.destroyRouter(backupRouter.getId(), caller, callerUserId);
    }
    // create new backup router
    implementNetworkElements(dest, context, network, offering, providersToImplement);
    // destroy master router
    if (masterRouter != null) {
        try {
            // wait for the keepalived/conntrackd on router
            Thread.sleep(sleepTimeInMsAfterRouterStart);
        } catch (final InterruptedException e) {
            s_logger.trace("Ignoring InterruptedException.", e);
        }
        _routerService.destroyRouter(masterRouter.getId(), caller, callerUserId);
        // create a new router
        implementNetworkElements(dest, context, network, offering, providersToImplement);
    }
    return true;
}
Also used : Account(com.cloud.user.Account) DeployDestination(com.cloud.deploy.DeployDestination) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) DomainRouterVO(com.cloud.vm.DomainRouterVO) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) LoadBalancingServiceProvider(com.cloud.network.element.LoadBalancingServiceProvider) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) DhcpServiceProvider(com.cloud.network.element.DhcpServiceProvider) Provider(com.cloud.network.Network.Provider)

Example 44 with NetworkOfferingVO

use of com.cloud.offerings.NetworkOfferingVO in project cosmic by MissionCriticalCloud.

the class NetworkOrchestrator method reprogramNetworkRules.

// This method re-programs the rules/ips for existing network
protected boolean reprogramNetworkRules(final long networkId, final Account caller, final Network network) throws ResourceUnavailableException {
    boolean success = true;
    // Apply egress rules first to effect the egress policy early on the guest traffic
    final List<FirewallRuleVO> firewallEgressRulesToApply = _firewallDao.listByNetworkPurposeTrafficType(networkId, Purpose.Firewall, FirewallRule.TrafficType.Egress);
    final NetworkOfferingVO offering = _networkOfferingDao.findById(network.getNetworkOfferingId());
    final Zone zone = _zoneRepository.findOne(network.getDataCenterId());
    if (_networkModel.areServicesSupportedInNetwork(network.getId(), Service.Firewall) && _networkModel.areServicesSupportedInNetwork(network.getId(), Service.Firewall) && (network.getGuestType() == GuestType.Isolated || network.getGuestType() == GuestType.Shared && zone.getNetworkType() == com.cloud.model.enumeration.NetworkType.Advanced)) {
        // add default egress rule to accept the traffic
        _firewallMgr.applyDefaultEgressFirewallRule(network.getId(), offering.getEgressDefaultPolicy(), true);
    }
    if (!_firewallMgr.applyFirewallRules(firewallEgressRulesToApply, false, caller)) {
        s_logger.warn("Failed to reapply firewall Egress rule(s) as a part of network id=" + networkId + " restart");
        success = false;
    }
    // associate all ip addresses
    if (!_ipAddrMgr.applyIpAssociations(network, false)) {
        s_logger.warn("Failed to apply ip addresses as a part of network id" + networkId + " restart");
        success = false;
    }
    // apply static nat
    if (!_rulesMgr.applyStaticNatsForNetwork(networkId, false, caller)) {
        s_logger.warn("Failed to apply static nats a part of network id" + networkId + " restart");
        success = false;
    }
    // apply firewall rules
    final List<FirewallRuleVO> firewallIngressRulesToApply = _firewallDao.listByNetworkPurposeTrafficType(networkId, Purpose.Firewall, FirewallRule.TrafficType.Ingress);
    if (!_firewallMgr.applyFirewallRules(firewallIngressRulesToApply, false, caller)) {
        s_logger.warn("Failed to reapply Ingress firewall rule(s) as a part of network id=" + networkId + " restart");
        success = false;
    }
    // apply port forwarding rules
    if (!_rulesMgr.applyPortForwardingRulesForNetwork(networkId, false, caller)) {
        s_logger.warn("Failed to reapply port forwarding rule(s) as a part of network id=" + networkId + " restart");
        success = false;
    }
    // apply static nat rules
    if (!_rulesMgr.applyStaticNatRulesForNetwork(networkId, false, caller)) {
        s_logger.warn("Failed to reapply static nat rule(s) as a part of network id=" + networkId + " restart");
        success = false;
    }
    // apply public load balancer rules
    if (!_lbMgr.applyLoadBalancersForNetwork(networkId, Scheme.Public)) {
        s_logger.warn("Failed to reapply Public load balancer rules as a part of network id=" + networkId + " restart");
        success = false;
    }
    // apply vpn rules
    final List<? extends RemoteAccessVpn> vpnsToReapply = _vpnMgr.listRemoteAccessVpns(networkId);
    if (vpnsToReapply != null) {
        for (final RemoteAccessVpn vpn : vpnsToReapply) {
            // Start remote access vpn per ip
            if (_vpnMgr.startRemoteAccessVpn(vpn.getServerAddressId(), false) == null) {
                s_logger.warn("Failed to reapply vpn rules as a part of network id=" + networkId + " restart");
                success = false;
            }
        }
    }
    // apply network ACLs
    if (!_networkACLMgr.applyACLToNetwork(networkId)) {
        s_logger.warn("Failed to reapply network ACLs as a part of  of network id=" + networkId + " restart");
        success = false;
    }
    return success;
}
Also used : Zone(com.cloud.db.model.Zone) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO) RemoteAccessVpn(com.cloud.network.RemoteAccessVpn)

Example 45 with NetworkOfferingVO

use of com.cloud.offerings.NetworkOfferingVO in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerImpl method createDefaultSystemNetworks.

@Override
public void createDefaultSystemNetworks(final long zoneId) throws ConcurrentOperationException {
    final DataCenterVO zone = _zoneDao.findById(zoneId);
    final String networkDomain = null;
    // the zone creation
    if (zone != null) {
        final List<NetworkOfferingVO> ntwkOff = _networkOfferingDao.listSystemNetworkOfferings();
        for (final NetworkOfferingVO offering : ntwkOff) {
            final DataCenterDeployment plan = new DataCenterDeployment(zone.getId(), null, null, null, null, null);
            final NetworkVO userNetwork = new NetworkVO();
            final Account systemAccount = _accountDao.findById(Account.ACCOUNT_ID_SYSTEM);
            BroadcastDomainType broadcastDomainType = null;
            if (offering.getTrafficType() == TrafficType.Management) {
                broadcastDomainType = BroadcastDomainType.Native;
            } else if (offering.getTrafficType() == TrafficType.Control) {
                broadcastDomainType = BroadcastDomainType.LinkLocal;
            } else if (offering.getTrafficType() == TrafficType.Public) {
                if (zone.getNetworkType() == NetworkType.Advanced || zone.getNetworkType() == NetworkType.Basic) {
                    broadcastDomainType = BroadcastDomainType.Vlan;
                } else {
                    // so broadcastDomainType remains null! why have None/Undecided/UnKnown?
                    continue;
                }
            } else if (offering.getTrafficType() == TrafficType.Guest) {
                continue;
            }
            userNetwork.setBroadcastDomainType(broadcastDomainType);
            userNetwork.setNetworkDomain(networkDomain);
            _networkMgr.setupNetwork(systemAccount, offering, userNetwork, plan, null, null, false, Domain.ROOT_DOMAIN, null, null, null, null, true, null, null, null);
        }
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) BroadcastDomainType(com.cloud.network.Networks.BroadcastDomainType) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO)

Aggregations

NetworkOfferingVO (com.cloud.offerings.NetworkOfferingVO)128 NetworkVO (com.cloud.network.dao.NetworkVO)45 ArrayList (java.util.ArrayList)34 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)32 Network (com.cloud.network.Network)27 DB (com.cloud.utils.db.DB)27 Test (org.junit.Test)27 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)26 Service (com.cloud.network.Network.Service)26 Account (com.cloud.user.Account)24 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 Provider (com.cloud.network.Network.Provider)22 PhysicalNetwork (com.cloud.network.PhysicalNetwork)21 HashSet (java.util.HashSet)21 TransactionStatus (com.cloud.utils.db.TransactionStatus)20 HashMap (java.util.HashMap)20 Set (java.util.Set)20 DataCenterVO (com.cloud.dc.DataCenterVO)19 NetworkOffering (com.cloud.offering.NetworkOffering)19 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)18