TopoSwitchUtil.java 3.09 KB
package com.ui.ctrl;

import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class TopoSwitchUtil {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    HttpRestClient httpRestClient;

    public String getLuaType(){
        String luaResponse = httpRestClient.defaultGet(HttpUriContants.VIEW_LUA_CONF, String.class, null);
        String luaType = "";
        if(StringUtils.isNotBlank(luaResponse)){
            if (luaResponse.matches(".*context.cloud_flag\\s*=\\s*2.*")) {
                luaType = "qq";
            } else if (luaResponse.matches(".*context.cloud_flag\\s*=\\s*3.*")) {
                luaType = "aws+qq";
            } else{
                luaType = "aws";
            }
        }
        return luaType;
    }

    public List<String> getNginxLocationType(List<String> az1HostIps ,List<String> az2HostIps ,
            List<String> grayHostIps,List<Map<String, Object>> qcloudList){
        List<String> ls=new ArrayList<>();
        String qqApiNginx = "";
        String qqGrayNginx = "";
        for (Map<String, Object> apiMap : qcloudList) {
            if (StringUtils.equals("apigateway", String.valueOf(apiMap.get("name")))) {
                List<String> serverList = (List<String>) apiMap.get("server");
                String tmpServerRecord=serverList.get(0);
                String tmpServerIp=tmpServerRecord.substring(0,tmpServerRecord.indexOf(":"));
                if(az1HostIps.contains(tmpServerIp)){
                    qqApiNginx = "qq";
                }else if(az2HostIps.contains(tmpServerIp)){
                    qqApiNginx = "az2";
                }
            }

            if (StringUtils.equals("grayapigateway", String.valueOf(apiMap.get("name")))) {
                List<String> serverList = (List<String>) apiMap.get("server");
                logger.error("qqgrayapigateway is === "+serverList.get(0));
                String tmpServerRecord=serverList.get(0);
                String tmpServerIp=tmpServerRecord.substring(0,tmpServerRecord.indexOf(":"));
                boolean grayFlag=false;
                for(String grayIp:grayHostIps){
                    if(tmpServerIp.startsWith(grayIp)){
                        grayFlag=true;
                        break;
                    }
                }
                if(grayFlag){
                    qqGrayNginx = "gray";
                }else{
                    if(az1HostIps.contains(tmpServerIp)){
                        qqGrayNginx = "qq";
                    }else if(az2HostIps.contains(tmpServerIp)){
                        qqGrayNginx = "az2";
                    }
                }
            }
        }
        ls.add(qqApiNginx);
        ls.add(qqGrayNginx);
        return ls;
    }

}