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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.yarn.api.records;
020
021import org.apache.hadoop.classification.InterfaceAudience.Private;
022import org.apache.hadoop.classification.InterfaceAudience.Public;
023import org.apache.hadoop.classification.InterfaceStability.Stable;
024import org.apache.hadoop.classification.InterfaceStability.Unstable;
025import org.apache.hadoop.yarn.api.protocolrecords.AllocateResponse;
026import org.apache.hadoop.yarn.util.Records;
027
028/**
029 * <p>The NMToken is used for authenticating communication with
030 * <code>NodeManager</code></p>
031 * <p>It is issued by <code>ResourceMananger</code> when <code>ApplicationMaster</code>
032 * negotiates resource with <code>ResourceManager</code> and
033 * validated on <code>NodeManager</code> side.</p>
034 * @see  AllocateResponse#getNMTokens()
035 */
036@Public
037@Stable
038public abstract class NMToken {
039
040  @Private
041  @Unstable
042  public static NMToken newInstance(NodeId nodeId, Token token) {
043    NMToken nmToken = Records.newRecord(NMToken.class);
044    nmToken.setNodeId(nodeId);
045    nmToken.setToken(token);
046    return nmToken;
047  }
048
049  /**
050   * Get the {@link NodeId} of the <code>NodeManager</code> for which the NMToken
051   * is used to authenticate.
052   * @return the {@link NodeId} of the <code>NodeManager</code> for which the
053   * NMToken is used to authenticate.
054   */
055  @Public
056  @Stable
057  public abstract NodeId getNodeId();
058  
059  @Public
060  @Stable
061  public abstract void setNodeId(NodeId nodeId);
062
063  /**
064   * Get the {@link Token} used for authenticating with <code>NodeManager</code>
065   * @return the {@link Token} used for authenticating with <code>NodeManager</code>
066   */
067  @Public
068  @Stable
069  public abstract Token getToken();
070  
071  @Public
072  @Stable
073  public abstract void setToken(Token token);
074
075
076  @Override
077  public int hashCode() {
078    final int prime = 31;
079    int result = 1;
080    result =
081        prime * result + ((getNodeId() == null) ? 0 : getNodeId().hashCode());
082    result =
083        prime * result + ((getToken() == null) ? 0 : getToken().hashCode());
084    return result;
085  }
086
087  @Override
088  public boolean equals(Object obj) {
089    if (this == obj)
090      return true;
091    if (obj == null)
092      return false;
093    if (getClass() != obj.getClass())
094      return false;
095    NMToken other = (NMToken) obj;
096    if (getNodeId() == null) {
097      if (other.getNodeId() != null)
098        return false;
099    } else if (!getNodeId().equals(other.getNodeId()))
100      return false;
101    if (getToken() == null) {
102      if (other.getToken() != null)
103        return false;
104    } else if (!getToken().equals(other.getToken()))
105      return false;
106    return true;
107  }
108}