Search in sources :

Example 1 with BizOperation

use of com.alipay.sofa.ark.spi.model.BizOperation in project sofa-ark by alipay.

the class OperationTransformer method doTransformToBizOperation.

public static List<BizOperation> doTransformToBizOperation(String config, Map<String, Map<String, BizState>> currentBizState) throws IllegalStateException {
    List<BizOperation> bizOperations = new ArrayList<>();
    Map<String, Map<String, BizState>> expectedBizState = new HashMap<>();
    Map<String, Map<String, BizState>> progressBizState = cloneBizStateMap(currentBizState);
    ArrayList<String> configOperation = adjustOperationOrder(config);
    for (String operation : configOperation) {
        int idx = operation.indexOf(Constants.QUESTION_MARK_SPLIT);
        String[] configInfo = (idx == -1) ? operation.split(Constants.STRING_COLON) : operation.substring(0, idx).split(Constants.STRING_COLON);
        String bizName = configInfo[0];
        String bizVersion = configInfo[1];
        String stateStr = configInfo[2];
        String parameterStr = (idx == -1) ? Constants.EMPTY_STR : operation.substring(idx + 1);
        BizState bizState = BizState.of(stateStr);
        Map<String, String> parameters = parseParameter(parameterStr);
        if (expectedBizState.get(bizName) != null && expectedBizState.get(bizName).get(bizVersion) == bizState) {
            continue;
        } else if (expectedBizState.get(bizName) != null && expectedBizState.get(bizName).get(bizVersion) != null && expectedBizState.get(bizName).get(bizVersion) != bizState) {
            throw new IllegalStateException(String.format("Don't specify same biz with different bizState, config is %s.", config));
        } else if (expectedBizState.get(bizName) != null && expectedBizState.get(bizName).containsValue(BizState.ACTIVATED) && bizState == BizState.ACTIVATED) {
            throw new IllegalStateException(String.format("Don't allow multi biz with same bizName to be active, config is %s. ", config));
        }
        if (bizState == BizState.ACTIVATED) {
            if (progressBizState.get(bizName) != null && progressBizState.get(bizName).get(bizVersion) != null) {
                // switch operation
                if (progressBizState.get(bizName).get(bizVersion).equals(BizState.DEACTIVATED)) {
                    BizOperation bizOperation = BizOperation.createBizOperation().setBizName(bizName).setBizVersion(bizVersion).setOperationType(BizOperation.OperationType.SWITCH).setParameters(parameters);
                    bizOperations.add(bizOperation);
                    transformBizState(progressBizState, BizOperation.OperationType.SWITCH, bizName, bizVersion);
                }
            } else {
                // install operation
                BizOperation bizOperation = BizOperation.createBizOperation().setBizName(bizName).setBizVersion(bizVersion).setOperationType(BizOperation.OperationType.INSTALL).setParameters(parameters);
                bizOperations.add(bizOperation);
                if (progressBizState.get(bizName) != null && progressBizState.get(bizName).containsValue(BizState.ACTIVATED)) {
                    // add switch
                    bizOperations.add(BizOperation.createBizOperation().setOperationType(BizOperation.OperationType.SWITCH).setBizName(bizName).setBizVersion(bizVersion));
                    transformBizState(progressBizState, BizOperation.OperationType.INSTALL, bizName, bizVersion);
                    transformBizState(progressBizState, BizOperation.OperationType.SWITCH, bizName, bizVersion);
                } else {
                    transformBizState(progressBizState, BizOperation.OperationType.INSTALL, bizName, bizVersion);
                }
            }
        } else {
            if (progressBizState.get(bizName) != null && progressBizState.get(bizName).get(bizVersion) == null && progressBizState.get(bizName).containsValue(BizState.ACTIVATED)) {
                BizOperation bizOperation = BizOperation.createBizOperation().setBizName(bizName).setBizVersion(bizVersion).setOperationType(BizOperation.OperationType.INSTALL).setParameters(parameters);
                bizOperations.add(bizOperation);
                transformBizState(progressBizState, BizOperation.OperationType.INSTALL, bizName, bizVersion);
            } else if (progressBizState.get(bizName) == null || !BizState.DEACTIVATED.equals(progressBizState.get(bizName).get(bizVersion))) {
                throw new IllegalStateException(String.format("Biz(%s:%s) cant be transform to %s, config is %s.", bizName, bizVersion, bizState, config));
            }
        }
        if (expectedBizState.get(bizName) == null) {
            expectedBizState.put(bizName, new HashMap<String, BizState>());
        }
        expectedBizState.get(bizName).put(bizVersion, bizState);
    }
    for (String bizName : currentBizState.keySet()) {
        for (String bizVersion : currentBizState.get(bizName).keySet()) {
            if (expectedBizState.get(bizName) == null || !expectedBizState.get(bizName).containsKey(bizVersion)) {
                bizOperations.add(BizOperation.createBizOperation().setOperationType(BizOperation.OperationType.UNINSTALL).setBizName(bizName).setBizVersion(bizVersion));
                transformBizState(progressBizState, BizOperation.OperationType.UNINSTALL, bizName, bizVersion);
            }
        }
    }
    // double check
    if (!checkBizState(expectedBizState, progressBizState)) {
        throw new IllegalStateException(String.format("Failed to transform biz operation, config is %s.", config));
    }
    return bizOperations;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BizState(com.alipay.sofa.ark.spi.model.BizState) BizOperation(com.alipay.sofa.ark.spi.model.BizOperation) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

BizOperation (com.alipay.sofa.ark.spi.model.BizOperation)1 BizState (com.alipay.sofa.ark.spi.model.BizState)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1