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

org.btrplace.plan.event.ForgeVM Maven / Gradle / Ivy

/*
 * Copyright  2020 The BtrPlace Authors. All rights reserved.
 * Use of this source code is governed by a LGPL-style
 * license that can be found in the LICENSE.txt file.
 */

package org.btrplace.plan.event;

import org.btrplace.model.Mapping;
import org.btrplace.model.Model;
import org.btrplace.model.VM;
import org.btrplace.model.VMState;

import java.util.Objects;

/**
 * Prepare a VM for being deployed.
 *
 * @author Fabien Hermenier
 */
public class ForgeVM extends Action implements VMStateTransition {

  private final VM id;

    /**
     * Make a new action.
     *
     * @param vm    the VM to forge.
     * @param start the moment the action starts
     * @param end   the moment the action ends
     */
    public ForgeVM(VM vm, int start, int end) {
        super(start, end);
        this.id = vm;
    }

    /**
     * Put the VM in the ready state iff
     * it does not already belong to the mapping.
     *
     * @param m the model to modify
     * @return {@code true} iff successful
     */
    @Override
    public boolean applyAction(Model m) {
        Mapping map = m.getMapping();

        if (!map.contains(id)) {
            map.addReadyVM(id);
            return true;
        }
        return false;
    }

    /**
     * Test if the action is equals to another object.
     *
     * @param o the object to compare with
     * @return true if {@code o} is an instance of ForgeVM and if both
     * instances involve the same VM
     */
    @Override
    public boolean equals(Object o) {
        if (!super.equals(o)) {
            return false;
        }
        ForgeVM that = (ForgeVM) o;
        return this.id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(getStart(), getEnd(), id);
    }

    @Override
    public VM getVM() {
        return id;
    }

    @Override
    public String pretty() {
        return "forge(vm=" + id + ')';
    }

    @Override
    public VMState getCurrentState() {
        return VMState.INIT;
    }

    @Override
    public VMState getNextState() {
        return VMState.READY;
    }

    @Override
    public Object visit(ActionVisitor v) {
        return v.visit(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy