package test.rewolucja.reservation; import eduni.simjava.Sim_event; import eduni.simjava.Sim_predicate; import eduni.simjava.Sim_type_p; import gridsim.GridSim; import gridsim.GridSimTags; import gridsim.IO_data; import gridsim.filter.FilterResult; import gridsim.gssim.GssimTags; import gridsim.gssim.Transaction_IO_data; import gridsim.gssim.filter.TransactionResult; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.joda.time.DateTime; import schedframe.exceptions.NotAuthorizedException; import schedframe.exceptions.PermanentException; import schedframe.exceptions.ReservationException; import schedframe.resources.ResourceProvider; import schedframe.scheduling.AbstractResourceRequirements; import schedframe.scheduling.AbstractTimeRequirements; import schedframe.scheduling.Offer; import schedframe.scheduling.ResourceUsage; import schedframe.scheduling.SecurityContextInterface; import schedframe.scheduling.TimeResourceAllocation; import schedframe.scheduling.plugin.grid.ModuleType; import test.rewolucja.scheduling.implementation.GridBroker; public class GridReservationManagerNew implements ReservationManagerNew { private Log log = LogFactory.getLog(GridReservationManagerNew.class); protected GridBroker broker; int transactionId = 0; public GridReservationManagerNew(GridBroker gridBroker) { this.broker = gridBroker; } public void cancelReservation(ReservationNew reservation, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { throw new ReservationException("Not implemented."); } public int checkStatus(ReservationNew reservation, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { throw new ReservationException("Not implemented."); } public ReservationNew commitReservation(ReservationNew reservation, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { String resourceName = reservation.getResourceName(); if (resourceName == null) throw new ReservationException("Resource provider is not defined."); if (resourceName == null || resourceName.length() == 0) throw new ReservationException("Provider id is not defined."); int destination = GridSim.getEntityId(resourceName); if (destination == -1) throw new ReservationException("Provider " + resourceName + " does not exist."); int transactionId = this.incrementID(); broker.getLogicalResource().send(destination, GridSimTags.SCHEDULE_NOW, GridSimTags.SEND_AR_COMMIT_ONLY, new Transaction_IO_data(transactionId, reservation, 10, destination)); // waiting for a response from the GridResource with an unique tag // of transaction id FilterResult tag = new TransactionResult(transactionId, GridSimTags.RETURN_AR_COMMIT); // only look for this type of ack for same Gridlet ID Sim_event ev = new Sim_event(); broker.getLogicalResource().sim_get_next(tag, ev); // get the result back IO_data ioData = (IO_data) ev.get_data(); Object obj = ioData.getData(); ReservationNew result = null; if (obj instanceof ReservationNew) { result = (ReservationNew) obj; log.warn("GridReservationManager.commitReservation() - Add reservation status check" + result.jobId + ";"+new DateTime(result.getStartMillis())+";"+new DateTime(result.getEndMillis())); } else if (obj instanceof ReservationException) { throw new ReservationException((ReservationException) obj); } else { throw new ReservationException(obj.getClass().getName() + " is not valid Reservation type."); } return result; } public ReservationNew commitReservation(ReservationNew reservation, TimeResourceAllocation resourceUsage, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { String resourceName = reservation.getResourceName(); if (resourceName == null) throw new ReservationException("Resource provider is not defined."); if (resourceName == null || resourceName.length() == 0) throw new ReservationException("Provider id is not defined."); int destination = GridSim.getEntityId(resourceName); if (destination == -1) throw new ReservationException("Provider " + resourceName + " does not exist."); int transactionId = this.incrementID(); Object data[] = {reservation, resourceUsage }; broker.getLogicalResource().send(destination, GridSimTags.SCHEDULE_NOW, GridSimTags.SEND_AR_COMMIT_ONLY, new Transaction_IO_data(transactionId, data, 10, destination)); // waiting for a response from the GridResource with an unique tag // of transaction id FilterResult tag = new TransactionResult(transactionId, GridSimTags.RETURN_AR_COMMIT); // only look for this type of ack for same Gridlet ID Sim_event ev = new Sim_event(); broker.getLogicalResource().sim_get_next(tag, ev); ReservationNew result = null; // get the result back Object obj = ev.get_data(); if (obj instanceof ReservationNew) { result = (ReservationNew) obj; log.warn("GridReservationManager.commitReservation() - Add reservation status check"); } else if (obj instanceof ReservationException) { throw new ReservationException((ReservationException) obj); } else { throw new ReservationException(obj.getClass().getName() + " is not valid Reservation type."); } return result; } public List createReservation( AbstractTimeRequirements timeRequirements, AbstractResourceRequirements resourceRequirements, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { throw new ReservationException("Not implemented."); } public ReservationNew createReservation(ResourceProvider provider, AbstractTimeRequirements timeRequirements, AbstractResourceRequirements resourceRequirements, List hostCandidates, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { throw new ReservationException("Not implemented."); } public List createReservation(ResourceUsage resourceUsage, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { ResourceProvider provider = resourceUsage.getProvider(); if (provider == null) throw new ReservationException("Resource provider is not defined."); String providerId = provider.getProviderId(); if (providerId == null || providerId.length() == 0) throw new ReservationException("Provider id is not defined."); int destination = GridSim.getEntityId(providerId); if (destination == -1) throw new ReservationException("Provider " + providerId + " does not exist."); int transactionId = this.incrementID(); broker.getLogicalResource().send(destination, GridSimTags.SCHEDULE_NOW, GridSimTags.SEND_AR_CREATE, new Transaction_IO_data(transactionId, resourceUsage, 10, destination)); // waiting for a response from the GridResource with an unique tag // of transaction id FilterResult tag = new TransactionResult(transactionId, GridSimTags.RETURN_AR_CREATE); // only look for this type of ack for same Gridlet ID Sim_event ev = new Sim_event(); broker.getLogicalResource().sim_get_next(tag, ev); List result = null; // get the result back IO_data ioData = (IO_data) ev.get_data(); Object obj = ioData.getData(); if (obj instanceof List) { result = (List) obj; log.warn("GridReservationManager.createReservation() - Add reservation status check"); } else if (obj instanceof ReservationException) { throw new ReservationException((ReservationException) obj); } else { throw new ReservationException(obj.getClass().getName() + " is not valid Reservation type."); } return result; } public List getOffer(AbstractTimeRequirements timeRequirements, AbstractResourceRequirements resourceRequirements, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { List gridsimProviders = this.broker.getMyGridResources(); int gridsimProvidersCount = gridsimProviders.size(); for (int i = 0; i < gridsimProvidersCount; i++) { int providerID = (Integer)gridsimProviders.get(i); Object[] data = { timeRequirements, resourceRequirements }; Transaction_IO_data ioData = new Transaction_IO_data(this.incrementID(), data, 8, providerID); broker.getLogicalResource().send(providerID, GridSimTags.SCHEDULE_NOW, GssimTags.GET_OFFERS, ioData); } List result = new ArrayList(gridsimProvidersCount); Sim_event e = new Sim_event(); Sim_predicate onlyGRMSGetOffers = new Sim_type_p( GssimTags.RETURN_GET_OFFERS); // collect answers for above send for (int i = 0; i < gridsimProvidersCount; i++) { broker.getLogicalResource().sim_get_next(onlyGRMSGetOffers, e); IO_data ioData = (IO_data) e.get_data(); List response = (List) ioData.getData(); result.addAll(response); } return result; } public Offer getOffer(String resourceName, AbstractTimeRequirements timeRequirements, AbstractResourceRequirements resourceRequirements, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { Object[] data = { timeRequirements, resourceRequirements }; broker.getLogicalResource().send(resourceName, GridSimTags.SCHEDULE_NOW, GssimTags.GET_OFFERS, data); Sim_event e = new Sim_event(); Sim_predicate onlyGRMSGetOffers = new Sim_type_p( GssimTags.RETURN_GET_OFFERS); broker.getLogicalResource().sim_get_next(onlyGRMSGetOffers, e); Offer response = (Offer) e.get_data(); return response; } public void modifyReservation(ReservationNew reservation, TimeResourceAllocation resourceUsage, SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { throw new ReservationException("Not implemented."); } public boolean supportProvider(ResourceProvider provider) { return true; } public ModuleType getType() { return ModuleType.RESERVATION_MANAGER; } public void init(Properties properties) { } public void dispose() { } private int incrementID(){ this.transactionId++; return transactionId; } }