package be.iminds.ilabt.jfed.experiment.util;

import be.iminds.ilabt.jfed.experiment.Experiment;
import be.iminds.ilabt.jfed.highlevel.model.Slice;
import be.iminds.ilabt.jfed.highlevel.model.Sliver;
import be.iminds.ilabt.jfed.rspec.model.ModelRspecType;
import be.iminds.ilabt.jfed.rspec.rspec_source.ManifestRspecMerger;
import be.iminds.ilabt.jfed.rspec.rspec_source.ManifestRspecSource;
import be.iminds.ilabt.jfed.rspec.rspec_source.RequestRspecSource;

/**
 * User: twalcari
 * Date: 8/24/2016
 * Time: 16:08
 */
public class ExperimentUtils {

    private ExperimentUtils(){}

    public static ManifestRspecSource getMergedRspec(Experiment experiment){
        return mergeNewRequestRspecWithExistingManifestRspec(experiment.getNewRequestRspecSource(), experiment.getSlice());
    }

    public static ManifestRspecSource mergeNewRequestRspecWithExistingManifestRspec(RequestRspecSource newRequestRspecSource, Slice slice) {
        ManifestRspecMerger manifestRspecMerger = new ManifestRspecMerger(ModelRspecType.FX);
        if (newRequestRspecSource != null)
            manifestRspecMerger.setRequest(newRequestRspecSource);
        else
            manifestRspecMerger.setRequest(slice.getRequestRspec());
        manifestRspecMerger.setAddNodesOnlyFoundInRequest(true);

        if (slice != null) {
            for (Sliver sliver : slice.getSlivers()) {
                manifestRspecMerger.setManifest(sliver.getServer().getDefaultComponentManagerAsGeniUrn(), sliver.getManifestRspec());
            }
        }

        return manifestRspecMerger.getMergedManifest();
    }
}
