Search in sources :

Example 1 with FirewallServiceProvider

use of com.cloud.network.element.FirewallServiceProvider in project cloudstack by apache.

the class FirewallManagerImpl method applyRules.

@Override
public boolean applyRules(Network network, Purpose purpose, List<? extends FirewallRule> rules) throws ResourceUnavailableException {
    boolean handled = false;
    switch(purpose) {
        /* StaticNatRule would be applied by Firewall provider, since the incompatible of two object */
        case StaticNat:
        case Firewall:
            for (FirewallServiceProvider fwElement : _firewallElements) {
                Network.Provider provider = fwElement.getProvider();
                boolean isFwProvider = _networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.Firewall, provider);
                if (!isFwProvider) {
                    continue;
                }
                handled = fwElement.applyFWRules(network, rules);
                if (handled)
                    break;
            }
            break;
        case PortForwarding:
            for (PortForwardingServiceProvider element : _pfElements) {
                Network.Provider provider = element.getProvider();
                boolean isPfProvider = _networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.PortForwarding, provider);
                if (!isPfProvider) {
                    continue;
                }
                handled = element.applyPFRules(network, (List<PortForwardingRule>) rules);
                if (handled)
                    break;
            }
            break;
        /*        case NetworkACL:
            for (NetworkACLServiceProvider element: _networkAclElements) {
                Network.Provider provider = element.getProvider();
                boolean  isAclProvider = _networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.NetworkACL, provider);
                if (!isAclProvider) {
                    continue;
                }
                handled = element.applyNetworkACLs(network, rules);
                if (handled)
                    break;
            }
            break;*/
        default:
            assert (false) : "Unexpected fall through in applying rules to the network elements";
            s_logger.error("FirewallManager cannot process rules of type " + purpose);
            throw new CloudRuntimeException("FirewallManager cannot process rules of type " + purpose);
    }
    return handled;
}
Also used : PortForwardingServiceProvider(com.cloud.network.element.PortForwardingServiceProvider) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) FirewallServiceProvider(com.cloud.network.element.FirewallServiceProvider) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with FirewallServiceProvider

use of com.cloud.network.element.FirewallServiceProvider in project cloudstack by apache.

the class FirewallManagerTest method testApplyFWRules.

@Ignore("Requires database to be set up")
@Test
public void testApplyFWRules() {
    List<FirewallRuleVO> ruleList = new ArrayList<FirewallRuleVO>();
    FirewallRuleVO rule = new FirewallRuleVO("rule1", 1, 80, "TCP", 1, 2, 1, FirewallRule.Purpose.Firewall, null, null, null, null);
    ruleList.add(rule);
    FirewallManagerImpl firewallMgr = (FirewallManagerImpl) _firewallMgr;
    VirtualRouterElement virtualRouter = mock(VirtualRouterElement.class);
    VpcVirtualRouterElement vpcVirtualRouter = mock(VpcVirtualRouterElement.class);
    List<FirewallServiceProvider> fwElements = new ArrayList<FirewallServiceProvider>();
    fwElements.add(ComponentContext.inject(VirtualRouterElement.class));
    fwElements.add(ComponentContext.inject(VpcVirtualRouterElement.class));
    firewallMgr._firewallElements = fwElements;
    try {
        when(virtualRouter.applyFWRules(any(Network.class), any(List.class))).thenReturn(false);
        when(vpcVirtualRouter.applyFWRules(any(Network.class), any(List.class))).thenReturn(true);
        //Network network, Purpose purpose, List<? extends FirewallRule> rules
        firewallMgr.applyRules(mock(Network.class), Purpose.Firewall, ruleList);
        verify(vpcVirtualRouter).applyFWRules(any(Network.class), any(List.class));
        verify(virtualRouter).applyFWRules(any(Network.class), any(List.class));
    } catch (ResourceUnavailableException e) {
        Assert.fail("Unreachable code");
    }
}
Also used : VpcVirtualRouterElement(com.cloud.network.element.VpcVirtualRouterElement) VirtualRouterElement(com.cloud.network.element.VirtualRouterElement) Network(com.cloud.network.Network) ArrayList(java.util.ArrayList) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) FirewallServiceProvider(com.cloud.network.element.FirewallServiceProvider) ArrayList(java.util.ArrayList) List(java.util.List) VpcVirtualRouterElement(com.cloud.network.element.VpcVirtualRouterElement) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Network (com.cloud.network.Network)2 FirewallServiceProvider (com.cloud.network.element.FirewallServiceProvider)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 PortForwardingServiceProvider (com.cloud.network.element.PortForwardingServiceProvider)1 VirtualRouterElement (com.cloud.network.element.VirtualRouterElement)1 VpcVirtualRouterElement (com.cloud.network.element.VpcVirtualRouterElement)1 FirewallRuleVO (com.cloud.network.rules.FirewallRuleVO)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1