
org.btrplace.scheduler.choco.transition.TransitionFactory Maven / Gradle / Ivy
/*
* Copyright (c) 2016 University Nice Sophia Antipolis
*
* This file is part of btrplace.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package org.btrplace.scheduler.choco.transition;
import org.btrplace.model.NodeState;
import org.btrplace.model.VMState;
import java.util.*;
/**
* A customisable factory that provide the right transition model according
* to the given element states.
*
* @author Fabien Hermenier
*/
public class TransitionFactory {
private Map> vmAMB2;
private Map nodeAMB;
/**
* Make a new factory.
*/
public TransitionFactory() {
vmAMB2 = new EnumMap<>(VMState.class);
nodeAMB = new EnumMap<>(NodeState.class);
}
/**
* Add a builder for a VM
*
* @param b the builder to add
*/
public void add(VMTransitionBuilder b) {
List l = vmAMB2.get(b.getDestinationState());
if (l == null) {
l = new ArrayList<>();
vmAMB2.put(b.getDestinationState(), l);
}
l.add(b);
}
/**
* Remove a builder for an action on a VM.
*
* @param b the builder to remove
* @return {@code true} if it has been removed
*/
public boolean remove(VMTransitionBuilder b) {
VMState dst = b.getDestinationState();
return vmAMB2.get(dst).remove(b);
}
/**
* Remove a builder for an action on a node.
*
* @param b the builder to remove
* @return {@code true} if it has been removed
*/
public boolean remove(NodeTransitionBuilder b) {
return nodeAMB.remove(b.getSourceState()) != null;
}
/**
* Add a builder for a VM
*
* @param b the builder to add
*/
public void add(NodeTransitionBuilder b) {
nodeAMB.put(b.getSourceState(), b);
}
/**
* Get the model builder for a given transition
*
* @param srcState the current VM state
* @param dstState the current VM state
* @return the list of possible transitions. {@code null} if no transition is available
*/
public VMTransitionBuilder getBuilder(VMState srcState, VMState dstState) {
List dstCompliant = vmAMB2.get(dstState);
if (dstCompliant == null) {
return null;
}
for (VMTransitionBuilder vmb : dstCompliant) {
if (vmb.getSourceStates().contains(srcState)) {
return vmb;
}
}
return null;
}
/**
* Get the model builder for a given transition
*
* @param srcState the current node state
* @return the {@link NodeTransition} associated to the state transition. {@code null} if no transition is available
*/
public NodeTransitionBuilder getBuilder(NodeState srcState) {
return nodeAMB.get(srcState);
}
/**
* a new factory that embeds the default builders.
*
* @return a viable factory
*/
public static TransitionFactory newBundle() {
TransitionFactory f = new TransitionFactory();
f.add(new BootVM.Builder());
f.add(new ShutdownVM.Builder());
f.add(new SuspendVM.Builder());
f.add(new ResumeVM.Builder());
f.add(new KillVM.Builder());
f.add(new RelocatableVM.Builder());
f.add(new ForgeVM.Builder());
f.add(new StayAwayVM.BuilderReady());
f.add(new StayAwayVM.BuilderSleeping());
f.add(new StayAwayVM.BuilderInit());
f.add(new BootableNode.Builder());
f.add(new ShutdownableNode.Builder());
return f;
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
for (Object nb : nodeAMB.values()) {
b.append("node ").append(nb).append('\n');
}
Set vmb = new HashSet<>();
for (Map.Entry> entry : vmAMB2.entrySet()) {
for (VMTransitionBuilder a : entry.getValue()) {
if (vmb.add(a)) {
b.append("vm ").append(a).append('\n');
}
}
}
return b.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy