Search in sources :

Example 6 with IpAddress

use of com.cloud.network.IpAddress in project cloudstack by apache.

the class CiscoVnmcElementTest method applyPRulesTest.

@Test
public void applyPRulesTest() throws ResourceUnavailableException {
    URI uri = URI.create("vlan://123");
    Network network = mock(Network.class);
    when(network.getId()).thenReturn(1L);
    when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vlan);
    when(network.getDataCenterId()).thenReturn(1L);
    when(network.getBroadcastUri()).thenReturn(uri);
    when(network.getCidr()).thenReturn("1.1.1.0/24");
    when(network.getState()).thenReturn(Network.State.Implemented);
    Ip ip = mock(Ip.class);
    when(ip.addr()).thenReturn("1.2.3.4");
    IpAddress ipAddress = mock(IpAddress.class);
    when(ipAddress.getAddress()).thenReturn(ip);
    when(ipAddress.getVlanId()).thenReturn(1L);
    when(_networkModel.getIp(anyLong())).thenReturn(ipAddress);
    when(_networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.PortForwarding, Provider.CiscoVnmc)).thenReturn(true);
    List<CiscoVnmcControllerVO> devices = new ArrayList<CiscoVnmcControllerVO>();
    devices.add(mock(CiscoVnmcControllerVO.class));
    when(_ciscoVnmcDao.listByPhysicalNetwork(network.getPhysicalNetworkId())).thenReturn(devices);
    when(_networkAsa1000vMapDao.findByNetworkId(network.getId())).thenReturn(mock(NetworkAsa1000vMapVO.class));
    HostVO hostVO = mock(HostVO.class);
    when(hostVO.getId()).thenReturn(1L);
    when(_hostDao.findById(anyLong())).thenReturn(hostVO);
    VlanVO vlanVO = mock(VlanVO.class);
    when(vlanVO.getVlanTag()).thenReturn(null);
    when(_vlanDao.findById(anyLong())).thenReturn(vlanVO);
    PortForwardingRule rule = mock(PortForwardingRule.class);
    when(rule.getSourceIpAddressId()).thenReturn(1L);
    when(rule.getDestinationIpAddress()).thenReturn(ip);
    List<PortForwardingRule> rules = new ArrayList<PortForwardingRule>();
    rules.add(rule);
    Answer answer = mock(Answer.class);
    when(answer.getResult()).thenReturn(true);
    when(_agentMgr.easySend(anyLong(), any(SetPortForwardingRulesCommand.class))).thenReturn(answer);
    assertTrue(_element.applyPFRules(network, rules));
}
Also used : Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) URI(java.net.URI) PortForwardingRule(com.cloud.network.rules.PortForwardingRule) HostVO(com.cloud.host.HostVO) Answer(com.cloud.agent.api.Answer) SetPortForwardingRulesCommand(com.cloud.agent.api.routing.SetPortForwardingRulesCommand) Network(com.cloud.network.Network) NetworkAsa1000vMapVO(com.cloud.network.cisco.NetworkAsa1000vMapVO) IpAddress(com.cloud.network.IpAddress) CiscoVnmcControllerVO(com.cloud.network.cisco.CiscoVnmcControllerVO) VlanVO(com.cloud.dc.VlanVO) Test(org.junit.Test)

Example 7 with IpAddress

use of com.cloud.network.IpAddress in project cloudstack by apache.

the class IntegrationTestConfiguration method entityManager.

@Bean
public EntityManager entityManager() {
    EntityManager mock = Mockito.mock(EntityManager.class);
    try {
        Mockito.when(mock.findById(Matchers.same(Account.class), Matchers.anyLong())).thenReturn(_accountDao.findById(Account.ACCOUNT_ID_SYSTEM));
        Mockito.when(mock.findById(Matchers.same(User.class), Matchers.anyLong())).thenReturn(_userDao.findById(User.UID_SYSTEM));
        Mockito.when(mock.findById(Matchers.same(NetworkOffering.class), Matchers.any(Long.class))).thenAnswer(new Answer<NetworkOffering>() {

            @Override
            public NetworkOffering answer(final InvocationOnMock invocation) throws Throwable {
                Long id = (Long) invocation.getArguments()[1];
                return _networkOfferingDao.findById(id);
            }
        });
        Mockito.when(mock.findById(Matchers.same(IpAddress.class), Matchers.any(Long.class))).thenAnswer(new Answer<IpAddress>() {

            @Override
            public IpAddress answer(final InvocationOnMock invocation) throws Throwable {
                Long id = (Long) invocation.getArguments()[1];
                return _ipAddressDao.findById(id);
            }
        });
        Mockito.when(mock.findById(Matchers.same(DataCenter.class), Matchers.any(Long.class))).thenAnswer(new Answer<DataCenter>() {

            @Override
            public DataCenter answer(final InvocationOnMock invocation) throws Throwable {
                Long id = (Long) invocation.getArguments()[1];
                return _zoneDao.findById(id);
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    CallContext.init(mock);
    return mock;
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) NetworkOffering(com.cloud.offering.NetworkOffering) IOException(java.io.IOException) EntityManager(com.cloud.utils.db.EntityManager) DataCenter(com.cloud.dc.DataCenter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IpAddress(com.cloud.network.IpAddress) Bean(org.springframework.context.annotation.Bean)

Example 8 with IpAddress

use of com.cloud.network.IpAddress in project cloudstack by apache.

the class FirewallManagerImpl method applyRules.

@Override
public boolean applyRules(List<? extends FirewallRule> rules, boolean continueOnError, boolean updateRulesInDB) throws ResourceUnavailableException {
    boolean success = true;
    if (rules == null || rules.size() == 0) {
        s_logger.debug("There are no rules to forward to the network elements");
        return true;
    }
    Purpose purpose = rules.get(0).getPurpose();
    if (!_ipAddrMgr.applyRules(rules, purpose, this, continueOnError)) {
        s_logger.warn("Rules are not completely applied");
        return false;
    } else {
        if (updateRulesInDB) {
            for (FirewallRule rule : rules) {
                if (rule.getState() == FirewallRule.State.Revoke) {
                    FirewallRuleVO relatedRule = _firewallDao.findByRelatedId(rule.getId());
                    if (relatedRule != null) {
                        s_logger.warn("Can't remove the firewall rule id=" + rule.getId() + " as it has related firewall rule id=" + relatedRule.getId() + "; leaving it in Revoke state");
                        success = false;
                    } else {
                        removeRule(rule);
                        if (rule.getSourceIpAddressId() != null) {
                            //if the rule is the last one for the ip address assigned to VPC, unassign it from the network
                            IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId());
                            _vpcMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
                        }
                    }
                } else if (rule.getState() == FirewallRule.State.Add) {
                    FirewallRuleVO ruleVO = _firewallDao.findById(rule.getId());
                    ruleVO.setState(FirewallRule.State.Active);
                    _firewallDao.update(ruleVO.getId(), ruleVO);
                }
            }
        }
    }
    return success;
}
Also used : Purpose(com.cloud.network.rules.FirewallRule.Purpose) IpAddress(com.cloud.network.IpAddress) FirewallRule(com.cloud.network.rules.FirewallRule) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO)

Example 9 with IpAddress

use of com.cloud.network.IpAddress in project cloudstack by apache.

the class VpcVirtualRouterElement method stopSite2SiteVpn.

@Override
public boolean stopSite2SiteVpn(final Site2SiteVpnConnection conn) throws ResourceUnavailableException {
    final Site2SiteVpnGateway vpnGw = _vpnGatewayDao.findById(conn.getVpnGatewayId());
    final IpAddress ip = _ipAddressDao.findById(vpnGw.getAddrId());
    final Map<Capability, String> vpnCapabilities = capabilities.get(Service.Vpn);
    if (!vpnCapabilities.get(Capability.VpnTypes).contains("s2svpn")) {
        s_logger.error("try to stop site 2 site vpn on unsupported network element?");
        return false;
    }
    final Long vpcId = ip.getVpcId();
    final Vpc vpc = _entityMgr.findById(Vpc.class, vpcId);
    if (!_ntwkModel.isProviderEnabledInZone(vpc.getZoneId(), Provider.VPCVirtualRouter.getName())) {
        throw new ResourceUnavailableException("VPC provider is not enabled in zone " + vpc.getZoneId(), DataCenter.class, vpc.getZoneId());
    }
    final List<DomainRouterVO> routers = _vpcRouterMgr.getVpcRouters(ip.getVpcId());
    if (routers == null) {
        throw new ResourceUnavailableException("Cannot enable site-to-site VPN on the backend; virtual router doesn't exist in the vpc " + ip.getVpcId(), DataCenter.class, vpc.getZoneId());
    }
    boolean result = true;
    for (final DomainRouterVO domainRouterVO : routers) {
        result = result && _vpcRouterMgr.stopSite2SiteVpn(conn, domainRouterVO);
    }
    return result;
}
Also used : Site2SiteVpnGateway(com.cloud.network.Site2SiteVpnGateway) Capability(com.cloud.network.Network.Capability) Vpc(com.cloud.network.vpc.Vpc) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 10 with IpAddress

use of com.cloud.network.IpAddress in project cloudstack by apache.

the class VpcVirtualRouterElement method startSite2SiteVpn.

@Override
public boolean startSite2SiteVpn(final Site2SiteVpnConnection conn) throws ResourceUnavailableException {
    final Site2SiteVpnGateway vpnGw = _vpnGatewayDao.findById(conn.getVpnGatewayId());
    final IpAddress ip = _ipAddressDao.findById(vpnGw.getAddrId());
    final Map<Capability, String> vpnCapabilities = capabilities.get(Service.Vpn);
    if (!vpnCapabilities.get(Capability.VpnTypes).contains("s2svpn")) {
        s_logger.error("try to start site 2 site vpn on unsupported network element?");
        return false;
    }
    final Long vpcId = ip.getVpcId();
    final Vpc vpc = _entityMgr.findById(Vpc.class, vpcId);
    if (!_ntwkModel.isProviderEnabledInZone(vpc.getZoneId(), Provider.VPCVirtualRouter.getName())) {
        throw new ResourceUnavailableException("VPC provider is not enabled in zone " + vpc.getZoneId(), DataCenter.class, vpc.getZoneId());
    }
    final List<DomainRouterVO> routers = _vpcRouterMgr.getVpcRouters(ip.getVpcId());
    if (routers == null) {
        throw new ResourceUnavailableException("Cannot enable site-to-site VPN on the backend; virtual router doesn't exist in the vpc " + ip.getVpcId(), DataCenter.class, vpc.getZoneId());
    }
    boolean result = true;
    for (final DomainRouterVO domainRouterVO : routers) {
        result = result && _vpcRouterMgr.startSite2SiteVpn(conn, domainRouterVO);
    }
    return result;
}
Also used : Site2SiteVpnGateway(com.cloud.network.Site2SiteVpnGateway) Capability(com.cloud.network.Network.Capability) Vpc(com.cloud.network.vpc.Vpc) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Aggregations

IpAddress (com.cloud.network.IpAddress)58 ArrayList (java.util.ArrayList)26 PublicIpAddress (com.cloud.network.PublicIpAddress)20 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)16 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)16 Network (com.cloud.network.Network)14 FirewallRule (com.cloud.network.rules.FirewallRule)11 HostVO (com.cloud.host.HostVO)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 DataCenterVO (com.cloud.dc.DataCenterVO)7 CiscoVnmcControllerVO (com.cloud.network.cisco.CiscoVnmcControllerVO)7 NetworkAsa1000vMapVO (com.cloud.network.cisco.NetworkAsa1000vMapVO)7 IPAddressVO (com.cloud.network.dao.IPAddressVO)7 PrivateIpAddress (com.cloud.network.vpc.PrivateIpAddress)7 Answer (com.cloud.agent.api.Answer)6 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)6 PublicIp (com.cloud.network.addr.PublicIp)6 StaticNat (com.cloud.network.rules.StaticNat)6 Account (com.cloud.user.Account)6 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)5