Search in sources :

Example 51 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class NetworkACLManagerTest method driveTestApplyNetworkACL.

public void driveTestApplyNetworkACL(final boolean result, final boolean applyNetworkACLs, final boolean applyACLToPrivateGw) throws Exception {
    // In order to test ONLY our scope method, we mock the others
    final NetworkACLManager aclManager = Mockito.spy(_aclMgr);
    // Prepare
    // Reset mocked objects to reuse
    Mockito.reset(_networkACLItemDao);
    // Make sure it is handled
    final long aclId = 1L;
    final NetworkVO network = Mockito.mock(NetworkVO.class);
    final List<NetworkVO> networks = new ArrayList<>();
    networks.add(network);
    Mockito.when(_networkDao.listByAclId(Matchers.anyLong())).thenReturn(networks);
    Mockito.when(_networkDao.findById(Matchers.anyLong())).thenReturn(network);
    Mockito.when(_networkModel.isProviderSupportServiceInNetwork(Matchers.anyLong(), Matchers.any(Network.Service.class), Matchers.any(Network.Provider.class))).thenReturn(true);
    Mockito.when(_networkAclElements.get(0).applyNetworkACLs(Matchers.any(Network.class), Matchers.anyList())).thenReturn(applyNetworkACLs);
    // Make sure it applies ACL to private gateway
    final List<VpcGatewayVO> vpcGateways = new ArrayList<>();
    final VpcGatewayVO vpcGateway = Mockito.mock(VpcGatewayVO.class);
    final PrivateGateway privateGateway = Mockito.mock(PrivateGateway.class);
    Mockito.when(_vpcSvc.getVpcPrivateGateway(Mockito.anyLong())).thenReturn(privateGateway);
    vpcGateways.add(vpcGateway);
    Mockito.when(_vpcGatewayDao.listByAclIdAndType(aclId, VpcGateway.Type.Private)).thenReturn(vpcGateways);
    // Create 4 rules to test all 4 scenarios: only revoke should
    // be deleted, only add should update
    final List<NetworkACLItemVO> rules = new ArrayList<>();
    final NetworkACLItemVO ruleActive = Mockito.mock(NetworkACLItemVO.class);
    final NetworkACLItemVO ruleStaged = Mockito.mock(NetworkACLItemVO.class);
    final NetworkACLItemVO rule2Revoke = Mockito.mock(NetworkACLItemVO.class);
    final NetworkACLItemVO rule2Add = Mockito.mock(NetworkACLItemVO.class);
    Mockito.when(ruleActive.getState()).thenReturn(NetworkACLItem.State.Active);
    Mockito.when(ruleStaged.getState()).thenReturn(NetworkACLItem.State.Staged);
    Mockito.when(rule2Add.getState()).thenReturn(NetworkACLItem.State.Add);
    Mockito.when(rule2Revoke.getState()).thenReturn(NetworkACLItem.State.Revoke);
    rules.add(ruleActive);
    rules.add(ruleStaged);
    rules.add(rule2Add);
    rules.add(rule2Revoke);
    final long revokeId = 8;
    Mockito.when(rule2Revoke.getId()).thenReturn(revokeId);
    final long addId = 9;
    Mockito.when(rule2Add.getId()).thenReturn(addId);
    Mockito.when(_networkACLItemDao.findById(addId)).thenReturn(rule2Add);
    Mockito.when(_networkACLItemDao.listByACL(aclId)).thenReturn(rules);
    // Mock methods to avoid
    Mockito.doReturn(applyACLToPrivateGw).when(aclManager).applyACLToPrivateGw(privateGateway);
    // Execute
    assertEquals("Result was not congruent with applyNetworkACLs and applyACLToPrivateGw", result, aclManager.applyNetworkACL(aclId));
    // Assert if conditions met, network ACL was applied
    final int timesProcessingDone = applyNetworkACLs && applyACLToPrivateGw ? 1 : 0;
    Mockito.verify(_networkACLItemDao, Mockito.times(timesProcessingDone)).remove(revokeId);
    Mockito.verify(rule2Add, Mockito.times(timesProcessingDone)).setState(NetworkACLItem.State.Active);
    Mockito.verify(_networkACLItemDao, Mockito.times(timesProcessingDone)).update(addId, rule2Add);
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) VpcGatewayVO(com.cloud.network.vpc.VpcGatewayVO) ArrayList(java.util.ArrayList) NetworkOrchestrationService(com.cloud.engine.orchestration.service.NetworkOrchestrationService) VpcService(com.cloud.network.vpc.VpcService) NetworkACLItemVO(com.cloud.network.vpc.NetworkACLItemVO) NetworkACLManager(com.cloud.network.vpc.NetworkACLManager) NetworkACLServiceProvider(com.cloud.network.element.NetworkACLServiceProvider) PrivateGateway(com.cloud.legacymodel.network.vpc.PrivateGateway) Network(com.cloud.legacymodel.network.Network)

Example 52 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class NetworkACLManagerTest method testApplyACL.

@Test
public void testApplyACL() throws Exception {
    final NetworkVO network = Mockito.mock(NetworkVO.class);
    Mockito.when(_networkDao.findById(Matchers.anyLong())).thenReturn(network);
    Mockito.when(_networkModel.isProviderSupportServiceInNetwork(Matchers.anyLong(), Matchers.any(Network.Service.class), Matchers.any(Network.Provider.class))).thenReturn(true);
    Mockito.when(_networkAclElements.get(0).applyNetworkACLs(Matchers.any(Network.class), Matchers.anyList())).thenReturn(true);
    assertTrue(_aclMgr.applyACLToNetwork(1L));
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) Network(com.cloud.legacymodel.network.Network) NetworkOrchestrationService(com.cloud.engine.orchestration.service.NetworkOrchestrationService) VpcService(com.cloud.network.vpc.VpcService) NetworkACLServiceProvider(com.cloud.network.element.NetworkACLServiceProvider) Test(org.junit.Test)

Example 53 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class AssociateIPAddrCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    final Account caller = CallContext.current().getCallingAccount();
    if (accountName != null && domainId != null) {
        final Account account = _accountService.finalizeOwner(caller, accountName, domainId, projectId);
        return account.getId();
    } else if (projectId != null) {
        final Project project = _projectService.getProject(projectId);
        if (project != null) {
            if (project.getState() == Project.State.Active) {
                return project.getProjectAccountId();
            } else {
                throw new PermissionDeniedException("Can't add resources to the project with specified projectId in state=" + project.getState() + " as it's no longer active");
            }
        } else {
            throw new InvalidParameterValueException("Unable to find project by ID");
        }
    } else if (networkId != null) {
        final Network network = _networkService.getNetwork(networkId);
        if (network == null) {
            throw new InvalidParameterValueException("Unable to find network by network id specified");
        }
        final NetworkOffering offering = _entityMgr.findById(NetworkOffering.class, network.getNetworkOfferingId());
        final DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
        if (zone.getNetworkType() == NetworkType.Basic && offering.getElasticIp() && offering.getElasticLb()) {
            // shared network with EIP/ELB service.
            return caller.getAccountId();
        }
        return network.getAccountId();
    } else if (vpcId != null) {
        final Vpc vpc = _entityMgr.findById(Vpc.class, getVpcId());
        if (vpc == null) {
            throw new InvalidParameterValueException("Can't find enabled VPC by ID specified");
        }
        return vpc.getAccountId();
    }
    return caller.getAccountId();
}
Also used : Account(com.cloud.legacymodel.user.Account) Project(com.cloud.projects.Project) DataCenter(com.cloud.legacymodel.dc.DataCenter) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) NetworkOffering(com.cloud.offering.NetworkOffering) Network(com.cloud.legacymodel.network.Network) Vpc(com.cloud.legacymodel.network.vpc.Vpc) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException)

Example 54 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class AssociateIPAddrCmd method getNetworkId.

public Long getNetworkId() {
    if (vpcId != null) {
        return null;
    }
    if (networkId != null) {
        return networkId;
    }
    final Long zoneId = getZoneId();
    final DataCenter zone = _entityMgr.findById(DataCenter.class, zoneId);
    if (zone.getNetworkType() == NetworkType.Advanced) {
        final List<? extends Network> networks = _networkService.getIsolatedNetworksOwnedByAccountInZone(getZoneId(), _accountService.getAccount(getEntityOwnerId()));
        if (networks.size() == 0) {
            final String domain = _domainService.getDomain(getDomainId()).getName();
            throw new InvalidParameterValueException("Account name=" + getAccountName() + " domain=" + domain + " doesn't have virtual networks in zone=" + zone.getName());
        }
        if (networks.size() < 1) {
            throw new InvalidParameterValueException("Account doesn't have any isolated networks in the zone");
        } else if (networks.size() > 1) {
            throw new InvalidParameterValueException("Account has more than one isolated network in the zone");
        }
        return networks.get(0).getId();
    } else {
        final Network defaultGuestNetwork = _networkService.getExclusiveGuestNetwork(zoneId);
        if (defaultGuestNetwork == null) {
            throw new InvalidParameterValueException("Unable to find a default guest network for account " + getAccountName() + " in domain ID=" + getDomainId());
        } else {
            return defaultGuestNetwork.getId();
        }
    }
}
Also used : DataCenter(com.cloud.legacymodel.dc.DataCenter) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Network(com.cloud.legacymodel.network.Network)

Example 55 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class CreateEgressFirewallRuleCmd method getVpcId.

public Long getVpcId() {
    final Network network = _networkService.getNetwork(getNetworkId());
    if (network == null) {
        throw new InvalidParameterValueException("Invalid networkId is given");
    }
    final Long vpcId = network.getVpcId();
    return vpcId;
}
Also used : InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Network(com.cloud.legacymodel.network.Network)

Aggregations

Network (com.cloud.legacymodel.network.Network)160 ArrayList (java.util.ArrayList)57 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)56 Account (com.cloud.legacymodel.user.Account)46 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)42 NetworkOffering (com.cloud.offering.NetworkOffering)36 PhysicalNetwork (com.cloud.network.PhysicalNetwork)34 IPAddressVO (com.cloud.network.dao.IPAddressVO)32 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)30 NetworkVO (com.cloud.network.dao.NetworkVO)28 List (java.util.List)28 Zone (com.cloud.db.model.Zone)27 DB (com.cloud.utils.db.DB)27 NicProfile (com.cloud.vm.NicProfile)26 Nic (com.cloud.legacymodel.network.Nic)21 DataCenter (com.cloud.legacymodel.dc.DataCenter)20 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)20 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)19 DomainRouterVO (com.cloud.vm.DomainRouterVO)18 ActionEvent (com.cloud.event.ActionEvent)17