<commit_before>/*
 * Copyright 2015 <NAME>.
 *
 * Licensed 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 com.northernwall.hadrian.workItem.action;

import com.northernwall.hadrian.config.Const;
import com.northernwall.hadrian.domain.Vip;
import com.northernwall.hadrian.domain.WorkItem;
import com.northernwall.hadrian.workItem.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VipMigrateAction extends Action {

    private final static Logger LOGGER = LoggerFactory.getLogger(VipMigrateAction.class);

    @Override
    public Result process(WorkItem workItem) {
        Vip vip = dataAccess.getVip(workItem.getService().serviceId, workItem.getVip().vipId);
        if (vip == null) {
            LOGGER.info("Failed the find vip");
            return Result.error;
        }
        if (vip.getMigration() == 1 && workItem.getVip().migration == 2) {
            return migrateStep1(workItem, vip);
        } else if (vip.getMigration() == 2 && workItem.getVip().migration == 3) {
            return migrateStep2(workItem, vip);
        } else if (vip.getMigration() == 3 && workItem.getVip().migration == 2) {
            return rollbackStep2(workItem, vip);
        } else if (vip.getMigration() == 3 && workItem.getVip().migration == 4) {
            return migrateStep3(workItem, vip);
        } else {
            LOGGER.info("Failed to migrating Vip {} for {}, current state {}", workItem.getVip().dns, workItem.getService().serviceName, vip.getMigration());
            return Result.error;
        }
    }

    public Result migrateStep1(WorkItem workItem, Vip vip) {
        LOGGER.info("Migrating Vip {} 1->2 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(2);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    public Result migrateStep2(WorkItem workItem, Vip vip) {
        LOGGER.info("Migrating Vip {} 2->3 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(3);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    public Result rollbackStep2(WorkItem workItem, Vip vip) {
        LOGGER.info("Rolling back Vip {} 3->2 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(2);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    public Result migrateStep3(WorkItem workItem, Vip vip) {
        LOGGER.info("Migrating Vip {} 3->4 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(4);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    @Override
    public void success(WorkItem workItem) {
        Vip vip = dataAccess.getVip(workItem.getService().serviceId, workItem.getVip().vipId);
        if (vip == null) {
            LOGGER.warn("Could not find vip {} being migrated", workItem.getVip().vipId);
            return;
        }
        vip.setStatus(false, Const.STATUS_NO);
        dataAccess.updateVip(vip);

        messagingCoodinator.sendMessage("VIP '"
                + workItem.getVip().dns
                + "."
                + workItem.getVip().domain
                + "' migration step was successful.",
                workItem.getTeam().teamId);
    }

    @Override
    public void error(WorkItem workItem) {
        Vip vip = dataAccess.getVip(workItem.getService().serviceId, workItem.getVip().vipId);
        if (vip == null) {
            LOGGER.warn("Could not find vip {} being migrated", workItem.getVip().vipId);<commit_msg>Improvement to migration action<commit_after>/*
 * Copyright 2015 <NAME>.
 *
 * Licensed 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 com.northernwall.hadrian.workItem.action;

import com.northernwall.hadrian.config.Const;
import com.northernwall.hadrian.domain.Vip;
import com.northernwall.hadrian.domain.WorkItem;
import com.northernwall.hadrian.workItem.Result;
import com.northernwall.hadrian.workItem.dao.CallbackData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VipMigrateAction extends Action {

    private final static Logger LOGGER = LoggerFactory.getLogger(VipMigrateAction.class);

    @Override
    public Result process(WorkItem workItem) {
        Vip vip = dataAccess.getVip(workItem.getService().serviceId, workItem.getVip().vipId);
        if (vip == null) {
            LOGGER.info("Failed the find vip");
            return Result.error;
        }
        if (vip.getMigration() == 1 && workItem.getVip().migration == 2) {
            return migrateStep1(workItem, vip);
        } else if (vip.getMigration() == 2 && workItem.getVip().migration == 3) {
            return migrateStep2(workItem, vip);
        } else if (vip.getMigration() == 3 && workItem.getVip().migration == 2) {
            return rollbackStep2(workItem, vip);
        } else if (vip.getMigration() == 3 && workItem.getVip().migration == 4) {
            return migrateStep3(workItem, vip);
        } else {
            LOGGER.info("Failed to migrating Vip {} for {}, current state {}", workItem.getVip().dns, workItem.getService().serviceName, vip.getMigration());
            return Result.error;
        }
    }

    public Result migrateStep1(WorkItem workItem, Vip vip) {
        LOGGER.info("Migrating Vip {} 1->2 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(2);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    public Result migrateStep2(WorkItem workItem, Vip vip) {
        LOGGER.info("Migrating Vip {} 2->3 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(3);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    public Result rollbackStep2(WorkItem workItem, Vip vip) {
        LOGGER.info("Rolling back Vip {} 3->2 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(2);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    public Result migrateStep3(WorkItem workItem, Vip vip) {
        LOGGER.info("Migrating Vip {} 3->4 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(4);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    @Override
    public Result processCallback(WorkItem workItem, CallbackData callbackData) {
        Vip vip = dataAccess.getVip(workItem.getService().serviceId, workItem.getVip().vipId);
        if (vip == null) {
            LOGGER.info("Failed the find vip");
            return Result.error;
        }
        if (vip.getMigration() == 1 && workItem.getVip().migration == 2) {
            return migrateStep1Callback(workItem, vip);
        } else if (vip.getMigration() == 2 && workItem.getVip().migration == 3) {
            return migrateStep2Callback(workItem, vip);
        } else if (vip.getMigration() == 3 && workItem.getVip().migration == 2) {
            return rollbackStep2Callback(workItem, vip);
        } else if (vip.getMigration() == 3 && workItem.getVip().migration == 4) {
            return migrateStep3Callback(workItem, vip);
        } else {
            LOGGER.info("Failed to migrating Vip {} for {}, current state {}", workItem.getVip().dns, workItem.getService().serviceName, vip.getMigration());
            return Result.error;
        }
    }

    public Result migrateStep1Callback(WorkItem workItem, Vip vip) {
        LOGGER.info("Migrating Vip {} 1->2 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(2);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    public Result migrateStep2Callback(WorkItem workItem, Vip vip) {
        LOGGER.info("Migrating Vip {} 2->3 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(3);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    public Result rollbackStep2Callback(WorkItem workItem, Vip vip) {
        LOGGER.info("Rolling back Vip {} 3->2 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(2);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    public Result migrateStep3Callback(WorkItem workItem, Vip vip) {
        LOGGER.info("Migrating Vip {} 3->4 for {}", workItem.getVip().dns, workItem.getService().serviceName);
        vip.setMigration(4);
        dataAccess.saveVip(vip);
        return Result.success;
    }

    @Override
    public void success(WorkItem workItem) {
        Vip vip = dataAccess.getVip(workItem.getService().serviceId, workItem.getVip().vipId);
        if (vip == null) {
            LOGGER.warn("Could not find vip {} being migrated", workItem.getVip().vipId);
            return;
        }
        vip.setStatus(false, Const.STATUS_NO);
        dataAccess.updateVip(vip);

        messagingCoodinator.sendMessage("VIP '"
                + workItem.getVip().dns
                + "."
                + workItem.getVip().domain
                + "' migration step was successful.",
                workItem.getTeam().teamId);
    }

    @Override
    public void error(WorkItem workItem) {
        Vip vip = dataAccess.getVip(workItem.getService().serviceId, workItem.getVip().vipId);
        if (vip == null) {
            LOGGER.warn("Could not find vip {} being migrated", workItem.getVip().vipId);
{'fraction_non_alphanumeric': 0.12446022454658862, 'fraction_numerical': 0.009979848383072643, 'mean_word_length': 2.7953386744355426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '378473', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3210, 'n_words': 713}