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;
023import org.codehaus.jackson.map.annotate.JsonSerialize;
024
025import java.io.IOException;
026import java.io.StringWriter;
027import java.util.ArrayList;
028import java.util.List;
029
030/**
031 * Represents an Application Submission object to the YARN REST API.
032 * Contains all the information needed to submit an application to the
033 * Resource Manager.
034 * For detailed information, please refer to
035 * https://hadoop.apache.org/docs/r2.6.0/hadoop-yarn/hadoop-yarn-site/ResourceManagerRest.html
036 */
037public final class ApplicationSubmission {
038
039  public static final String DEFAULT_QUEUE = "default";
040  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
041  private static final String APPLICATION_SUBMISSION = "applicationSubmission";
042  private String queue = DEFAULT_QUEUE;
043
044  public static final int DEFAULT_PRIORITY = 3;
045  private int priority = DEFAULT_PRIORITY;
046
047  public static final int DEFAULT_MAX_APP_ATTEMPTS = 1;
048  private int maxAppAttempts = DEFAULT_MAX_APP_ATTEMPTS;
049
050  public static final String DEFAULT_APPLICATION_TYPE = "YARN";
051  private String applicationType = DEFAULT_APPLICATION_TYPE;
052
053  public static final boolean DEFAULT_KEEP_CONTAINERS_ACROSS_APPLICATION_ATTEMPTS = false;
054  private boolean keepContainers = DEFAULT_KEEP_CONTAINERS_ACROSS_APPLICATION_ATTEMPTS;
055
056  public static final boolean DEFAULT_UNMANAGED_AM = false;
057  private boolean isUnmanagedAM = DEFAULT_UNMANAGED_AM;
058
059  private String applicationId;
060  private String applicationName;
061  private AmContainerSpec amContainerSpec;
062  private Resource resource;
063  private List<String> applicationTags = new ArrayList<>();
064
065  @JsonProperty(Constants.APPLICATION_ID)
066  public String getApplicationId() {
067    return applicationId;
068  }
069
070  public ApplicationSubmission setApplicationId(final String applicationId) {
071    this.applicationId = applicationId;
072    return this;
073  }
074
075  @JsonProperty(Constants.APPLICATION_NAME)
076  public String getApplicationName() {
077    return applicationName;
078  }
079
080  public ApplicationSubmission setApplicationName(final String applicationName) {
081    this.applicationName = applicationName;
082    return this;
083  }
084
085  @JsonProperty(Constants.APPLICATION_TYPE)
086  public String getApplicationType() {
087    return applicationType;
088  }
089
090  public ApplicationSubmission setApplicationType(final String applicationType) {
091    this.applicationType = applicationType;
092    return this;
093  }
094
095  @JsonProperty(Constants.AM_CONTAINER_SPEC)
096  public AmContainerSpec getAmContainerSpec() {
097    return amContainerSpec;
098  }
099
100  public ApplicationSubmission setAmContainerSpec(final AmContainerSpec amContainerSpec) {
101    this.amContainerSpec = amContainerSpec;
102    return this;
103  }
104
105  @JsonProperty(Constants.UNMANAGED_AM)
106  public boolean isUnmanagedAM() {
107    return isUnmanagedAM;
108  }
109
110  @SuppressWarnings("checkstyle:hiddenfield")
111  public ApplicationSubmission setUnmanagedAM(final boolean isUnmanagedAM) {
112    this.isUnmanagedAM = isUnmanagedAM;
113    return this;
114  }
115
116  @JsonProperty(Constants.KEEP_CONTAINERS_ACROSS_APPLICATION_ATTEMPTS)
117  public boolean isKeepContainers() {
118    return keepContainers;
119  }
120
121  public ApplicationSubmission setKeepContainers(final boolean keepContainers) {
122    this.keepContainers = keepContainers;
123    return this;
124  }
125
126  @JsonProperty(Constants.MAX_APP_ATTEMPTS)
127  public int getMaxAppAttempts() {
128    return maxAppAttempts;
129  }
130
131  public ApplicationSubmission setMaxAppAttempts(final int maxAppAttempts) {
132    this.maxAppAttempts = maxAppAttempts;
133    return this;
134  }
135
136  @JsonProperty(Constants.PRIORITY)
137  public int getPriority() {
138    return priority;
139  }
140
141  public ApplicationSubmission setPriority(final int priority) {
142    this.priority = priority;
143    return this;
144  }
145
146  @JsonProperty(Constants.QUEUE)
147  public String getQueue() {
148    return queue;
149  }
150
151  public ApplicationSubmission setQueue(final String queue) {
152    this.queue = queue;
153    return this;
154  }
155
156  @JsonProperty(Constants.RESOURCE)
157  public Resource getResource() {
158    return resource;
159  }
160
161  public ApplicationSubmission setResource(final Resource resource) {
162    this.resource = resource;
163    return this;
164  }
165
166  public ApplicationSubmission addApplicationTag(final String tag) {
167    this.applicationTags.add(tag);
168    return this;
169  }
170
171  @JsonProperty(Constants.APPLICATION_TAGS)
172  @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)
173  public List<String> getApplicationTags() {
174    return this.applicationTags;
175  }
176
177  public ApplicationSubmission setApplicationTags(final List<String> applicationTags) {
178    this.applicationTags = applicationTags;
179    return this;
180  }
181
182  @Override
183  public String toString() {
184    final StringWriter writer = new StringWriter();
185    final String objectString;
186    try {
187      OBJECT_MAPPER.writeValue(writer, this);
188      objectString = writer.toString();
189    } catch (final IOException e) {
190      throw new RuntimeException("Exception while serializing ApplicationSubmission: " + e);
191    }
192
193    return APPLICATION_SUBMISSION + objectString;
194  }
195}