Index: DCWoRMS/branches/coolemall/src/example/energy/coolemall/CB1EnergyEstimationPlugin.java
===================================================================
--- DCWoRMS/branches/coolemall/src/example/energy/coolemall/CB1EnergyEstimationPlugin.java	(revision 1299)
+++ DCWoRMS/branches/coolemall/src/example/energy/coolemall/CB1EnergyEstimationPlugin.java	(revision 1329)
@@ -1,17 +1,36 @@
 package example.energy.coolemall;
 
+import java.util.List;
+
+import schedframe.resources.CoolEmAllResourceType;
+import schedframe.resources.computing.ComputingResource;
 import schedframe.resources.computing.Node;
+import schedframe.resources.computing.Processor;
+import schedframe.resources.computing.Rack;
 import schedframe.resources.computing.coolemall.ComputeBox1;
 import schedframe.resources.computing.coolemall.NodeGroup;
 import schedframe.resources.computing.profiles.energy.EnergyEvent;
+import schedframe.resources.computing.profiles.energy.airthroughput.UserAirThroughputStateName;
+import schedframe.resources.computing.profiles.energy.power.PState;
 import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName;
+import schedframe.resources.devices.Device;
 import schedframe.resources.devices.PhysicalResource;
 import schedframe.scheduling.manager.tasks.JobRegistry;
+import simulator.DataCenterWorkloadSimulator;
 import example.energy.BaseEnergyEstimationPlugin;
+import gridsim.dcworms.DCWormsTags;
 
 public class CB1EnergyEstimationPlugin extends BaseEnergyEstimationPlugin{
 
+	double powerCapLevel = -1;
+	double powerUpgradeLevel = -1;
+	
 	public double estimatePowerConsumption(EnergyEvent event, JobRegistry jobRegistry,
 			PhysicalResource resource) {
+		
+		if(powerCapLevel == -1 || powerUpgradeLevel == -1){
+			loadThresholds((ComputingResource) resource);
+		}
+		
 		double powerConsumption = 0;
 		ComputeBox1 computeBox1 = (ComputeBox1)resource;
@@ -33,5 +52,72 @@
 			powerConsumption = (powerConsumption + CoolEmAllTestbedMeasurements.OTHER_DEVICES_POWER_CONSUMPTION)/CoolEmAllTestbedMeasurements.POWER_SUPPLY_EFFICIENCY;			
 		}
+		
+		if(powerConsumption > powerCapLevel){
+			DataCenterWorkloadSimulator.getEventManager().sendToAllSchedulers(0, DCWormsTags.POWER_LIMIT_EXCEEDED, null);
+		}
+		else if(powerConsumption < powerUpgradeLevel){
+			DataCenterWorkloadSimulator.getEventManager().sendToAllSchedulers(0, DCWormsTags.POWER_LIMIT_EXCEEDED, null);
+		}
 		return powerConsumption;
+	}
+	
+	private void loadThresholds(ComputingResource compResource){
+		
+		Rack rack = (Rack) compResource;
+		double pcLevel = 0;
+		double puLevel = 0;
+		for(Processor proc: rack.getProcessors()){
+			PState highestPState =  proc.getPowerInterface().getHighestPState();
+			PState secondHighestPState =  proc.getPowerInterface().getLowestPState();
+			for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+				PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+				if(pState.getFrequency()  < secondHighestPState.getFrequency() && pState.getFrequency() > highestPState.getFrequency()){
+					secondHighestPState = pState;
+				}
+			}
+			double maxMinPowerUsage = 0;
+			double powerUsage = 0;
+			
+			for(Double load: highestPState.getLoadPowerUsage().keySet()){
+				powerUsage = highestPState.getLoadPowerUsage().get(load);
+				if(powerUsage > maxMinPowerUsage){
+					maxMinPowerUsage= powerUsage;				
+				}
+			}
+			pcLevel = pcLevel + maxMinPowerUsage;
+			
+			maxMinPowerUsage = 0;
+			powerUsage = 0;
+			
+			for(Double load: secondHighestPState.getLoadPowerUsage().keySet()){
+				powerUsage = secondHighestPState.getLoadPowerUsage().get(load);
+				if(powerUsage > maxMinPowerUsage){
+					maxMinPowerUsage= powerUsage;				
+				}
+			}
+			puLevel = puLevel + maxMinPowerUsage;
+			
+		}
+		
+		double fpc = 0;
+		List<NodeGroup> nodeGroups = (List<NodeGroup>) rack.getDescendantsByType(CoolEmAllResourceType.NodeGroup);
+		for(NodeGroup nodeGroup: nodeGroups){
+			for(Device device: nodeGroup.getResourceCharacteristic().getDevices()){
+				try {
+					fpc = fpc + device.getAirThroughputInterface().getPowerConsumption(new UserAirThroughputStateName("2"));
+				} catch (NoSuchFieldException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+			}
+
+		}
+		pcLevel = (pcLevel + fpc) / CoolEmAllTestbedMeasurements.POWER_SUPPLY_EFFICIENCY;
+		puLevel = (puLevel + fpc) / CoolEmAllTestbedMeasurements.POWER_SUPPLY_EFFICIENCY;
+
+		puLevel = (pcLevel * pcLevel / puLevel);
+
+		powerCapLevel = pcLevel;
+		powerUpgradeLevel = puLevel;
 	}
 
