public static <M extends VMTRootObject> void setUsedForPod( @Nonnull Class<M> cls, EReference classCommRef, EReference pod2EntityRef, EReference entityCommRef) { @SuppressWarnings("unchecked") Iterable<ServiceEntity> ses = (Iterable<ServiceEntity>) rg.getInstances(cls); for (ServiceEntity se : ses) { if (se.isIsTemplate() || se.getParticipatesIn() == null || !se.getParticipatesIn().isMainMarket()) continue; float cpuUsed = 0f, memUsed = 0f, stAmount = 0f, flow1Used = 0f, flow2Used = 0f; for (VMTRootObject vmtObj : se.getRef(pod2EntityRef, AnalysisPackage.eINSTANCE.getServiceEntity())) { for (VMTRootObject commObj : vmtObj.getRef(entityCommRef, AnalysisPackage.eINSTANCE.getCommodity())) { Commodity comm = (Commodity) commObj; if (comm instanceof CPU) cpuUsed += comm.getUsed(); else if (comm instanceof Mem) memUsed += comm.getUsed(); else if (comm instanceof StorageAmount) stAmount += comm.getUsed(); else if (comm instanceof Flow) { if (comm.getKey().contains(PodUtil.FLOW_KEYS[1])) flow1Used += comm.getUsed(); else if (comm.getKey().contains(PodUtil.FLOW_KEYS[2])) flow2Used += comm.getUsed(); } } } CPUAllocation cpuAllocation = (CPUAllocation) se.getRef(classCommRef, AbstractionPackage.eINSTANCE.getCPUAllocation()).get(0); MemAllocation memAllocation = (MemAllocation) se.getRef(classCommRef, AbstractionPackage.eINSTANCE.getMemAllocation()).get(0); List<VMTRootObject> flowAllocation = se.getRef(classCommRef, AbstractionPackage.eINSTANCE.getFlowAllocation()); cpuAllocation.setUsed(cpuUsed); memAllocation.setUsed(memUsed); if (se.getRef(classCommRef, AbstractionPackage.eINSTANCE.getStorageAllocation()).size() > 0) { StorageAllocation stAllocation = (StorageAllocation) se.getRef(classCommRef, AbstractionPackage.eINSTANCE.getStorageAllocation()).get(0); stAllocation.setUsed(stAmount); } for (VMTRootObject flowAllocObj : flowAllocation) { Commodity flowComm = (Commodity) flowAllocObj; if (flowComm.getKey().contains(PodUtil.FLOW_KEYS[1])) flowComm.setUsed(flow1Used); else if (flowComm.getKey().contains(PodUtil.FLOW_KEYS[2])) flowComm.setUsed(flow2Used); } } }
/** * Create a DPod object layered over the given list of entities. * * @param seList A list of entities layered over the DPod * @param scName The name of the Storage Controller that defined this DPod * @return The DPod created */ public static DPod createDPod( DPod dPod, final Set<ServiceEntity> seList, HistoryUtil historyLayeredOver, HistoryUtil historyUnderlying) { // Need a naming method for DPod dPod.setDisplayName(dPod.getName()); // Create all the relationships between the providers and the dPod Map<String, Float> key2flowCap = new HashMap<String, Float>(); key2flowCap.put(PodUtil.FLOW_KEYS[1], 0f); key2flowCap.put(PodUtil.FLOW_KEYS[2], 0f); float cpuCap = 0f, memCap = 0f, stCap = 0f; for (ServiceEntity se : seList) { attachDPodToProvider(se, dPod, historyLayeredOver); // calculate the sum of the capacities for each type of commodity for (Commodity comm : se.getCommodities()) { if (comm.eClass() == AbstractionPackage.eINSTANCE.getFlow()) { if (comm.getKey().contains(PodUtil.FLOW_KEYS[1])) { key2flowCap.put( PodUtil.FLOW_KEYS[1], key2flowCap.get(PodUtil.FLOW_KEYS[1]) + comm.getCapacity()); logger.trace( "Flow-1 Commodity: " + comm.getDisplayName() + " has capacity " + comm.getCapacity()); logger.trace( "Flow-1 capacity (after addition): " + key2flowCap.get(PodUtil.FLOW_KEYS[1])); } else if (comm.getKey().contains(PodUtil.FLOW_KEYS[2])) { key2flowCap.put( PodUtil.FLOW_KEYS[2], key2flowCap.get(PodUtil.FLOW_KEYS[2]) + comm.getCapacity()); logger.trace("Flow-2 Commodity has capacity " + comm.getCapacity()); logger.trace( "Flow-2 capacity (after addition): " + key2flowCap.get(PodUtil.FLOW_KEYS[2])); } } else if (comm.eClass() == AbstractionPackage.eINSTANCE.getCPU()) { cpuCap += comm.getCapacity(); } else if (comm.eClass() == AbstractionPackage.eINSTANCE.getMem()) { memCap += comm.getCapacity(); } else if (comm.eClass() == AbstractionPackage.eINSTANCE.getStorageAmount()) { stCap += comm.getCapacity(); } } } // Create commodities sold by the DPod // The capacity of the commodities is equal to the sum of the capacities on all underlying // entities StorageAllocation stAlloc = (StorageAllocation) createObject( AbstractionPackage.eINSTANCE.getStorageAllocation(), "ST-" + dPod.getName()); stAlloc.setCapacity(stCap); stAlloc.setKey(CommodityImpl.dynCommKey(dPod)); stAlloc.setResizeable(false); dPod.getCommodities().add(stAlloc); CPUAllocation cpuAlloc = (CPUAllocation) createObject(AbstractionPackage.eINSTANCE.getCPUAllocation(), "CPU-" + dPod.getName()); cpuAlloc.setCapacity(cpuCap); cpuAlloc.setKey(CommodityImpl.dynCommKey(dPod)); cpuAlloc.setResizeable(false); dPod.getCommodities().add(cpuAlloc); MemAllocation memAlloc = (MemAllocation) createObject(AbstractionPackage.eINSTANCE.getMemAllocation(), "MEM-" + dPod.getName()); memAlloc.setCapacity(memCap); memAlloc.setKey(CommodityImpl.dynCommKey(dPod)); memAlloc.setResizeable(false); dPod.getCommodities().add(memAlloc); for (int level = 1; level < PodUtil.FLOW_KEYS.length; level++) { FlowAllocation flowAlloc = (FlowAllocation) createObject( AbstractionPackage.eINSTANCE.getFlowAllocation(), "FlowAllocation-" + level + "-" + dPod.getName()); logger.info( "Flow-" + level + " Allocation capacity " + key2flowCap.get(PodUtil.FLOW_KEYS[level])); if (key2flowCap.get(PodUtil.FLOW_KEYS[level]) >= 9e14) { logger.trace("Flow-" + level + " Allocation capacity is too high."); logger.trace( "Flow-" + level + " Allocation Capacity is " + key2flowCap.get(PodUtil.FLOW_KEYS[level])); } flowAlloc.setCapacity(key2flowCap.get(PodUtil.FLOW_KEYS[level])); flowAlloc.setKey(PodUtil.getFlowKey(CommodityImpl.dynCommKey(dPod), level)); flowAlloc.setResizeable(false); dPod.getCommodities().add(flowAlloc); } return dPod; }
/** * Creates all the necessary commodities and relationships between a provider and the DPod layered * over it. * * @param provider The provider (Physical Machine or Storage) * @param dPod The DPod * @param historyLayeredOver */ public static void attachDPodToProvider( final ServiceEntity provider, final DPod dPod, final HistoryUtil historyLayeredOver) { String providerName = provider.getName(); String commKey = providerName + ":" + dPod.getName(); if (provider instanceof PhysicalMachine) { // Create dPod commodities bought and commodities sold from the provider to the DPod CPUAllocation cpuAllocBought = (CPUAllocation) dPod.buyDynamicCommodity(AbstractionPackage.eINSTANCE.getCPUAllocation(), commKey, 0); cpuAllocBought.setName( commKey + ":" + AbstractionPackage.eINSTANCE.getCPUAllocation().getName() + "Bought"); cpuAllocBought.setResizeable(false); dPod.getCommoditiesBought().add(cpuAllocBought); Commodity cpuAlloc = provider.sellDynamicCommodity( AbstractionPackage.eINSTANCE.getCPUAllocation(), commKey, 0, null); provider.getCommodities().add(cpuAlloc); cpuAllocBought.getConsumes().clear(); cpuAllocBought.getConsumes().add(cpuAlloc); MemAllocation memAllocBought = (MemAllocation) dPod.buyDynamicCommodity(AbstractionPackage.eINSTANCE.getMemAllocation(), commKey, 0); memAllocBought.setName( commKey + ":" + AbstractionPackage.eINSTANCE.getMemAllocation().getName() + "Bought"); memAllocBought.setResizeable(false); dPod.getCommoditiesBought().add(memAllocBought); Commodity memAlloc = provider.sellDynamicCommodity( AbstractionPackage.eINSTANCE.getMemAllocation(), commKey, 0, null); provider.getCommodities().add(memAlloc); memAllocBought.getConsumes().clear(); memAllocBought.getConsumes().add(memAlloc); Map<String, Commodity> key2FlowAlloc = new HashMap<>(); for (int level = 1; level < PodUtil.FLOW_KEYS.length; level++) { String commFlowKey = PodUtil.getFlowKey(commKey, level); FlowAllocation flowAllocBought = (FlowAllocation) dPod.buyDynamicCommodity( AbstractionPackage.eINSTANCE.getFlowAllocation(), commFlowKey, 0); flowAllocBought.setName( commFlowKey + ":" + AbstractionPackage.eINSTANCE.getFlowAllocation().getName() + "Bought"); flowAllocBought.setResizeable(false); dPod.getCommoditiesBought().add(flowAllocBought); Commodity flowAlloc = provider.sellDynamicCommodity( AbstractionPackage.eINSTANCE.getFlowAllocation(), commFlowKey, 0, null); provider.getCommodities().add(flowAlloc); flowAllocBought.getConsumes().clear(); flowAllocBought.getConsumes().add(flowAlloc); key2FlowAlloc.put(PodUtil.FLOW_KEYS[level], flowAlloc); } // set the capacity of the provider sold allocation commodities to match the capacity // of the regular commodities for (Commodity comm : provider.getCommodities()) { if (comm.eClass() == AbstractionPackage.eINSTANCE.getMem()) memAlloc.setCapacity(comm.getCapacity()); else if (comm.eClass() == AbstractionPackage.eINSTANCE.getCPU()) cpuAlloc.setCapacity(comm.getCapacity()); else if (comm.eClass() == AbstractionPackage.eINSTANCE.getFlow()) { // update the alloc comm for the same level if (key2FlowAlloc.containsKey(comm.getKey())) logger.trace( "The Dpod " + dPod.getName() + "Flow-" + comm.getKey() + " capacity is " + key2FlowAlloc.get(comm.getKey()).getCapacity()); logger.trace( "The Provider " + provider.getName() + "Flow-" + comm.getKey() + "capacity is " + comm.getCapacity()); if (key2FlowAlloc.get(comm.getKey()) != null) key2FlowAlloc.get(comm.getKey()).setCapacity(comm.getCapacity()); } } } else if (provider instanceof Storage) { StorageAllocation stAllocBought = (StorageAllocation) dPod.buyDynamicCommodity( AbstractionPackage.eINSTANCE.getStorageAllocation(), commKey, 0); stAllocBought.setName( commKey + ":" + AbstractionPackage.eINSTANCE.getStorageAllocation().getName() + "Bought"); stAllocBought.setResizeable(false); dPod.getCommoditiesBought().add(stAllocBought); Commodity stAlloc = provider.sellDynamicCommodity( AbstractionPackage.eINSTANCE.getStorageAllocation(), commKey, 0, null); provider.getCommodities().add(stAlloc); stAllocBought.getConsumes().clear(); stAllocBought.getConsumes().add(stAlloc); for (Commodity comm : provider.getCommodities()) { if (comm.eClass() == AbstractionPackage.eINSTANCE.getStorageAmount()) stAlloc.setCapacity(comm.getCapacity()); } } dPod.getLayeredOver().add(provider); historyLayeredOver.remove(provider); }