Search in sources :

Example 66 with NetworkVO

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

the class ManagementServerImpl method listCapabilities.

@Override
public Map<String, Object> listCapabilities(final ListCapabilitiesCmd cmd) {
    final Map<String, Object> capabilities = new HashMap<String, Object>();
    final Account caller = getCaller();
    boolean securityGroupsEnabled = false;
    boolean elasticLoadBalancerEnabled = false;
    boolean KVMSnapshotEnabled = false;
    String supportELB = "false";
    final List<NetworkVO> networks = _networkDao.listSecurityGroupEnabledNetworks();
    if (networks != null && !networks.isEmpty()) {
        securityGroupsEnabled = true;
        final String elbEnabled = _configDao.getValue(Config.ElasticLoadBalancerEnabled.key());
        elasticLoadBalancerEnabled = elbEnabled == null ? false : Boolean.parseBoolean(elbEnabled);
        if (elasticLoadBalancerEnabled) {
            final String networkType = _configDao.getValue(Config.ElasticLoadBalancerNetwork.key());
            if (networkType != null) {
                supportELB = networkType;
            }
        }
    }
    final long diskOffMinSize = VolumeOrchestrationService.CustomDiskOfferingMinSize.value();
    final long diskOffMaxSize = VolumeOrchestrationService.CustomDiskOfferingMaxSize.value();
    KVMSnapshotEnabled = Boolean.parseBoolean(_configDao.getValue("KVM.snapshot.enabled"));
    final boolean userPublicTemplateEnabled = TemplateManager.AllowPublicUserTemplates.valueIn(caller.getId());
    // add some parameters UI needs to handle API throttling
    final boolean apiLimitEnabled = Boolean.parseBoolean(_configDao.getValue(Config.ApiLimitEnabled.key()));
    final Integer apiLimitInterval = Integer.valueOf(_configDao.getValue(Config.ApiLimitInterval.key()));
    final Integer apiLimitMax = Integer.valueOf(_configDao.getValue(Config.ApiLimitMax.key()));
    final boolean allowUserViewDestroyedVM = (QueryManagerImpl.AllowUserViewDestroyedVM.valueIn(caller.getId()) | _accountService.isAdmin(caller.getId()));
    final boolean allowUserExpungeRecoverVM = (UserVmManager.AllowUserExpungeRecoverVm.valueIn(caller.getId()) | _accountService.isAdmin(caller.getId()));
    // check if region-wide secondary storage is used
    boolean regionSecondaryEnabled = false;
    final List<ImageStoreVO> imgStores = _imgStoreDao.findRegionImageStores();
    if (imgStores != null && imgStores.size() > 0) {
        regionSecondaryEnabled = true;
    }
    capabilities.put("securityGroupsEnabled", securityGroupsEnabled);
    capabilities.put("userPublicTemplateEnabled", userPublicTemplateEnabled);
    capabilities.put("cloudStackVersion", getVersion());
    capabilities.put("supportELB", supportELB);
    capabilities.put("projectInviteRequired", _projectMgr.projectInviteRequired());
    capabilities.put("allowusercreateprojects", _projectMgr.allowUserToCreateProject());
    capabilities.put("customDiskOffMinSize", diskOffMinSize);
    capabilities.put("customDiskOffMaxSize", diskOffMaxSize);
    capabilities.put("regionSecondaryEnabled", regionSecondaryEnabled);
    capabilities.put("KVMSnapshotEnabled", KVMSnapshotEnabled);
    capabilities.put("allowUserViewDestroyedVM", allowUserViewDestroyedVM);
    capabilities.put("allowUserExpungeRecoverVM", allowUserExpungeRecoverVM);
    if (apiLimitEnabled) {
        capabilities.put("apiLimitInterval", apiLimitInterval);
        capabilities.put("apiLimitMax", apiLimitMax);
    }
    return capabilities;
}
Also used : Account(com.cloud.user.Account) NetworkVO(com.cloud.network.dao.NetworkVO) HashMap(java.util.HashMap) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO)

Example 67 with NetworkVO

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

the class RouterControlHelperTest method testGetRouterControlIpWithRouterIp.

@Test
public void testGetRouterControlIpWithRouterIp() {
    // Prepare
    List<NicVO> nics = new ArrayList<>();
    NicVO nic1 = mock(NicVO.class);
    when(nic1.getNetworkId()).thenReturn(NW_ID_1);
    when(nic1.getIPv4Address()).thenReturn(null);
    nics.add(nic1);
    when(this.nicDao.listByVmId(ROUTER_ID)).thenReturn(nics);
    NetworkVO nw1 = mock(NetworkVO.class);
    when(nw1.getTrafficType()).thenReturn(TrafficType.Public);
    when(this.nwDao.findById(NW_ID_1)).thenReturn(nw1);
    DomainRouterVO router = mock(DomainRouterVO.class);
    when(this.routerDao.findById(ROUTER_ID)).thenReturn(router);
    when(router.getPrivateIpAddress()).thenReturn(IP4_ADDRES1);
    // Execute
    final String ip4address = this.routerControlHelper.getRouterControlIp(ROUTER_ID);
    // Assert
    assertEquals(DIDN_T_GET_THE_EXPECTED_IP4_ADDRESS, IP4_ADDRES1, ip4address);
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) NicVO(com.cloud.vm.NicVO) DomainRouterVO(com.cloud.vm.DomainRouterVO) Test(org.junit.Test)

Example 68 with NetworkVO

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

the class RouterControlHelperTest method testGetRouterControlIp.

@Test
public void testGetRouterControlIp() {
    // Prepare
    List<NicVO> nics = new ArrayList<>();
    NicVO nic1 = mock(NicVO.class);
    NicVO nic2 = mock(NicVO.class);
    // Actually the third one will never be used, but we must assert that is not
    NicVO nic3 = mock(NicVO.class);
    when(nic1.getNetworkId()).thenReturn(NW_ID_1);
    when(nic2.getNetworkId()).thenReturn(NW_ID_2);
    when(nic2.getIPv4Address()).thenReturn(IP4_ADDRES1);
    when(nic3.getNetworkId()).thenReturn(NW_ID_3);
    when(nic3.getIPv4Address()).thenReturn(IP4_ADDRES2);
    nics.add(nic1);
    nics.add(nic2);
    nics.add(nic3);
    when(this.nicDao.listByVmId(ROUTER_ID)).thenReturn(nics);
    NetworkVO nw1 = mock(NetworkVO.class);
    when(nw1.getTrafficType()).thenReturn(TrafficType.Public);
    NetworkVO nw2 = mock(NetworkVO.class);
    when(nw2.getTrafficType()).thenReturn(TrafficType.Control);
    NetworkVO nw3 = mock(NetworkVO.class);
    when(nw3.getTrafficType()).thenReturn(TrafficType.Control);
    when(this.nwDao.findById(NW_ID_1)).thenReturn(nw1);
    when(this.nwDao.findById(NW_ID_2)).thenReturn(nw2);
    when(this.nwDao.findById(NW_ID_3)).thenReturn(nw3);
    // Execute
    final String ip4address = this.routerControlHelper.getRouterControlIp(ROUTER_ID);
    // Assert
    assertEquals(DIDN_T_GET_THE_EXPECTED_IP4_ADDRESS, IP4_ADDRES1, ip4address);
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) NicVO(com.cloud.vm.NicVO) Test(org.junit.Test)

Example 69 with NetworkVO

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

the class UserVmManagerImpl method updateDefaultNicForVirtualMachine.

@Override
@ActionEvent(eventType = EventTypes.EVENT_NIC_UPDATE, eventDescription = "Creating Nic", async = true)
public UserVm updateDefaultNicForVirtualMachine(UpdateDefaultNicForVMCmd cmd) throws InvalidParameterValueException, CloudRuntimeException {
    Long vmId = cmd.getVmId();
    Long nicId = cmd.getNicId();
    Account caller = CallContext.current().getCallingAccount();
    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);
    }
    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
    DataCenterVO dc = _dcDao.findById(vmInstance.getDataCenterId());
    if (dc.getNetworkType() == DataCenter.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
    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;
    List<NicProfile> nicProfiles = _networkMgr.getNicProfiles(vmInstance);
    for (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");
    }
    Network oldDefaultNetwork = null;
    oldDefaultNetwork = _networkModel.getDefaultNetworkForVm(vmId);
    String oldNicIdString = Long.toString(_networkModel.getDefaultNic(vmId).getId());
    long oldNetworkOfferingId = -1L;
    if (oldDefaultNetwork != null) {
        oldNetworkOfferingId = oldDefaultNetwork.getNetworkOfferingId();
    }
    NicVO existingVO = _nicDao.findById(existing.id);
    Integer chosenID = nic.getDeviceId();
    Integer existingID = existing.getDeviceId();
    nic.setDefaultNic(true);
    nic.setDeviceId(existingID);
    existingVO.setDefaultNic(false);
    existingVO.setDeviceId(chosenID);
    nic = _nicDao.persist(nic);
    existingVO = _nicDao.persist(existingVO);
    Network newdefault = null;
    newdefault = _networkModel.getDefaultNetworkForVm(vmId);
    if (newdefault == null) {
        nic.setDefaultNic(false);
        nic.setDeviceId(chosenID);
        existingVO.setDefaultNic(true);
        existingVO.setDeviceId(existingID);
        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);
        String nicIdString = Long.toString(nic.getId());
        long newNetworkOfferingId = network.getNetworkOfferingId();
        UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vmInstance.getAccountId(), vmInstance.getDataCenterId(), vmInstance.getId(), oldNicIdString, oldNetworkOfferingId, null, 1L, VirtualMachine.class.getName(), vmInstance.getUuid(), vmInstance.isDisplay());
        UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vmInstance.getAccountId(), vmInstance.getDataCenterId(), vmInstance.getId(), nicIdString, newNetworkOfferingId, null, 1L, VirtualMachine.class.getName(), vmInstance.getUuid(), vmInstance.isDisplay());
        UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vmInstance.getAccountId(), vmInstance.getDataCenterId(), vmInstance.getId(), nicIdString, newNetworkOfferingId, null, 0L, VirtualMachine.class.getName(), vmInstance.getUuid(), vmInstance.isDisplay());
        UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vmInstance.getAccountId(), vmInstance.getDataCenterId(), vmInstance.getId(), oldNicIdString, oldNetworkOfferingId, null, 0L, VirtualMachine.class.getName(), vmInstance.getUuid(), vmInstance.isDisplay());
        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");
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) NetworkVO(com.cloud.network.dao.NetworkVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) ActionEvent(com.cloud.event.ActionEvent)

Example 70 with NetworkVO

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

the class NetworkOrchestratorTest method testDontRemoveDhcpServiceFromDomainRouter.

@Test
public void testDontRemoveDhcpServiceFromDomainRouter() {
    // make local mocks
    VirtualMachineProfile vm = mock(VirtualMachineProfile.class);
    NicVO nic = mock(NicVO.class);
    NetworkVO network = mock(NetworkVO.class);
    // make sure that release dhcp won't be called
    when(vm.getType()).thenReturn(Type.DomainRouter);
    when(network.getGuruName()).thenReturn(guruName);
    when(testOrchastrator._networksDao.findById(nic.getNetworkId())).thenReturn(network);
    testOrchastrator.removeNic(vm, nic);
    verify(nic, times(1)).setState(Nic.State.Deallocating);
    verify(testOrchastrator._networkModel, never()).getElementImplementingProvider(dhcpProvider);
    verify(testOrchastrator._ntwkSrvcDao, never()).getProviderForServiceInNetwork(network.getId(), Service.Dhcp);
    verify(testOrchastrator._networksDao, times(1)).findById(nic.getNetworkId());
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) NicVO(com.cloud.vm.NicVO) Test(org.junit.Test)

Aggregations

NetworkVO (com.cloud.network.dao.NetworkVO)230 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)108 ArrayList (java.util.ArrayList)79 Test (org.junit.Test)56 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)55 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)49 Account (com.cloud.user.Account)44 Network (com.cloud.network.Network)39 DataCenterVO (com.cloud.dc.DataCenterVO)35 DataCenter (com.cloud.dc.DataCenter)34 NicVO (com.cloud.vm.NicVO)33 NicProfile (com.cloud.vm.NicProfile)27 HostVO (com.cloud.host.HostVO)24 NetworkOffering (com.cloud.offering.NetworkOffering)24 NetworkOfferingVO (com.cloud.offerings.NetworkOfferingVO)22 ReservationContext (com.cloud.vm.ReservationContext)22 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)20 DeployDestination (com.cloud.deploy.DeployDestination)19 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)19 NetworkGuru (com.cloud.network.guru.NetworkGuru)19