/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dubbo.rpc.cluster.support.migration;

import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.util.Optional;

import static org.apache.dubbo.common.constants.RegistryConstants.INIT;

public class MigrationRule {
    private static final String DUBBO_SERVICEDISCOVERY_MIGRATION_KEY = "dubbo.application.service-discovery.migration";
    public static final String DUBBO_SERVICEDISCOVERY_MIGRATION_GROUP = "MIGRATION";
    public static final String RULE_KEY = ApplicationModel.getName() + ".migration";

    private static DynamicConfiguration configuration = null;

    static {
        Optional<DynamicConfiguration> optional = ApplicationModel.getEnvironment().getDynamicConfiguration();
        optional.ifPresent(dynamicConfiguration -> configuration = dynamicConfiguration);
    }

    private String key;
    private MigrationStep step = MigrationStep.FORCE_INTERFACE;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public MigrationStep getStep() {
        return step;
    }

    public void setStep(MigrationStep step) {
        this.step = step;
    }

    public static MigrationRule parse(String rawRule) {
        if (null == configuration) {
            return getMigrationRule(null);
        }

        if (StringUtils.isBlank(rawRule) || INIT.equals(rawRule)) {
            String step = (String)configuration.getInternalProperty(DUBBO_SERVICEDISCOVERY_MIGRATION_KEY);
            return getMigrationRule(step);

        }

        Constructor constructor = new Constructor(MigrationRule.class);
        Yaml yaml = new Yaml(constructor);
        return yaml.load(rawRule);
    }

    public static MigrationRule queryRule() {
        if (null == configuration) {
            return getMigrationRule(null);
        }

        String rawRule = configuration.getConfig(MigrationRule.RULE_KEY, DUBBO_SERVICEDISCOVERY_MIGRATION_GROUP);
        return parse(rawRule);
    }

    private  static MigrationRule getMigrationRule(String step) {
        MigrationRule rule = new MigrationRule();
        rule.setStep(Enum.valueOf(MigrationStep.class, StringUtils.isBlank(step) ? MigrationStep.APPLICATION_FIRST.name() : step));
        return rule;
    }
}

@@ -925,16 +925,163 @@
 ration;%0A
+import org.apache.dubbo.common.logger.Logger;%0Aimport org.apache.dubbo.common.logger.LoggerFactory;%0Aimport org.apache.dubbo.common.utils.PojoUtils;%0A
 import o
@@ -1244,16 +1244,20 @@
 tructor.
+Safe
 Construc
@@ -1262,16 +1262,38 @@
 uctor;%0A%0A
+import java.util.Map;%0A
 import j
@@ -1414,16 +1414,104 @@
 nRule %7B%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(MigrationRule.class);%0A%0A
     priv
@@ -2823,50 +2823,222 @@
     
-Constructor constructor = new Constructor(
+Yaml yaml = new Yaml(new SafeConstructor());%0A        MigrationRule migrationRule = null;%0A        try %7B%0A            Map%3CString, Object%3E map = yaml.load(rawRule);%0A            migrationRule = PojoUtils.mapToPojo(map, 
 Migr
@@ -3055,24 +3055,25 @@
 class);%0A
+%0A
         
 Yaml yam
@@ -3068,76 +3068,468 @@
     
-Yaml yaml = new Yaml(constructor);%0A        return yaml.load(raw
+    if (null == migrationRule.getStep()) %7B%0A                LOGGER.warn(%22Failed to parse migrationRule, step is empty, automatically switch to APPLICATION_FIRST.%22);%0A                migrationRule = getMigrationRule(null);%0A            %7D%0A        %7D catch (Exception e) %7B%0A            LOGGER.error(%22Failed to parse migrationRule, automatically switch to APPLICATION_FIRST.%22);%0A            migrationRule = getMigrationRule(null);%0A        %7D%0A%0A        return migration
 Rule
-)
 ;%0A  

fix migrationRule bug (#8358)