Search in sources :

Example 26 with Service

use of com.cloud.network.Network.Service in project cloudstack by apache.

the class ConfigurationManagerImpl method searchForNetworkOfferings.

@Override
public Pair<List<? extends NetworkOffering>, Integer> searchForNetworkOfferings(final ListNetworkOfferingsCmd cmd) {
    Boolean isAscending = Boolean.parseBoolean(_configDao.getValue("sortkey.algorithm"));
    isAscending = isAscending == null ? Boolean.TRUE : isAscending;
    final Filter searchFilter = new Filter(NetworkOfferingVO.class, "sortKey", isAscending, null, null);
    final Account caller = CallContext.current().getCallingAccount();
    final SearchCriteria<NetworkOfferingVO> sc = _networkOfferingDao.createSearchCriteria();
    final Long id = cmd.getId();
    final Object name = cmd.getNetworkOfferingName();
    final Object displayText = cmd.getDisplayText();
    final Object trafficType = cmd.getTrafficType();
    final Object isDefault = cmd.getIsDefault();
    final Object specifyVlan = cmd.getSpecifyVlan();
    final Object availability = cmd.getAvailability();
    final Object state = cmd.getState();
    final Long zoneId = cmd.getZoneId();
    DataCenter zone = null;
    final Long networkId = cmd.getNetworkId();
    final String guestIpType = cmd.getGuestIpType();
    final List<String> supportedServicesStr = cmd.getSupportedServices();
    final Object specifyIpRanges = cmd.getSpecifyIpRanges();
    final String tags = cmd.getTags();
    final Boolean isTagged = cmd.isTagged();
    final Boolean forVpc = cmd.getForVpc();
    if (zoneId != null) {
        zone = _entityMgr.findById(DataCenter.class, zoneId);
        if (zone == null) {
            throw new InvalidParameterValueException("Unable to find the zone by id=" + zoneId);
        }
    }
    final Object keyword = cmd.getKeyword();
    if (keyword != null) {
        final SearchCriteria<NetworkOfferingVO> ssc = _networkOfferingDao.createSearchCriteria();
        ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (name != null) {
        sc.addAnd("name", SearchCriteria.Op.EQ, name);
    }
    if (guestIpType != null) {
        sc.addAnd("guestType", SearchCriteria.Op.EQ, guestIpType);
    }
    if (displayText != null) {
        sc.addAnd("displayText", SearchCriteria.Op.LIKE, "%" + displayText + "%");
    }
    if (trafficType != null) {
        sc.addAnd("trafficType", SearchCriteria.Op.EQ, trafficType);
    }
    if (isDefault != null) {
        sc.addAnd("isDefault", SearchCriteria.Op.EQ, isDefault);
    }
    // only root admin can list network offering with specifyVlan = true
    if (specifyVlan != null) {
        sc.addAnd("specifyVlan", SearchCriteria.Op.EQ, specifyVlan);
    }
    if (availability != null) {
        sc.addAnd("availability", SearchCriteria.Op.EQ, availability);
    }
    if (state != null) {
        sc.addAnd("state", SearchCriteria.Op.EQ, state);
    }
    if (specifyIpRanges != null) {
        sc.addAnd("specifyIpRanges", SearchCriteria.Op.EQ, specifyIpRanges);
    }
    if (zone != null) {
        if (zone.getNetworkType() == NetworkType.Basic) {
            // basic zone, and shouldn't display networkOfferings
            return new Pair<List<? extends NetworkOffering>, Integer>(new ArrayList<NetworkOffering>(), 0);
        }
    }
    // Don't return system network offerings to the user
    sc.addAnd("systemOnly", SearchCriteria.Op.EQ, false);
    // if networkId is specified, list offerings available for upgrade only
    // (for this network)
    Network network = null;
    if (networkId != null) {
        // check if network exists and the caller can operate with it
        network = _networkModel.getNetwork(networkId);
        if (network == null) {
            throw new InvalidParameterValueException("Unable to find the network by id=" + networkId);
        }
        // Don't allow to update system network
        final NetworkOffering offering = _networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId());
        if (offering.isSystemOnly()) {
            throw new InvalidParameterValueException("Can't update system networks");
        }
        _accountMgr.checkAccess(caller, null, true, network);
        final List<Long> offeringIds = _networkModel.listNetworkOfferingsForUpgrade(networkId);
        if (!offeringIds.isEmpty()) {
            sc.addAnd("id", SearchCriteria.Op.IN, offeringIds.toArray());
        } else {
            return new Pair<List<? extends NetworkOffering>, Integer>(new ArrayList<NetworkOffering>(), 0);
        }
    }
    if (id != null) {
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
    }
    if (tags != null) {
        sc.addAnd("tags", SearchCriteria.Op.EQ, tags);
    }
    if (isTagged != null) {
        if (isTagged) {
            sc.addAnd("tags", SearchCriteria.Op.NNULL);
        } else {
            sc.addAnd("tags", SearchCriteria.Op.NULL);
        }
    }
    final List<NetworkOfferingVO> offerings = _networkOfferingDao.search(sc, searchFilter);
    final Boolean sourceNatSupported = cmd.getSourceNatSupported();
    final List<String> pNtwkTags = new ArrayList<String>();
    boolean checkForTags = false;
    if (zone != null) {
        final List<PhysicalNetworkVO> pNtwks = _physicalNetworkDao.listByZoneAndTrafficType(zoneId, TrafficType.Guest);
        if (pNtwks.size() > 1) {
            checkForTags = true;
            // go through tags
            for (final PhysicalNetworkVO pNtwk : pNtwks) {
                final List<String> pNtwkTag = pNtwk.getTags();
                if (pNtwkTag == null || pNtwkTag.isEmpty()) {
                    throw new CloudRuntimeException("Tags are not defined for physical network in the zone id=" + zoneId);
                }
                pNtwkTags.addAll(pNtwkTag);
            }
        }
    }
    // filter by supported services
    final boolean listBySupportedServices = supportedServicesStr != null && !supportedServicesStr.isEmpty() && !offerings.isEmpty();
    final boolean checkIfProvidersAreEnabled = zoneId != null;
    final boolean parseOfferings = listBySupportedServices || sourceNatSupported != null || checkIfProvidersAreEnabled || forVpc != null || network != null;
    if (parseOfferings) {
        final List<NetworkOfferingVO> supportedOfferings = new ArrayList<NetworkOfferingVO>();
        Service[] supportedServices = null;
        if (listBySupportedServices) {
            supportedServices = new Service[supportedServicesStr.size()];
            int i = 0;
            for (final String supportedServiceStr : supportedServicesStr) {
                final Service service = Service.getService(supportedServiceStr);
                if (service == null) {
                    throw new InvalidParameterValueException("Invalid service specified " + supportedServiceStr);
                } else {
                    supportedServices[i] = service;
                }
                i++;
            }
        }
        for (final NetworkOfferingVO offering : offerings) {
            boolean addOffering = true;
            List<Service> checkForProviders = new ArrayList<Service>();
            if (checkForTags) {
                if (!pNtwkTags.contains(offering.getTags())) {
                    continue;
                }
            }
            if (listBySupportedServices) {
                addOffering = addOffering && _networkModel.areServicesSupportedByNetworkOffering(offering.getId(), supportedServices);
            }
            if (checkIfProvidersAreEnabled) {
                if (supportedServices != null && supportedServices.length > 0) {
                    checkForProviders = Arrays.asList(supportedServices);
                } else {
                    checkForProviders = _networkModel.listNetworkOfferingServices(offering.getId());
                }
                addOffering = addOffering && _networkModel.areServicesEnabledInZone(zoneId, offering, checkForProviders);
            }
            if (sourceNatSupported != null) {
                addOffering = addOffering && _networkModel.areServicesSupportedByNetworkOffering(offering.getId(), Network.Service.SourceNat) == sourceNatSupported;
            }
            if (forVpc != null) {
                addOffering = addOffering && isOfferingForVpc(offering) == forVpc.booleanValue();
            } else if (network != null) {
                addOffering = addOffering && isOfferingForVpc(offering) == (network.getVpcId() != null);
            }
            if (addOffering) {
                supportedOfferings.add(offering);
            }
        }
        // Now apply pagination
        final List<? extends NetworkOffering> wPagination = StringUtils.applyPagination(supportedOfferings, cmd.getStartIndex(), cmd.getPageSizeVal());
        if (wPagination != null) {
            final Pair<List<? extends NetworkOffering>, Integer> listWPagination = new Pair<List<? extends NetworkOffering>, Integer>(wPagination, offerings.size());
            return listWPagination;
        }
        return new Pair<List<? extends NetworkOffering>, Integer>(supportedOfferings, supportedOfferings.size());
    } else {
        final List<? extends NetworkOffering> wPagination = StringUtils.applyPagination(offerings, cmd.getStartIndex(), cmd.getPageSizeVal());
        if (wPagination != null) {
            final Pair<List<? extends NetworkOffering>, Integer> listWPagination = new Pair<List<? extends NetworkOffering>, Integer>(wPagination, offerings.size());
            return listWPagination;
        }
        return new Pair<List<? extends NetworkOffering>, Integer>(offerings, offerings.size());
    }
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair) NetworkOffering(com.cloud.offering.NetworkOffering) NetworkOrchestrationService(org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService) Service(com.cloud.network.Network.Service) NetworkService(com.cloud.network.NetworkService) ManagementService(com.cloud.server.ManagementService) ResourceLimitService(com.cloud.user.ResourceLimitService) AffinityGroupService(org.apache.cloudstack.affinity.AffinityGroupService) DataCenter(com.cloud.dc.DataCenter) Filter(com.cloud.utils.db.Filter) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO)

Example 27 with Service

use of com.cloud.network.Network.Service in project cloudstack by apache.

the class UserVmManagerTest method testUpdateVmNicIpFailure1.

// vm is running in network with dhcp support
@Test(expected = InvalidParameterValueException.class)
public void testUpdateVmNicIpFailure1() throws Exception {
    UpdateVmNicIpCmd cmd = new UpdateVmNicIpCmd();
    Class<?> _class = cmd.getClass();
    Field virtualmachineIdField = _class.getDeclaredField("nicId");
    virtualmachineIdField.setAccessible(true);
    virtualmachineIdField.set(cmd, 1L);
    Field accountNameField = _class.getDeclaredField("ipAddr");
    accountNameField.setAccessible(true);
    accountNameField.set(cmd, "10.10.10.10");
    NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
    when(_nicDao.findById(anyLong())).thenReturn(nic);
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
    when(_networkMock.getState()).thenReturn(Network.State.Implemented);
    doReturn(9L).when(_networkMock).getNetworkOfferingId();
    when(_networkOfferingDao.findByIdIncludingRemoved(anyLong())).thenReturn(_networkOfferingMock);
    doReturn(10L).when(_networkOfferingMock).getId();
    List<Service> services = new ArrayList<Service>();
    services.add(Service.Dhcp);
    when(_networkModel.listNetworkOfferingServices(anyLong())).thenReturn(services);
    when(_vmMock.getState()).thenReturn(State.Running);
    Account caller = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, caller);
    try {
        _userVmMgr.updateNicIpForVirtualMachine(cmd);
    } finally {
        CallContext.unregister();
    }
}
Also used : Field(java.lang.reflect.Field) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) ArrayList(java.util.ArrayList) AccountService(com.cloud.user.AccountService) NetworkOrchestrationService(org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService) Service(com.cloud.network.Network.Service) VolumeOrchestrationService(org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService) ResourceLimitService(com.cloud.user.ResourceLimitService) UpdateVmNicIpCmd(org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 28 with Service

use of com.cloud.network.Network.Service in project cloudstack by apache.

the class UserVmManagerTest method testUpdateVmNicIpSuccess1.

@Test
public void testUpdateVmNicIpSuccess1() throws Exception {
    UpdateVmNicIpCmd cmd = new UpdateVmNicIpCmd();
    Class<?> _class = cmd.getClass();
    Field virtualmachineIdField = _class.getDeclaredField("nicId");
    virtualmachineIdField.setAccessible(true);
    virtualmachineIdField.set(cmd, 1L);
    Field accountNameField = _class.getDeclaredField("ipAddr");
    accountNameField.setAccessible(true);
    accountNameField.set(cmd, "10.10.10.10");
    NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
    when(_nicDao.findById(anyLong())).thenReturn(nic);
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
    doReturn(9L).when(_networkMock).getNetworkOfferingId();
    when(_networkOfferingDao.findByIdIncludingRemoved(anyLong())).thenReturn(_networkOfferingMock);
    doReturn(10L).when(_networkOfferingMock).getId();
    List<Service> services = new ArrayList<Service>();
    services.add(Service.Dhcp);
    when(_networkModel.listNetworkOfferingServices(anyLong())).thenReturn(services);
    when(_vmMock.getState()).thenReturn(State.Stopped);
    doNothing().when(_accountMgr).checkAccess(_account, null, true, _vmMock);
    when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(_accountMock);
    when(_networkMock.getState()).thenReturn(Network.State.Implemented);
    when(_networkMock.getDataCenterId()).thenReturn(3L);
    when(_networkMock.getGuestType()).thenReturn(GuestType.Isolated);
    when(_dcDao.findById(anyLong())).thenReturn(_dcMock);
    when(_dcMock.getNetworkType()).thenReturn(NetworkType.Advanced);
    when(_ipAddrMgr.allocateGuestIP(Mockito.eq(_networkMock), anyString())).thenReturn("10.10.10.10");
    doNothing().when(_networkMgr).implementNetworkElementsAndResources(Mockito.any(DeployDestination.class), Mockito.any(ReservationContext.class), Mockito.eq(_networkMock), Mockito.eq(_networkOfferingMock));
    when(_nicDao.persist(any(NicVO.class))).thenReturn(nic);
    Account caller = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, caller);
    try {
        _userVmMgr.updateNicIpForVirtualMachine(cmd);
    } finally {
        CallContext.unregister();
    }
}
Also used : Field(java.lang.reflect.Field) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) DeployDestination(com.cloud.deploy.DeployDestination) ArrayList(java.util.ArrayList) AccountService(com.cloud.user.AccountService) NetworkOrchestrationService(org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService) Service(com.cloud.network.Network.Service) VolumeOrchestrationService(org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService) ResourceLimitService(com.cloud.user.ResourceLimitService) UpdateVmNicIpCmd(org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 29 with Service

use of com.cloud.network.Network.Service in project cloudstack by apache.

the class CreateNetworkOfferingTest method createVpcNtwkOff.

@Test
public void createVpcNtwkOff() {
    Map<Service, Set<Provider>> serviceProviderMap = new HashMap<Network.Service, Set<Network.Provider>>();
    Set<Network.Provider> vrProvider = new HashSet<Network.Provider>();
    vrProvider.add(Provider.VPCVirtualRouter);
    serviceProviderMap.put(Network.Service.Dhcp, vrProvider);
    serviceProviderMap.put(Network.Service.Dns, vrProvider);
    serviceProviderMap.put(Network.Service.Lb, vrProvider);
    serviceProviderMap.put(Network.Service.SourceNat, vrProvider);
    serviceProviderMap.put(Network.Service.Gateway, vrProvider);
    serviceProviderMap.put(Network.Service.Lb, vrProvider);
    NetworkOfferingVO off = configMgr.createNetworkOffering("isolated", "isolated", TrafficType.Guest, null, true, Availability.Optional, 200, serviceProviderMap, false, Network.GuestType.Isolated, false, null, false, null, false, false, null, false, null, true);
    // System.out.println("Creating Vpc Network Offering");
    assertNotNull("Vpc Isolated network offering with Vpc provider ", off);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Network(com.cloud.network.Network) Service(com.cloud.network.Network.Service) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) Provider(com.cloud.network.Network.Provider) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 30 with Service

use of com.cloud.network.Network.Service in project cloudstack by apache.

the class UserVmManagerTest method testUpdateVmNicIpFailure3.

// vm is stopped in shared network in advanced zone
@Test(expected = InvalidParameterValueException.class)
public void testUpdateVmNicIpFailure3() throws Exception {
    UpdateVmNicIpCmd cmd = new UpdateVmNicIpCmd();
    Class<?> _class = cmd.getClass();
    Field virtualmachineIdField = _class.getDeclaredField("nicId");
    virtualmachineIdField.setAccessible(true);
    virtualmachineIdField.set(cmd, 1L);
    Field accountNameField = _class.getDeclaredField("ipAddr");
    accountNameField.setAccessible(true);
    accountNameField.set(cmd, "10.10.10.10");
    NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
    when(_nicDao.findById(anyLong())).thenReturn(nic);
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
    doReturn(9L).when(_networkMock).getNetworkOfferingId();
    when(_networkOfferingDao.findByIdIncludingRemoved(anyLong())).thenReturn(_networkOfferingMock);
    doReturn(10L).when(_networkOfferingMock).getId();
    List<Service> services = new ArrayList<Service>();
    services.add(Service.Dhcp);
    when(_networkModel.listNetworkOfferingServices(anyLong())).thenReturn(services);
    when(_vmMock.getState()).thenReturn(State.Stopped);
    doNothing().when(_accountMgr).checkAccess(_account, null, true, _vmMock);
    when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(_accountMock);
    when(_networkMock.getState()).thenReturn(Network.State.Implemented);
    when(_networkMock.getDataCenterId()).thenReturn(3L);
    when(_networkMock.getGuestType()).thenReturn(GuestType.Shared);
    when(_dcDao.findById(anyLong())).thenReturn(_dcMock);
    when(_dcMock.getNetworkType()).thenReturn(NetworkType.Advanced);
    when(_ipAddrMgr.allocatePublicIpForGuestNic(Mockito.eq(_networkMock), anyLong(), Mockito.eq(_accountMock), anyString())).thenReturn(null);
    Account caller = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, caller);
    try {
        _userVmMgr.updateNicIpForVirtualMachine(cmd);
    } finally {
        CallContext.unregister();
    }
}
Also used : Field(java.lang.reflect.Field) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) ArrayList(java.util.ArrayList) AccountService(com.cloud.user.AccountService) NetworkOrchestrationService(org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService) Service(com.cloud.network.Network.Service) VolumeOrchestrationService(org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService) ResourceLimitService(com.cloud.user.ResourceLimitService) UpdateVmNicIpCmd(org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Aggregations

Service (com.cloud.network.Network.Service)76 HashMap (java.util.HashMap)40 ArrayList (java.util.ArrayList)37 Provider (com.cloud.network.Network.Provider)36 HashSet (java.util.HashSet)36 NetworkOrchestrationService (org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService)31 Set (java.util.Set)30 ResourceLimitService (com.cloud.user.ResourceLimitService)28 Map (java.util.Map)22 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)21 Network (com.cloud.network.Network)18 Capability (com.cloud.network.Network.Capability)17 NetworkOfferingVO (com.cloud.offerings.NetworkOfferingVO)15 NetworkElement (com.cloud.network.element.NetworkElement)14 Test (org.junit.Test)13 UserDataServiceProvider (com.cloud.network.element.UserDataServiceProvider)12 NetworkService (com.cloud.network.NetworkService)11 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)11 NetworkOffering (com.cloud.offering.NetworkOffering)10 Account (com.cloud.user.Account)10