package be.iminds.ilabt.jfed.highlevel.tasks;

import be.iminds.ilabt.jfed.highlevel.controller.JFedHighLevelException;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;

import be.iminds.ilabt.jfed.highlevel.model.Slice;
import be.iminds.ilabt.jfed.lowlevel.GeniUserProvider;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.ilabt.jfed.lowlevel.TestbedInfoSource;
import be.iminds.ilabt.jfed.lowlevel.api.RestApi;
import be.iminds.ilabt.jfed.lowlevel.api.SlaCollector;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;
import be.iminds.ilabt.jfed.lowlevel.connection.JFedConnectionProvider;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.util.JavaFXLogger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;

/**
 * User: twalcari
 * Date: 10/29/2015
 * Time: 16:29
 */
public class RegisterStopToSLACollectorTask extends SlaTask {
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(RegisterSliversTask.class);

    @Nonnull
    private final Slice slice;
    @Nonnull
    private final Server auth;


    public RegisterStopToSLACollectorTask(@Nonnull Slice slice, @Nonnull Server auth,
                                          JavaFXLogger logger, JFedPreferences jFedPreferences, TestbedInfoSource testbedInfoSource,
                                          JFedConnectionProvider connectionProvider, GeniUserProvider geniUserProvider) {
        super("Registering stop of resources @ " + auth.getDefaultComponentManagerUrn() + " to SLA Collector", logger, jFedPreferences, testbedInfoSource, connectionProvider, geniUserProvider);

        this.slice = slice;
        this.auth = auth;

    }

    @Override
    protected void doSlaTask(TaskExecution taskExecution, SlaCollector slaCollector) throws JFedException {
        if (slice.getUuid() == null)
            throw new JFedHighLevelException("No UUID available to present to the SLA Collector");

        RestApi.RestReply<String> result =
                slaCollector.registerStop(createConnection(), slice.getUuid(), auth.getDefaultComponentManagerAsGeniUrn());

        if (result.getHttpCallDetails().getResultHttpStatusCode() != 200)
            throw new JFedHighLevelException("Error while registering stop of slivers at " + auth.getName() + " to SLA collector");


    }

    public Server getAuthority() {
        return auth;
    }
}
