001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.runtime.hdinsight.client.yarnrest; 020 021import org.codehaus.jackson.annotate.JsonProperty; 022import org.codehaus.jackson.map.ObjectMapper; 023 024import java.io.IOException; 025import java.io.StringWriter; 026 027/** 028 * Represents the resoure field in the YARN REST API. 029 * For detailed information, please refer to 030 * https://hadoop.apache.org/docs/r2.6.0/hadoop-yarn/hadoop-yarn-site/ResourceManagerRest.html 031 */ 032public final class Resource { 033 034 private static final String RESOURCE = "resource"; 035 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); 036 private int memory; 037 private int vCores; 038 039 @JsonProperty(Constants.MEMORY) 040 public int getMemory() { 041 return this.memory; 042 } 043 044 public Resource setMemory(final int memory) { 045 this.memory = memory; 046 return this; 047 } 048 049 @JsonProperty(Constants.VCORES) 050 public int getvCores() { 051 return this.vCores; 052 } 053 054 public Resource setvCores(final int vCores) { 055 this.vCores = vCores; 056 return this; 057 } 058 059 @Override 060 public String toString() { 061 final StringWriter writer = new StringWriter(); 062 final String objectString; 063 try { 064 OBJECT_MAPPER.writeValue(writer, this); 065 objectString = writer.toString(); 066 } catch (final IOException e) { 067 throw new RuntimeException("Exception while serializing Resource: " + e); 068 } 069 070 return RESOURCE + objectString; 071 } 072}