/**
 * This file is part of CloudML [ http://cloudml.org ]
 *
 * Copyright (C) 2012 - SINTEF ICT
 * Contact: Franck Chauvel <franck.chauvel@sintef.no>
 *
 * Module: root
 *
 * CloudML 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.
 *
 * CloudML 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 CloudML. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.cloudml.core.samples;

import com.amazonaws.services.ec2.model.InstanceType;
import org.cloudml.core.*;

/**
 * Created by Iurii on 4/6/2015.
 */
public class DefaultLoadBalancer {

    // TODO: Change properties later

    public static String DEFAULT_NAME = "defaultLoadBalancerVm";

    public static LoadBalancerEntity initializeLoadBalancer(VM vm, InstanceType vmType, Resource resource, ProvidedPort proPort, String name){

        VM loadBalancerVm = createVM(vm, vmType);
        VMInstance loadBalancerVmInstance = new VMInstance("defaultLoadBalancerVmInstance" + "_" + name, loadBalancerVm);
        InternalComponent loadBalancerComponent = createDefaultLbComponent(vm, resource, proPort, name);
        InternalComponentInstance loadBalancerComponentInstance = new InternalComponentInstance("defaultLoadBalancerComponentInstance" + "_" + name, loadBalancerComponent);
        ExecuteInstance loadBalancerExecuteInstance = new ExecuteInstance(
                "defaultExecuteLbInstance" + "_" + String.valueOf(System.currentTimeMillis()),
                loadBalancerComponentInstance,
                loadBalancerVmInstance.getProvidedExecutionPlatforms().firstMatchFor(loadBalancerComponent)
        );

        return new LoadBalancerEntity(createDefaultLbComponent(vm, resource, proPort, name),
                loadBalancerComponentInstance,
                loadBalancerVm,
                loadBalancerVmInstance,
                loadBalancerExecuteInstance
        );
    }

    private static VM createVM(VM vm, InstanceType vmType){

        VM defaultVm = new VM(DEFAULT_NAME + "_" + String.valueOf(System.currentTimeMillis()), vm.getProvider());
        defaultVm.setProviderSpecificTypeName(vmType.toString());
        defaultVm.setLocation(vm.getLocation());
        defaultVm.setSshKey(vm.getSshKey());
        defaultVm.setSecurityGroup(vm.getSecurityGroup());
        defaultVm.setGroupName(vm.getGroupName());
        defaultVm.setPrivateKey(vm.getPrivateKey());
        defaultVm.setImageId(vm.getImageId());
        defaultVm.setIs64os(true);
        defaultVm.getProvidedExecutionPlatforms().add(vm.getProvidedExecutionPlatforms().toList().get(0));

        return  defaultVm;
    }

    private static InternalComponent createDefaultLbComponent(VM vm, Resource resource, ProvidedPort proPort, String name){

        RequiredExecutionPlatform platform = new RequiredExecutionPlatform("platform");

        InternalComponent defaultLoadBalancer = new InternalComponent("defaultLoadBalancerComponent" + "_" + name, platform);

        defaultLoadBalancer.getResources().add(resource);
        defaultLoadBalancer.getProvidedPorts().add(proPort);

        return defaultLoadBalancer;
    }
}
