Search in sources :

Example 11 with PortForwardingRuleTO

use of com.cloud.agent.api.to.PortForwardingRuleTO in project cloudstack by apache.

the class JuniperSrxResource method execute.

private Answer execute(SetPortForwardingRulesCommand cmd, int numRetries) {
    PortForwardingRuleTO[] allRules = cmd.getRules();
    Map<String, ArrayList<FirewallRuleTO>> activeRules = getActiveRules(allRules);
    try {
        openConfiguration();
        Set<String> ipPairs = activeRules.keySet();
        for (String ipPair : ipPairs) {
            String[] ipPairComponents = ipPair.split("-");
            String publicIp = ipPairComponents[0];
            String privateIp = ipPairComponents[1];
            List<FirewallRuleTO> activeRulesForIpPair = activeRules.get(ipPair);
            // Get a list of all destination NAT rules for the public/private IP address pair
            List<String[]> destNatRules = getDestNatRules(RuleMatchCondition.PUBLIC_PRIVATE_IPS, publicIp, privateIp, null, null);
            Map<String, Long> publicVlanTags = getPublicVlanTagsForNatRules(destNatRules);
            // Delete all of these rules, along with the destination NAT pools and security policies they use
            removeDestinationNatRules(null, publicVlanTags, destNatRules);
            // If there are active rules for the public/private IP address pair, add them back
            for (FirewallRuleTO rule : activeRulesForIpPair) {
                Long publicVlanTag = getVlanTag(rule.getSrcVlanTag());
                PortForwardingRuleTO portForwardingRule = (PortForwardingRuleTO) rule;
                addDestinationNatRule(getProtocol(rule.getProtocol()), publicVlanTag, portForwardingRule.getSrcIp(), portForwardingRule.getDstIp(), portForwardingRule.getSrcPortRange()[0], portForwardingRule.getSrcPortRange()[1], portForwardingRule.getDstPortRange()[0], portForwardingRule.getDstPortRange()[1]);
            }
        }
        commitConfiguration();
        return new Answer(cmd);
    } catch (ExecutionException e) {
        s_logger.error(e);
        closeConfiguration();
        if (numRetries > 0 && refreshSrxConnection()) {
            int numRetriesRemaining = numRetries - 1;
            s_logger.debug("Retrying SetPortForwardingRulesCommand. Number of retries remaining: " + numRetriesRemaining);
            return execute(cmd, numRetriesRemaining);
        } else {
            return new Answer(cmd, e);
        }
    }
}
Also used : PortForwardingRuleTO(com.cloud.agent.api.to.PortForwardingRuleTO) ArrayList(java.util.ArrayList) FirewallRuleTO(com.cloud.agent.api.to.FirewallRuleTO) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) ExecutionException(com.cloud.utils.exception.ExecutionException)

Example 12 with PortForwardingRuleTO

use of com.cloud.agent.api.to.PortForwardingRuleTO in project cloudstack by apache.

the class NiciraNvpElement method applyPFRules.

/**
     * From interface PortForwardingServiceProvider
     */
@Override
public boolean applyPFRules(Network network, List<PortForwardingRule> rules) throws ResourceUnavailableException {
    if (!canHandle(network, Service.PortForwarding)) {
        return false;
    }
    List<NiciraNvpDeviceVO> devices = niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId());
    if (devices.isEmpty()) {
        s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId());
        return false;
    }
    NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
    HostVO niciraNvpHost = hostDao.findById(niciraNvpDevice.getHostId());
    NiciraNvpRouterMappingVO routermapping = niciraNvpRouterMappingDao.findByNetworkId(network.getId());
    if (routermapping == null) {
        s_logger.error("No logical router uuid found for network " + network.getDisplayText());
        return false;
    }
    List<PortForwardingRuleTO> portForwardingRules = new ArrayList<PortForwardingRuleTO>();
    for (PortForwardingRule rule : rules) {
        IpAddress sourceIp = networkModel.getIp(rule.getSourceIpAddressId());
        Vlan vlan = vlanDao.findById(sourceIp.getVlanId());
        PortForwardingRuleTO ruleTO = new PortForwardingRuleTO(rule, vlan.getVlanTag(), sourceIp.getAddress().addr());
        portForwardingRules.add(ruleTO);
    }
    ConfigurePortForwardingRulesOnLogicalRouterCommand cmd = new ConfigurePortForwardingRulesOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), portForwardingRules);
    ConfigurePortForwardingRulesOnLogicalRouterAnswer answer = (ConfigurePortForwardingRulesOnLogicalRouterAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd);
    return answer.getResult();
}
Also used : PortForwardingRuleTO(com.cloud.agent.api.to.PortForwardingRuleTO) NiciraNvpRouterMappingVO(com.cloud.network.NiciraNvpRouterMappingVO) ConfigurePortForwardingRulesOnLogicalRouterCommand(com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand) NiciraNvpDeviceVO(com.cloud.network.NiciraNvpDeviceVO) ArrayList(java.util.ArrayList) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) Vlan(com.cloud.dc.Vlan) PortForwardingRule(com.cloud.network.rules.PortForwardingRule) ConfigurePortForwardingRulesOnLogicalRouterAnswer(com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer) HostVO(com.cloud.host.HostVO)

Example 13 with PortForwardingRuleTO

use of com.cloud.agent.api.to.PortForwardingRuleTO in project cloudstack by apache.

the class PaloAltoResourceTest method addPortForwardingRule.

@Test
public void addPortForwardingRule() throws ConfigurationException, Exception {
    if (_context.containsKey("enable_console_output") && _context.get("enable_console_output").equals("true")) {
        System.out.println("\nTEST: addPortForwardingRule");
        System.out.println("---------------------------------------------------");
    }
    _context.put("has_public_interface", "true");
    _context.put("has_private_interface", "true");
    _context.put("has_src_nat_rule", "true");
    _context.put("has_isolation_fw_rule", "true");
    _context.put("has_service_tcp_80", "true");
    _resource.setMockContext(_context);
    _resource.configure("PaloAltoResource", _resourceParams);
    long vlanId = 3954;
    List<PortForwardingRuleTO> rules = new ArrayList<PortForwardingRuleTO>();
    PortForwardingRuleTO active = new PortForwardingRuleTO(9, "192.168.80.103", 80, 80, "10.3.97.158", 8080, 8080, "tcp", false, false);
    rules.add(active);
    SetPortForwardingRulesCommand cmd = new SetPortForwardingRulesCommand(rules);
    cmd.setContextParam(NetworkElementCommand.GUEST_VLAN_TAG, Long.toString(vlanId));
    cmd.setContextParam(NetworkElementCommand.GUEST_NETWORK_CIDR, "10.3.96.1/20");
    Answer answer = _resource.executeRequest(cmd);
    assertTrue(answer.getResult());
}
Also used : Answer(com.cloud.agent.api.Answer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) PortForwardingRuleTO(com.cloud.agent.api.to.PortForwardingRuleTO) SetPortForwardingRulesCommand(com.cloud.agent.api.routing.SetPortForwardingRulesCommand) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with PortForwardingRuleTO

use of com.cloud.agent.api.to.PortForwardingRuleTO in project cloudstack by apache.

the class VirtualRoutingResourceTest method generateSetPortForwardingRulesVpcCommand.

protected SetPortForwardingRulesVpcCommand generateSetPortForwardingRulesVpcCommand() {
    final List<PortForwardingRuleTO> pfRules = new ArrayList<>();
    pfRules.add(new PortForwardingRuleTO(1, "64.1.1.10", 22, 80, "10.10.1.10", 22, 80, "TCP", false, false));
    pfRules.add(new PortForwardingRuleTO(2, "64.1.1.11", 8080, 8080, "10.10.1.11", 8080, 8080, "UDP", true, false));
    final SetPortForwardingRulesVpcCommand cmd = new SetPortForwardingRulesVpcCommand(pfRules);
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, ROUTERNAME);
    assertEquals(cmd.getAnswersCount(), 2);
    return cmd;
}
Also used : PortForwardingRuleTO(com.cloud.agent.api.to.PortForwardingRuleTO) SetPortForwardingRulesVpcCommand(com.cloud.agent.api.routing.SetPortForwardingRulesVpcCommand) ArrayList(java.util.ArrayList)

Example 15 with PortForwardingRuleTO

use of com.cloud.agent.api.to.PortForwardingRuleTO in project cloudstack by apache.

the class VirtualRoutingResourceTest method generateSetPortForwardingRulesCommand.

protected SetPortForwardingRulesCommand generateSetPortForwardingRulesCommand() {
    final List<PortForwardingRuleTO> pfRules = new ArrayList<>();
    pfRules.add(new PortForwardingRuleTO(1, "64.1.1.10", 22, 80, "10.10.1.10", 22, 80, "TCP", false, false));
    pfRules.add(new PortForwardingRuleTO(2, "64.1.1.11", 8080, 8080, "10.10.1.11", 8080, 8080, "UDP", true, false));
    final SetPortForwardingRulesCommand cmd = new SetPortForwardingRulesCommand(pfRules);
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, ROUTERNAME);
    assertEquals(cmd.getAnswersCount(), 2);
    return cmd;
}
Also used : PortForwardingRuleTO(com.cloud.agent.api.to.PortForwardingRuleTO) SetPortForwardingRulesCommand(com.cloud.agent.api.routing.SetPortForwardingRulesCommand) ArrayList(java.util.ArrayList)

Aggregations

PortForwardingRuleTO (com.cloud.agent.api.to.PortForwardingRuleTO)26 ArrayList (java.util.ArrayList)22 Test (org.junit.Test)8 Answer (com.cloud.agent.api.Answer)7 ConfigurePortForwardingRulesOnLogicalRouterAnswer (com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer)7 SetPortForwardingRulesCommand (com.cloud.agent.api.routing.SetPortForwardingRulesCommand)7 ConfigurePortForwardingRulesOnLogicalRouterCommand (com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand)6 NatRule (com.cloud.network.nicira.NatRule)6 IpAssocAnswer (com.cloud.agent.api.routing.IpAssocAnswer)5 DestinationNatRule (com.cloud.network.nicira.DestinationNatRule)5 SourceNatRule (com.cloud.network.nicira.SourceNatRule)5 PortForwardingRule (com.cloud.network.rules.PortForwardingRule)5 IpAddress (com.cloud.network.IpAddress)4 PublicIpAddress (com.cloud.network.PublicIpAddress)4 HashMap (java.util.HashMap)4 ExternalNetworkResourceUsageAnswer (com.cloud.agent.api.ExternalNetworkResourceUsageAnswer)3 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)3 ReadyAnswer (com.cloud.agent.api.ReadyAnswer)3 SetPortForwardingRulesVpcCommand (com.cloud.agent.api.routing.SetPortForwardingRulesVpcCommand)3 Vlan (com.cloud.dc.Vlan)3