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 021 022import org.codehaus.jackson.annotate.JsonProperty; 023import org.codehaus.jackson.map.ObjectMapper; 024import org.codehaus.jackson.map.annotate.JsonSerialize; 025 026import java.io.IOException; 027import java.io.StringWriter; 028import java.util.ArrayList; 029import java.util.HashMap; 030import java.util.List; 031import java.util.Map; 032 033/** 034 * Represents the specifications for an application master 035 * container. Used in job submission to the Resource Manager 036 * via the YARN REST API. 037 * For detailed information, please refer to 038 * https://hadoop.apache.org/docs/r2.6.0/hadoop-yarn/hadoop-yarn-site/ResourceManagerRest.html 039 */ 040public final class AmContainerSpec { 041 042 public static final String ACLS_VIEW_APP = "VIEW_APP"; 043 public static final String ACLS_MODIFY_APP = "MODIFY_APP"; 044 045 private static final String AM_CONTAINER_SPEC = "AmContainerSpec"; 046 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); 047 048 private Commands commands = new Commands(); 049 private Map<String, List<StringEntry>> environment = new HashMap<>(); 050 private Map<String, List<LocalResourcesEntry>> localResources = new HashMap<>(); 051 private Map<String, List<StringEntry>> applicationAcls = new HashMap<>(); 052 private Map<String, List<StringEntry>> serviceData = new HashMap<>(); 053 private Credentials credentials; 054 055 public AmContainerSpec(){ 056 this.localResources.put(Constants.ENTRY, new ArrayList<LocalResourcesEntry>()); 057 this.environment.put(Constants.ENTRY, new ArrayList<StringEntry>()); 058 this.applicationAcls.put(Constants.ENTRY, new ArrayList<StringEntry>()); 059 this.serviceData.put(Constants.ENTRY, new ArrayList<StringEntry>()); 060 } 061 062 public AmContainerSpec addEnvironment(final String key, final String value) { 063 if (!this.environment.containsKey(Constants.ENTRY)) { 064 this.environment.put(Constants.ENTRY, new ArrayList<StringEntry>()); 065 } 066 this.environment.get(Constants.ENTRY).add(new StringEntry(key, value)); 067 return this; 068 } 069 070 public AmContainerSpec addLocalResource(final String key, final LocalResource localResource) { 071 if (!this.localResources.containsKey(Constants.ENTRY)) { 072 this.localResources.put(Constants.ENTRY, new ArrayList<LocalResourcesEntry>()); 073 } 074 this.localResources.get(Constants.ENTRY).add(new LocalResourcesEntry(key, localResource)); 075 return this; 076 } 077 078 public AmContainerSpec addApplicationAcl(final String key, final String value) { 079 if (!this.applicationAcls.containsKey(Constants.ENTRY)) { 080 this.applicationAcls.put(Constants.ENTRY, new ArrayList<StringEntry>()); 081 } 082 this.applicationAcls.get(Constants.ENTRY).add(new StringEntry(key, value)); 083 return this; 084 } 085 086 public AmContainerSpec setCommand(final String command) { 087 this.commands.setCommand(command); 088 return this; 089 } 090 091 public AmContainerSpec addServiceData(final String key, final String value) { 092 if (!this.serviceData.containsKey(Constants.ENTRY)) { 093 this.serviceData.put(Constants.ENTRY, new ArrayList<StringEntry>()); 094 } 095 this.serviceData.get(Constants.ENTRY).add(new StringEntry(key, value)); 096 return this; 097 } 098 099 @JsonProperty(Constants.CREDENTIALS) 100 @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT) 101 public Credentials getCredentials() { 102 return this.credentials; 103 } 104 105 public AmContainerSpec setCredentials(final Credentials credentials) { 106 this.credentials = credentials; 107 return this; 108 } 109 110 @JsonProperty(Constants.SERVICE_DATA) 111 @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT) 112 public Map<String, List<StringEntry>> getServiceData() { 113 return this.serviceData; 114 } 115 116 public AmContainerSpec setServiceData(final Map<String, List<StringEntry>> serviceData) { 117 this.serviceData = serviceData; 118 return this; 119 } 120 121 @JsonProperty(Constants.APPLICATION_ACLS) 122 @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT) 123 public Map<String, List<StringEntry>> getApplicationAcls() { 124 return this.applicationAcls; 125 } 126 127 public AmContainerSpec setApplicationAcls(final Map<String, List<StringEntry>> applicationAcls) { 128 this.applicationAcls = applicationAcls; 129 return this; 130 } 131 132 @JsonProperty(Constants.ENVIRONMENT) 133 @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT) 134 public Map<String, List<StringEntry>> getEnvironment() { 135 return this.environment; 136 } 137 138 public void setEnvironment(final Map<String, List<StringEntry>> environment) { 139 this.environment = environment; 140 } 141 142 @JsonProperty(Constants.COMMANDS) 143 public Commands getCommands() { 144 return this.commands; 145 } 146 147 public AmContainerSpec setCommands(final Commands commands) { 148 this.commands = commands; 149 return this; 150 } 151 152 @JsonProperty(Constants.LOCAL_RESOURCES) 153 public Map<String, List<LocalResourcesEntry>> getLocalResources() { 154 return this.localResources; 155 } 156 157 public AmContainerSpec setLocalResources(final Map<String, List<LocalResourcesEntry>> localResources) { 158 this.localResources = localResources; 159 return this; 160 } 161 162 @Override 163 public String toString() { 164 final StringWriter writer = new StringWriter(); 165 final String objectString; 166 try { 167 OBJECT_MAPPER.writeValue(writer, this); 168 objectString = writer.toString(); 169 } catch (final IOException e) { 170 throw new RuntimeException("Exception while serializing AmContainerSpec: " + e); 171 } 172 173 return AM_CONTAINER_SPEC + objectString; 174 } 175}