All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.doip.cluster.util.RouterTool Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package org.bdware.doip.cluster.util;

import com.google.gson.*;
import com.mysql.cj.log.Log;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.cluster.client.DoipClusterClient;
import org.bdware.doip.endpoint.client.ClientConfig;
import org.bdware.doip.endpoint.client.DoipClientImpl;
import org.bdware.irp.exception.IrpClientException;
import org.bdware.irp.stateinfo.StateInfoBase;
import org.bdware.sc.bean.JoinInfo;
import org.bdware.sc.bean.RouteInfo;
import wrp.jdk.nashorn.api.scripting.NashornScriptEngineUtil;

import javax.script.ScriptException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class RouterTool {
    private AuditIrpClient irsClient;
    private NashornScriptEngineUtil engineUtil;

    Logger Logger = LogManager.getLogger(RouterTool.class);;

    public RouterTool(AuditIrpClient irsClient, NashornScriptEngineUtil engineUtil) {
        this.irsClient = irsClient;
        this.engineUtil = engineUtil;
    }

    public JsonObject verifyCluster(String id) throws IrpClientException {
        StateInfoBase content = irsClient.resolve(id);
        if(content == null || content.handleValues == null || content.handleValues.isJsonNull()) {
            throw new IrpClientException("BDO's info isn't in the router, id cannot be resolved");
        }

        // 如果不含有clusterInfo或者BCOID(ClusterInfo & JoinInfo)
        JsonObject BDOInfo = content.handleValues;
        if(BDOInfo.get("clusterIds") == null || BDOInfo.get("clusterIds").isJsonNull()
            || BDOInfo.get("bcoId") == null || BDOInfo.get("bcoId").isJsonNull()
            || BDOInfo.get("appendixes") == null || BDOInfo.get("appendixes").isJsonNull()) {
            throw new IrpClientException("BDO doesn't have enough cluster info");
        }

        // 如果不有ClusterInfo & JoinInfo
        String bcoId = BDOInfo.get("bcoId").getAsString();
        JsonObject appendixes = BDOInfo.get("appendixes").getAsJsonObject();
        if(appendixes.get(bcoId) == null || appendixes.get(bcoId).isJsonNull()
                || appendixes.get(bcoId).getAsJsonObject().get("accessRules") == null
                || appendixes.get(bcoId).getAsJsonObject().get("accessRules").isJsonNull()) {
            throw new IrpClientException("BDO doesn't have enough cluster info");
        }
        Logger.info(new GsonBuilder().setPrettyPrinting().create().toJson(BDOInfo));
        return BDOInfo;
    }

    public void parseClusterInfo(JsonObject BDOInfo, NashornScriptEngineUtil engineUtil) {
        JsonArray clusterIds = BDOInfo.get("clusterIds").getAsJsonArray();
        JsonObject appendixes = BDOInfo.get("appendixes").getAsJsonObject();

        List BDRepoIDs = new LinkedList<>();
        for (JsonElement clusterId : clusterIds) {
            String cluster = clusterId.getAsString();
            if (appendixes.get(cluster) != null || !appendixes.get(cluster).isJsonNull()) {
                BDRepoIDs.add(cluster);
            } else {
                Logger.error(cluster + " cannot be parsed by Router");
            }
        }

        engineUtil.doipClusterUtil.doipServers = new String[BDRepoIDs.size()];
        for (int i = 0; i < BDRepoIDs.size(); i++) {
            engineUtil.doipClusterUtil.doipServers[i] = BDRepoIDs.get(i);
        }
    }

    // accessRules中保存了所有的Func/Join和相关的所有函数,进行解析
    public void parseAccessRules(Map doipOperationToRouteInfo, Map doipOperationToJoinInfo, JsonObject BDOInfo) throws ScriptException {
        String bcoId = BDOInfo.get("bcoId").getAsString();
        JsonObject appendixes = BDOInfo.get("appendixes").getAsJsonObject();

        // 将所有的functions通过engine注册到functionBindings中
        JsonObject accessRulesJO = appendixes.get(bcoId).getAsJsonObject().get("accessRules").getAsJsonObject();
        JsonElement functions = accessRulesJO.get("functions");
        if(functions != null && !functions.isJsonNull() && functions.isJsonObject()) {
            JsonObject functionsObj = functions.getAsJsonObject();
            for (String funcName: functionsObj.keySet()) {
                String funcScript = functionsObj.get(funcName).getAsString();
                engineUtil.evalFunction(funcScript);
            }
        }

        // 解析RouteInfo
        Gson gson = new Gson();
        JsonElement routeInfos = accessRulesJO.get("routeInfo");
        if(routeInfos != null && !routeInfos.isJsonNull() && routeInfos.isJsonObject()) {
            JsonObject routeInfosMap = routeInfos.getAsJsonObject();

            for (String doipOperationName: routeInfosMap.keySet()) {
                JsonObject doipFunctionRouteInfoJson = routeInfosMap.get(doipOperationName).getAsJsonObject();
                RouteInfo doipFunctionRouteInfo = gson.fromJson(doipFunctionRouteInfoJson, RouteInfo.class);
                doipOperationToRouteInfo.put(doipOperationName, doipFunctionRouteInfo);
            }
        }

        // 解析JoinInfo
        JsonElement joinInfos = accessRulesJO.get("joinInfo");
        if(joinInfos != null && !joinInfos.isJsonNull() && joinInfos.isJsonObject()) {
            JsonObject joinInfosMap = joinInfos.getAsJsonObject();

            for (String doipOperationName: joinInfosMap.keySet()) {
                JsonObject doipFunctionJoinInfoJson = joinInfosMap.get(doipOperationName).getAsJsonObject();
                JoinInfo doipFunctionJoinInfo = gson.fromJson(doipFunctionJoinInfoJson, JoinInfo.class);
                doipOperationToJoinInfo.put(doipOperationName, doipFunctionJoinInfo);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy