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

org.cloudsimplus.traces.google.BrokerManager Maven / Gradle / Ivy

Go to download

CloudSim Plus: A modern, highly extensible and easier-to-use Java 8+ Framework for Modeling and Simulation of Cloud Computing Infrastructures and Services

There is a newer version: 8.0.0
Show newest version
/*
 * CloudSim Plus: A modern, highly-extensible and easier-to-use Framework for
 * Modeling and Simulation of Cloud Computing Infrastructures and Services.
 * http://cloudsimplus.org
 *
 *     Copyright (C) 2015-2021 Universidade da Beira Interior (UBI, Portugal) and
 *     the Instituto Federal de Educação Ciência e Tecnologia do Tocantins (IFTO, Brazil).
 *
 *     This file is part of CloudSim Plus.
 *
 *     CloudSim Plus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     CloudSim Plus 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 General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with CloudSim Plus. If not, see .
 */
package org.cloudsimplus.traces.google;

import org.cloudbus.cloudsim.brokers.DatacenterBroker;
import org.cloudbus.cloudsim.brokers.DatacenterBrokerSimple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;

/**
 * Manages creation and access to {@link DatacenterBroker}s used by
 * {@link GoogleTaskEventsTraceReader}.
 *
 * @author Manoel Campos da Silva Filho
 * @since CloudSim Plus 7.0.1
 */
public final class BrokerManager {
    /**
     * A default broker to be used by all created Cloudlets.
     * @see #setDefaultBroker(DatacenterBroker)
     * @see #brokersMap
     */
    private DatacenterBroker defaultBroker;

    /**
     * A map of brokers created according to the username from the trace file,
     * representing a customer. Each key is the username field and the value the created broker.
     * If a default {@link #defaultBroker} is set, the map is empty.
     * @see #getBrokers()
     */
    private final Map brokersMap;

    private final GoogleTaskEventsTraceReader reader;

    BrokerManager(final GoogleTaskEventsTraceReader reader){
        this.reader = reader;
        this.brokersMap = new HashMap<>();
    }

    /**
     * Gets the List of brokers created according to the username from the trace file,
     * representing a customer.
     * @return
     * @see #setDefaultBroker(DatacenterBroker)
     */
    public List getBrokers() {
        return defaultBroker == null ? new ArrayList<>(brokersMap.values()) : singletonList(defaultBroker);
    }

    /**
     * Defines a default broker to will be used for all created Cloudlets.
     * This way, the username field inside the trace file won't be used
     * to dynamically create brokers.
     * The {@link #getBrokers()} will only return an unitary list containing this broker.
     *
     * @param broker the broker for all created cloudlets, representing a single username (customer)
     */
    public void setDefaultBroker(final DatacenterBroker broker) {
        this.defaultBroker = requireNonNull(broker);
        this.brokersMap.clear();
    }


    /**
     * Gets a {@link #setDefaultBroker(DatacenterBroker) default broker (if ones was set)}
     * or the one with the specified username (creating it if not yet).
     * @param username the username of the broker
     * @return (i) an already existing broker with the given username or a new one if not created yet;
     *         (ii) the default broker if one was set.
     */
    DatacenterBroker getOrCreateBroker(final String username){
        return getBroker(() -> brokersMap.computeIfAbsent(username, this::createBroker));
    }

    private DatacenterBroker createBroker(final String username) {
        return new DatacenterBrokerSimple(reader.getSimulation(), "Broker_"+username);
    }

    /**
     * Gets an {@link DatacenterBroker} instance representing the username from the last trace line read.
     * @return the {@link DatacenterBroker} instance
     */
    DatacenterBroker getBroker(){
        final String value = TaskEventField.USERNAME.getValue(reader);
        return getBroker(value);
    }

    /**
     * Gets an {@link DatacenterBroker} instance represented by a given username.
     * If a {@link #setDefaultBroker(DatacenterBroker) default broker was set to be used for all created Cloudlets},
     * that one is returned, ignoring the username given.
     *
     * @param username the name of the user read from a trace line
     * @return the {@link DatacenterBroker} instance for the given username or the default broker (if it was set)
     */
    DatacenterBroker getBroker(final String username){
        return getBroker(() -> brokersMap.get(username));
    }

    /**
     * Gets the default broker or the one returned by the provided supplier
     * @param supplier a broker {@link Supplier} Function.
     * @return
     */
    DatacenterBroker getBroker(final Supplier supplier){
        return defaultBroker == null ? supplier.get() : defaultBroker;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy