OfferResources { public OfferResources(Protos.Offer offer) { this.offer = offer; this.offerId = offer.getId(); double offerMem = 0; double offerCpu = 0; Protos.Value.Ranges portRanges = null; String hostName = offer.getHostname(); for (Protos.Resource r : offer.getResourcesList()) { if (r.hasReservation()) { continue; } if (r.getName().equals("cpus")) { offerCpu += r.getScalar().getValue(); } else if (r.getName().equals("mem")) { offerMem += r.getScalar().getValue(); } else if (r.getName().equals("ports")) { Protos.Value.Ranges tmp = r.getRanges(); if (portRanges == null) { portRanges = tmp; continue; } portRanges.getRangeList().addAll(tmp.getRangeList()); } } this.hostName = hostName; this.mem = offerMem; this.cpu = offerCpu; if ((portRanges != null) && (!portRanges.getRangeList().isEmpty())) { this.addPortRanges(portRanges); } } OfferResources(Protos.Offer offer); void addPortRanges(Protos.Value.Ranges ranges); Protos.Offer getOffer(); Protos.OfferID getOfferId(); String getHostName(); double getMem(); double getCpu(); void decCpu(double val); void decMem(double val); long getPort(); boolean hasPort(); @Override String toString();  }
@Test public void testOfferResources() throws Exception { Offer offer = buildOfferWithReservation("offer1", sampleHost, 2, 1000, 6, 1000); OfferResources offerResources = new OfferResources(offer); assertEquals(8, offerResources.getCpu(), 0.0f); assertEquals(2000, offerResources.getMem(), 0.0f); assertEquals(sampleHost, offerResources.getHostName()); offer = buildOffer("offer1", sampleHost, 2.0, 2.0); offerResources = new OfferResources(offer); assertEquals(2, offerResources.getCpu(), 0.0); assertEquals(2, offerResources.getMem(), 0.0); assertEquals(sampleHost, offerResources.getHostName()); offer = buildOfferWithPorts("offer1", sampleHost, 2.0, 2000, 3000, 3100); offerResources = new OfferResources(offer); assertEquals(2, offerResources.getCpu(), 0.0); assertEquals(2000, offerResources.getMem(), 0.0); assertEquals(true, offerResources.hasPort()); offerResources.decCpu(1); offerResources.decMem(1000); assertEquals(1, offerResources.getCpu(), 0.0); assertEquals(1000, offerResources.getMem(), 0.0); assertEquals(3000, offerResources.getPort()); assertEquals(3001, offerResources.getPort()); }