private boolean validateTrafficRules(List<NetworkTrafficRule> networkTrafficRules, List<String> exposedPorts) {
        StringBuilder portsNotExposed = new StringBuilder();
        boolean portsValid = true;
        for (NetworkTrafficRule networkTrafficRule : networkTrafficRules) {
            if (networkTrafficRule.getPorts() == null) {
                logger.info("Network traffic Rules are not Valid");
                addErrorMessage(ValidatorActivity.MISSING_PORTS, (String) null);
                WorkflowLogger.persist(ValidatorActivity.MISSING_PORTS, LoggerTags.ERROR);
                return false;
            }
            // Rules are invalid if ports are not exposed
            for (String port : networkTrafficRule.getPorts()) {
                port = defaultPortsBuilder.updatePortProtocol(port);
                if (CollectionUtils.isEmpty(exposedPorts) || !exposedPorts.contains(port)) {
                    portsNotExposed.append(port + " ");
                    portsValid = false;
                }
            }
        }
        if (!portsValid) {
            logger.info("Cannot apply traffic rules to ports that are not exposed");
            addErrorMessage(ValidatorActivity.PORT_NOT_EXPOSED, portsNotExposed.toString());
            WorkflowLogger.persist(ValidatorActivity.PORT_NOT_EXPOSED, LoggerTags.ERROR, portsNotExposed.toString());
            return false;
        }
        return true;
    }
This class covers different cases
for validity of network traffic rules
before they can be applied.
workflow-controller/controller-service/src/main/java/io/hyscale/controller/validator/impl/NetworkPoliciesValidator.java