Search in sources :

Example 1 with AllAclRule

use of com.cloud.agent.resource.virtualnetwork.model.AllAclRule in project cloudstack by apache.

the class SetNetworkAclConfigItem method generateConfig.

@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
    final SetNetworkACLCommand command = (SetNetworkACLCommand) cmd;
    final String privateGw = cmd.getAccessDetail(NetworkElementCommand.VPC_PRIVATE_GATEWAY);
    final String[][] rules = command.generateFwRules();
    final String[] aclRules = rules[0];
    final NicTO nic = command.getNic();
    final String dev = "eth" + nic.getDeviceId();
    final String netmask = Long.toString(NetUtils.getCidrSize(nic.getNetmask()));
    final List<AclRule> ingressRules = new ArrayList<AclRule>();
    final List<AclRule> egressRules = new ArrayList<AclRule>();
    for (int i = 0; i < aclRules.length; i++) {
        AclRule aclRule;
        final String[] ruleParts = aclRules[i].split(":");
        switch(ruleParts[1].toLowerCase()) {
            case "icmp":
                aclRule = new IcmpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "tcp":
                aclRule = new TcpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "udp":
                aclRule = new UdpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "all":
                aclRule = new AllAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]));
                break;
            default:
                // So, let's catch the exception and continue in the loop.
                try {
                    aclRule = new ProtocolAclRule(ruleParts[5], false, Integer.parseInt(ruleParts[1]));
                } catch (final Exception e) {
                    s_logger.warn("Problem occured when reading the entries in the ruleParts array. Actual array size is '" + ruleParts.length + "', but trying to read from index 5.");
                    continue;
                }
        }
        if ("Ingress".equals(ruleParts[0])) {
            ingressRules.add(aclRule);
        } else {
            egressRules.add(aclRule);
        }
    }
    final NetworkACL networkACL = new NetworkACL(dev, nic.getMac(), privateGw != null, nic.getIp(), netmask, ingressRules.toArray(new AclRule[ingressRules.size()]), egressRules.toArray(new AclRule[egressRules.size()]));
    return generateConfigItems(networkACL);
}
Also used : AllAclRule(com.cloud.agent.resource.virtualnetwork.model.AllAclRule) ArrayList(java.util.ArrayList) SetNetworkACLCommand(com.cloud.agent.api.routing.SetNetworkACLCommand) IcmpAclRule(com.cloud.agent.resource.virtualnetwork.model.IcmpAclRule) NetworkACL(com.cloud.agent.resource.virtualnetwork.model.NetworkACL) ProtocolAclRule(com.cloud.agent.resource.virtualnetwork.model.ProtocolAclRule) TcpAclRule(com.cloud.agent.resource.virtualnetwork.model.TcpAclRule) UdpAclRule(com.cloud.agent.resource.virtualnetwork.model.UdpAclRule) TcpAclRule(com.cloud.agent.resource.virtualnetwork.model.TcpAclRule) AclRule(com.cloud.agent.resource.virtualnetwork.model.AclRule) UdpAclRule(com.cloud.agent.resource.virtualnetwork.model.UdpAclRule) IcmpAclRule(com.cloud.agent.resource.virtualnetwork.model.IcmpAclRule) ProtocolAclRule(com.cloud.agent.resource.virtualnetwork.model.ProtocolAclRule) AllAclRule(com.cloud.agent.resource.virtualnetwork.model.AllAclRule) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

SetNetworkACLCommand (com.cloud.agent.api.routing.SetNetworkACLCommand)1 NicTO (com.cloud.agent.api.to.NicTO)1 AclRule (com.cloud.agent.resource.virtualnetwork.model.AclRule)1 AllAclRule (com.cloud.agent.resource.virtualnetwork.model.AllAclRule)1 IcmpAclRule (com.cloud.agent.resource.virtualnetwork.model.IcmpAclRule)1 NetworkACL (com.cloud.agent.resource.virtualnetwork.model.NetworkACL)1 ProtocolAclRule (com.cloud.agent.resource.virtualnetwork.model.ProtocolAclRule)1 TcpAclRule (com.cloud.agent.resource.virtualnetwork.model.TcpAclRule)1 UdpAclRule (com.cloud.agent.resource.virtualnetwork.model.UdpAclRule)1 ArrayList (java.util.ArrayList)1