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.HashMap;
029import java.util.List;
030import java.util.Map;
031
032/**
033 * Represents credentials for an application in the YARN REST API.
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 class Credentials {
038
039  private static final String CREDENTIALS = "credentials";
040  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
041
042  private Map<String, List<StringEntry>> tokens = new HashMap<>();
043  private Map<String, List<StringEntry>> secrets = new HashMap<>();
044
045  public Credentials() {
046    this.tokens.put(Constants.ENTRY, new ArrayList<StringEntry>());
047    this.secrets.put(Constants.ENTRY, new ArrayList<StringEntry>());
048  }
049
050  public Credentials addSecret(final String key, final String value) {
051    if (!this.secrets.containsKey(Constants.ENTRY)) {
052      this.secrets.put(Constants.ENTRY, new ArrayList<StringEntry>());
053    }
054    this.secrets.get(Constants.ENTRY).add(new StringEntry(key, value));
055    return this;
056  }
057
058  public Credentials addToken(final String key, final String value) {
059    if (!this.tokens.containsKey(Constants.ENTRY)) {
060      this.tokens.put(Constants.ENTRY, new ArrayList<StringEntry>());
061    }
062    this.tokens.get(Constants.ENTRY).add(new StringEntry(key, value));
063    return this;
064  }
065
066  @JsonProperty(Constants.SECRETS)
067    @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
068    public Map<String, List<StringEntry>> getSecrets() {
069    return this.secrets;
070  }
071
072  public Credentials setSecrets(final Map<String, List<StringEntry>> secrets) {
073    this.secrets = secrets;
074    return this;
075  }
076
077  @JsonProperty(Constants.TOKENS)
078    @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
079    public Map<String, List<StringEntry>> getTokens() {
080    return this.tokens;
081  }
082
083  public Credentials setTokens(final Map<String, List<StringEntry>> tokens) {
084    this.tokens = tokens;
085    return this;
086  }
087
088  @Override
089    public String toString() {
090    final StringWriter writer = new StringWriter();
091    final String objectString;
092    try {
093      OBJECT_MAPPER.writeValue(writer, this);
094      objectString = writer.toString();
095    } catch (final IOException e) {
096      throw new RuntimeException("Exception while serializing Credentials: " + e);
097    }
098
099    return CREDENTIALS + objectString;
100  }
101}