package com.ctrip.zeus.service.model.handler.impl;

import com.ctrip.zeus.dal.core.*;
import com.ctrip.zeus.exceptions.ValidationException;
import com.ctrip.zeus.model.entity.Domain;
import com.ctrip.zeus.model.entity.Slb;
import com.ctrip.zeus.model.entity.VirtualServer;
import com.ctrip.zeus.service.model.handler.SlbValidator;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by zhoumy on 2015/6/30.
 */
@Component("slbModelValidator")
public class DefaultSlbValidator implements SlbValidator {
    @Resource
    private GroupSlbDao groupSlbDao;

    @Override
    public void validate(Slb slb) throws Exception {
        if (slb == null || slb.getName() == null || slb.getName().isEmpty()) {
            throw new ValidationException("Slb with null value cannot be persisted.");
        }
        if (slb.getSlbServers() == null || slb.getSlbServers().size() == 0) {
            throw new ValidationException("Slb without slb servers cannot be persisted.");
        }
    }

    @Override
    public void checkVirtualServerDependencies(VirtualServer[] virtualServers) throws Exception {
        for (VirtualServer vs : virtualServers) {
            if (groupSlbDao.findAllByVirtualServer(vs.getId(), GroupSlbEntity.READSET_FULL).size() > 0)
                throw new ValidationException("Virtual server with id " + vs.getId() + " cannot be deleted. Dependencies exist.");
        }
    }

    @Override
    public void validateVirtualServer(VirtualServer[] virtualServers) throws Exception {
        Set<String> existingHost = new HashSet<>();
        for (VirtualServer virtualServer : virtualServers) {
            for (Domain domain : virtualServer.getDomains()) {
                String key = domain.getName() + ":" + virtualServer.getPort();
                if (existingHost.contains(key))
                    throw new ValidationException("Duplicate domain and port combination is found: " + key);
                else
                    existingHost.add(key);
            }
        }
    }

    @Override
    public void removable(Long slbId) throws Exception {
        if (groupSlbDao.findAllBySlb(slbId, GroupSlbEntity.READSET_FULL).size() > 0)
            throw new ValidationException("Slb with id " + slbId + " cannot be deleted. Dependencies exist.");
    }
}

@@ -324,16 +324,115 @@
 idator;%0A
+import com.netflix.config.DynamicPropertyFactory;%0Aimport com.netflix.config.DynamicStringProperty;%0A
 import o
@@ -472,16 +472,16 @@
 ponent;%0A
-
 %0Aimport 
@@ -740,16 +740,150 @@
 pSlbDao;
+%0A    private DynamicStringProperty portWhiteList = DynamicPropertyFactory.getInstance().getStringProperty(%22port.whitelist%22, %2280,443%22);
 %0A%0A    @O
@@ -1994,24 +1994,226 @@
 omains()) %7B%0A
+                if (!getPortWhiteList().contains(virtualServer.getPort())) %7B%0A                    throw new ValidationException(%22Port %22 + virtualServer.getPort() + %22 is not allowed.%22);%0A                %7D%0A
             
@@ -2506,32 +2506,32 @@
 ;%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -2523,32 +2523,298 @@
        %7D%0A    %7D%0A%0A
+    private Set%3CString%3E getPortWhiteList() %7B%0A        Set%3CString%3E result = new HashSet%3C%3E();%0A        String whiteList = portWhiteList.getValue();%0A        for (String s : whiteList.split(%22,%22)) %7B%0A            result.add(s.trim());%0A        %7D%0A        return result;%0A    %7D%0A%0A
     @Override%0A  

add port whitelist