/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package alluxio.mesos;

import org.apache.mesos.Protos;

import alluxio.Constants;
import alluxio.Configuration;

/**
 * Mesos framework offer utils
 */
public final class OfferUtils {
  private static final Configuration sConf = new Configuration();

  private OfferUtils() {

  }

  /**
   * @param offer Resource offer from Mesos
   * @return Return true if the master port available in this offer
   */
  public static boolean hasAvailableMasterPorts(Protos.Offer offer) {
    Protos.Value.Ranges ranges = getOfferedPorts(offer);

    return ranges != null
        && hasAvailablePorts(sConf.getInt(Constants.MASTER_WEB_PORT), ranges)
        && hasAvailablePorts(sConf.getInt(Constants.MASTER_RPC_PORT), ranges);
  }

  /**
   * @param offer Resource offer from Mesos
   * @return Return true if the worker port available in this offer
   */
  public static boolean hasAvailableWorkerPorts(Protos.Offer offer) {
    Protos.Value.Ranges ranges = getOfferedPorts(offer);

    return ranges != null
        && hasAvailablePorts(sConf.getInt(Constants.WORKER_WEB_PORT), ranges)
        && hasAvailablePorts(sConf.getInt(Constants.WORKER_RPC_PORT), ranges)
        && hasAvailablePorts(sConf.getInt(Constants.WORKER_DATA_PORT), ranges);
  }

  private static boolean hasAvailablePorts(int port, Protos.Value.Ranges ranges) {
    for (Protos.Value.Range range : ranges.getRangeList()) {
      if (port >= range.getBegin() && port <= range.getEnd()) {
        return true;
      }
    }
    return false;
  }

  /**
   * @param offer Resource offer from Mesos
   * @return Ports ranges
   */
  public static Protos.Value.Ranges getOfferedPorts(Protos.Offer offer) {
    for (Protos.Resource resource : offer.getResourcesList()) {
      if (Constants.MESOS_RESOURCE_PORTS.equals(resource.getName())) {
        return resource.getRanges();
      }
    }
    return null;
  }

  /**
   * @param offer Resource offer from Mesos
   * @return offered cpus size
   */
  public static double getOfferedCpus(Protos.Offer offer) {
    for (Protos.Resource resource : offer.getResourcesList()) {
      if (Constants.MESOS_RESOURCE_CPUS.equals(resource.getName())) {
        return resource.getScalar().getValue();
      }
    }
    return 0.0d;
  }

  /**
   * @param offer Resource offer from Mesos
   * @return offered memory size
   */
  public static double getOfferedMem(Protos.Offer offer) {
    for (Protos.Resource resource : offer.getResourcesList()) {
      if (Constants.MESOS_RESOURCE_MEM.equals(resource.getName())) {
        return resource.getScalar().getValue();
      }
    }
    return 0.0d;
  }

  /**
   * @param offer Resource offer from Mesos
   * @return offered disk size
   */
  public static double getOfferedDisk(Protos.Offer offer) {
    for (Protos.Resource resource : offer.getResourcesList()) {
      if (Constants.MESOS_RESOURCE_DISK.equals(resource.getName())) {
        return resource.getScalar().getValue();
      }
    }
    return 0.0d;
  }
}

@@ -3,227 +3,35 @@
 %0A * 
-Licensed to the University of California, Berkeley under one or more contributor license%0A * agreements. See the NOTICE file distributed with this work for additional information regarding%0A * copyright ownership. The ASF
+The Alluxio Open Foundation
  lic
@@ -45,19 +45,12 @@
 his 
-file to You
+work
  und
@@ -72,17 +72,17 @@
 icense, 
-V
+v
 ersion 2
@@ -87,30 +87,30 @@
  2.0
+%0A *
  (the
-%0A * %22
+ %E2%80%9C
 License
-%22); y
+%E2%80%9D). Y
 ou m
@@ -129,12 +129,12 @@
 his 
-file
+work
  exc
@@ -171,208 +171,90 @@
 ense
-. You may obtain a%0A * copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software distributed under the License%0A *
+, which is%0A * available at www.apache.org/licenses/LICENSE-2.0%0A *%0A * This software
  is 
@@ -283,13 +283,13 @@
 IS%22 
-BASIS
+basis
 , WI
@@ -331,16 +331,19 @@
 NY KIND,
+%0A *
  either 
@@ -349,19 +349,16 @@
  express
-%0A *
  or impl
@@ -364,110 +364,148 @@
 lied
-. See the License for the specific language governing permissions and limitations under%0A * the License
+, as more fully set forth in the License.%0A *%0A * See the NOTICE file distributed with this work for information regarding copyright ownership
 .%0A *

fix header