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

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

The newest version!
package org.bdware.doip.cluster.util;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.cluster.entity.BDWType;
import org.bdware.irp.client.IrpClient;
import org.bdware.irp.exception.IrpClientException;
import org.bdware.irp.stateinfo.StateInfoBase;
import org.bdware.sc.bean.ForkInfo;
import org.bdware.sc.bean.JoinInfo;
import org.bdware.sc.bean.RouteInfo;

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

public class DOResolutionUtil {
    private static final Logger LOGGER = LogManager.getLogger(DOResolutionUtil.class);
    private static final Gson gson = new Gson();

    // TODO rename bdoList in Router
    public static String[] getDDOClusterInfo(JsonObject ddoInfo) {
        JsonArray doList = ddoInfo.get("bdoList").getAsJsonArray();
        List subDOIDsList = new ArrayList<>();
        for (JsonElement doid : doList) {
            subDOIDsList.add(doid.getAsString());
        }

        return subDOIDsList.toArray(new String[0]);
    }

    public static Map getDDORouteInfo(JsonObject bcoAccessRules) {
        // 解析RouteInfo
        JsonElement routeInfos = bcoAccessRules.get("routeInfo");
        Map res = new HashMap<>();
        assert 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);
            res.put(doipOperationName, doipFunctionRouteInfo);
        }
        return res;
    }

    public static Map getDDOJoinInfo(JsonObject bcoAccessRules) {
        // 解析JoinInfo
        JsonElement joinInfos = bcoAccessRules.get("joinInfo");
        Map res = new HashMap<>();
        if (joinInfos == null || joinInfos.isJsonNull() || !joinInfos.isJsonObject())
            return res;
        JsonObject joinInfosMap = joinInfos.getAsJsonObject();
        //  new Gson().fromJson(joinInfosMap, new TypeToken>() {
        //  }.getType())
        for (String doipOperationName : joinInfosMap.keySet()) {
            JsonObject doipFunctionJoinInfoJson = joinInfosMap.get(doipOperationName).getAsJsonObject();
            JoinInfo doipFunctionJoinInfo = gson.fromJson(doipFunctionJoinInfoJson, JoinInfo.class);
            res.put(doipOperationName, doipFunctionJoinInfo);
        }

        return res;
    }

    public static Map getDDOForkInfo(JsonObject bcoAccessRules) {
        // 解析JoinInfo
        JsonElement forkInfos = bcoAccessRules.get("forkInfo");
        Map res = new HashMap<>();
        if (forkInfos == null || forkInfos.isJsonNull() || !forkInfos.isJsonObject())
            return res;
        JsonObject forkInfosMap = forkInfos.getAsJsonObject();

        for (String doipOperationName : forkInfosMap.keySet()) {
            JsonObject doipFunctionForkInfoJson = forkInfosMap.get(doipOperationName).getAsJsonObject();
            ForkInfo doipFunctionForkInfo = gson.fromJson(doipFunctionForkInfoJson, ForkInfo.class);
            res.put(doipOperationName, doipFunctionForkInfo);
        }

        return res;
    }

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

    // todo refactor appendixes logic to cater to IRSUtil demand
    public static JsonObject verifyDDOInfo(JsonObject ddoInfo) throws IrpClientException {
        // 如果不含有 ClusterInfo 或者 BCOID(ClusterInfo & JoinInfo)
        if (ddoInfo.get("bdoList") == null || ddoInfo.get("bdoList").isJsonNull()
                || ddoInfo.get("bcoId") == null || ddoInfo.get("bcoId").isJsonNull()
                || ddoInfo.get("appendixes") == null || ddoInfo.get("appendixes").isJsonNull()) {
            throw new IrpClientException("DDO doesn't have enough info, bdoList or bcoId or appendixes cannot be verified");
        }
        // 如果不含有 ClusterInfo & JoinInfo
        String bcoId = ddoInfo.get("bcoId").getAsString();
        JsonObject appendixes = ddoInfo.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("DDO doesn't have enough info , accessRules cannot be verified");
        }
        //  LOGGER.info(new GsonBuilder().setPrettyPrinting().create().toJson(ddoInfo));
        return ddoInfo;
    }

    public static void verifyBDOInfo(JsonObject doInfo) throws IrpClientException {
        if (doInfo.has("address") && doInfo.has("version")) {
            return;
        } else if (doInfo.has("repoId")) {
            JsonObject repoInfo = doInfo.getAsJsonObject("appendixes").get(doInfo.get("repoId").getAsString()).getAsJsonObject();
            doInfo.add("address", repoInfo.get("address"));
            doInfo.add("version", repoInfo.get("version"));
        } else
            throw new IrpClientException("BDO doesn't have enough info , accessRules cannot be verified");
    }

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

        JsonObject doInfo = content.handleValues;
        if (doInfo.get("bdwType") != null && !doInfo.get("bdwType").isJsonNull()) {
            String bdwType = doInfo.get("bdwType").getAsString();
            return BDWType.valueOf(bdwType);
        }

        return null;
    }

    public static BDWType getDOType(JsonObject doInfo) {
        if (doInfo.get("bdwType") == null || doInfo.get("bdwType").isJsonNull()) {
            return BDWType.DO;
        }
        String bdwType = doInfo.get("bdwType").getAsString();
        return BDWType.valueOf(bdwType);
    }

    public static void expendAppendixes(JsonObject doInfo, IrpClient irsClient) {
        JsonObject appendixes;
        if (!doInfo.has("appendixes")) {
            doInfo.add("appendixes", appendixes = new JsonObject());
        } else
            appendixes = doInfo.get("appendixes").getAsJsonObject();
        String[] toAppends = new String[]{"bcoId", "repoId"};
        for (String toAppend : toAppends)
            if (doInfo.has(toAppend) && !appendixes.has(doInfo.get(toAppend).getAsString())) {
                try {
                    StateInfoBase content = irsClient.resolve(doInfo.get(toAppend).getAsString());
                    appendixes.add(doInfo.get(toAppend).getAsString(), content.handleValues);
                } catch (IrpClientException e) {
                    throw new RuntimeException(e);
                }
            }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy