Search in sources :

Example 1 with Protocol

use of com.vmware.photon.controller.model.resources.SecurityGroupService.Protocol in project photon-model by vmware.

the class AWSSecurityGroupUtils method calculatePorts.

/**
 * Calculate the port range based on the input ipPermission from and to port values
 */
public static String calculatePorts(Rule rule, IpPermission ipPermission) {
    String lowerCaseIpProtocol = (ipPermission.getIpProtocol() != null) ? ipPermission.getIpProtocol().toLowerCase() : null;
    Protocol ruleProtocol = Protocol.fromString(lowerCaseIpProtocol);
    String protocolName = (ruleProtocol != null) ? ruleProtocol.getName().toLowerCase() : null;
    if (protocolName != null && (protocolName.equals(Protocol.ICMPv6.getName()) || protocolName.equals(Protocol.ICMPv4.getName()))) {
        if (ipPermission.getFromPort() != null && ipPermission.getFromPort() != -1) {
            if (ipPermission.getToPort() != null && ipPermission.getToPort() != -1) {
                return ipPermission.getFromPort() + "-" + ipPermission.getToPort();
            }
            // only from port is provided
            return ipPermission.getFromPort().toString();
        }
        if (ipPermission.getToPort() != null && ipPermission.getToPort() != -1) {
            return ipPermission.getToPort().toString();
        }
        return rule.ports = SecurityGroupService.ALL_PORTS;
    }
    // range is all ports
    if (isAllPorts(ipPermission)) {
        return SecurityGroupService.ALL_PORTS;
    }
    // from port is provided
    if (ipPermission.getFromPort() != null) {
        // both from and to ports are provided
        if (ipPermission.getToPort() != null) {
            // and they have different values
            if (ipPermission.getFromPort().intValue() != ipPermission.getToPort().intValue()) {
                return ipPermission.getFromPort() + "-" + ipPermission.getToPort();
            }
        }
        // only from port is provided
        return ipPermission.getFromPort().toString();
    }
    // only to port is provided
    if (ipPermission.getToPort() != null) {
        return ipPermission.getToPort().toString();
    }
    return SecurityGroupService.ALL_PORTS;
}
Also used : Protocol(com.vmware.photon.controller.model.resources.SecurityGroupService.Protocol)

Aggregations

Protocol (com.vmware.photon.controller.model.resources.SecurityGroupService.Protocol)1