Java tutorial
/******************************************************************************* * Copyright (C) 2013, University of Patras, Greece* * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ package eu.xipi.bro4xipi.resourceadvisor; import eu.xipi.bro4xipi.brokermodel.BrokerJpaController; import gr.upatras.ece.nam.broker.Broker; import gr.upatras.ece.nam.broker.model.federationscenarios.RequestedFederationScenario; import java.util.Vector; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Creates the Resource Advisor methods for Offered Plans * @author ctranoris * */ public class ResourceAdvisor { private static final transient Log logger = LogFactory.getLog(ResourceAdvisor.class.getName()); private static final int MAX_OFFERS = 5; private int planID = -1; private Vector<OfferedPlan> offeredPlans; private Broker broker; private RequestedFederationScenario sourceFedScenario = null; private BrokerJpaController brokerJpaController; /** * Will create a new resource advisor. It will start calculating offered plans based on requested services in the RequestedFederationScenario * @param Broker b {@link Broker } * @param RequestedFederationScenario sourceFedScenario selected from XiPi portlet * @param BrokerJpaController brokerJpaController */ public ResourceAdvisor(Broker b, RequestedFederationScenario sourceFedScenario, BrokerJpaController brokerJpaController) { this.broker = b; this.sourceFedScenario = sourceFedScenario; this.brokerJpaController = brokerJpaController; offeredPlans = new Vector<OfferedPlan>(); } /** * @return the targetFedScenario or null if no plan selected */ public RequestedFederationScenario getTargetFedScenario() { if (planID >= 0) return this.getOfferedPlans().get(planID).getProposedScenario(); else return null; } /** * @return the targetFedScenario */ public RequestedFederationScenario getSourceFedScenario() { return sourceFedScenario; } /** * @return the planID */ public int getPlanID() { return planID; } /** * @param planID the planID to set */ public void setPlanID(int planID) { this.planID = planID; } public Vector<OfferedPlan> getOfferedPlans() { return offeredPlans; } /** * This calculates Offers. Max offers are MAX_OFFERS */ public void CalculateOffers() { logger.info("Start calculating offers"); for (int j = 0; j < MAX_OFFERS; j++) { OfferedPlan offeredPlan = new OfferedPlan(broker, sourceFedScenario, j, brokerJpaController);// here we deal with only one broker, XiPi if (!offeredPlanExists(offeredPlan)) offeredPlans.add(offeredPlan); } } private boolean offeredPlanExists(OfferedPlan newPlan) { logger.info("Check if similar resources already exist in other plan"); boolean allExist = false; for (int j = 0; j < offeredPlans.size(); j++) { Vector<AdvicedOffer> existingAdvOffers = offeredPlans.get(j).getAdvicedOffers(); Vector<AdvicedOffer> newAdvOffers = newPlan.getAdvicedOffers(); allExist = true; for (AdvicedOffer existingAdvOffer : existingAdvOffers) { boolean advOfferExists = false; for (AdvicedOffer newAdvicedOffer : newAdvOffers) { logger.info("newAdvicedOffer.getOfferedResource().getId() =" + newAdvicedOffer.getOfferedResource().getId()); logger.info("existingAdvOffer.getOfferedResource().getId() =" + existingAdvOffer.getOfferedResource().getId()); advOfferExists = advOfferExists || newAdvicedOffer.getOfferedResource() .getId() == existingAdvOffer.getOfferedResource().getId(); } allExist = allExist && advOfferExists; } if (allExist) return true; } return false; } // public Double getTotalCostForAdvicedOffer(ResourceServiceContract contract) { // java.util.Date lastDate, earlyDate; // if ( (sourceFedScenario.getScheduledPlan()!=null)&& // (sourceFedScenario.getScheduledPlan().getValidFrom()!=null)&& // (sourceFedScenario.getScheduledPlan().getValidUntil()!=null) ){ // earlyDate = sourceFedScenario.getScheduledPlan().getValidFrom(); // lastDate = sourceFedScenario.getScheduledPlan().getValidUntil(); // long deltaHours = ( lastDate.getTime() - earlyDate.getTime() )/ (60*60*1000); // Double balanceDelta= (double) 0 ; // if (contract.getAvailability().getCost().getPerUnit().getName().equals("HOUR") ){ // balanceDelta = (deltaHours*contract.getAvailability().getCost().getAmount()) ; // }else if (contract.getAvailability().getCost().getPerUnit().getName().equals("USAGE") ){ // balanceDelta = (contract.getAvailability().getCost().getAmount()) ; // }else if (contract.getAvailability().getCost().getPerUnit().getName().equals("DAY") ){ // balanceDelta = (deltaHours*contract.getAvailability().getCost().getAmount()/24 ) ; // } // //// StringBuilder sb = new StringBuilder(); //// Formatter formatter = new Formatter(sb, Locale.US); // // return balanceDelta; // } // // return (double) 0; // } }