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.util;
020
021import java.net.InetSocketAddress;
022import java.net.URI;
023import java.net.URISyntaxException;
024
025import org.apache.hadoop.classification.InterfaceAudience.Private;
026import org.apache.hadoop.classification.InterfaceAudience.Public;
027import org.apache.hadoop.classification.InterfaceStability;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.io.Text;
030import org.apache.hadoop.security.SecurityUtil;
031import org.apache.hadoop.security.token.Token;
032import org.apache.hadoop.security.token.TokenIdentifier;
033import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
034import org.apache.hadoop.yarn.api.records.ApplicationId;
035import org.apache.hadoop.yarn.api.records.ContainerId;
036import org.apache.hadoop.yarn.api.records.NodeId;
037import org.apache.hadoop.yarn.api.records.URL;
038import org.apache.hadoop.yarn.factories.RecordFactory;
039
040
041/**
042 * This class contains a set of utilities which help converting data structures
043 * from/to 'serializableFormat' to/from hadoop/nativejava data structures.
044 *
045 */
046@Public
047public class ConverterUtils {
048
049  public static final String APPLICATION_PREFIX = "application";
050  public static final String CONTAINER_PREFIX = "container";
051  public static final String APPLICATION_ATTEMPT_PREFIX = "appattempt";
052
053  /**
054   * return a hadoop path from a given url
055   * This method is deprecated, use {@link URL#toPath()} instead.
056   * 
057   * @param url
058   *          url to convert
059   * @return path from {@link URL}
060   * @throws URISyntaxException
061   */
062  @Public
063  @Deprecated
064  public static Path getPathFromYarnURL(URL url) throws URISyntaxException {
065    return url.toPath();
066  }
067
068  /*
069   * This method is deprecated, use {@link URL#fromPath(Path)} instead.
070   */
071  @Public
072  @Deprecated
073  public static URL getYarnUrlFromPath(Path path) {
074    return URL.fromPath(path);
075  }
076  
077  /*
078   * This method is deprecated, use {@link URL#fromURI(URI)} instead.
079   */
080  @Public
081  @Deprecated
082  public static URL getYarnUrlFromURI(URI uri) {
083    return URL.fromURI(uri);
084  }
085
086  /*
087   * This method is deprecated, use {@link ApplicationId#toString()} instead.
088   */
089  @Public
090  @Deprecated
091  public static String toString(ApplicationId appId) {
092    return appId.toString();
093  }
094
095  /*
096   * This method is deprecated, use {@link ApplicationId#fromString(String)}
097   * instead.
098   */
099  @Public
100  @Deprecated
101  public static ApplicationId toApplicationId(RecordFactory recordFactory,
102      String applicationIdStr) {
103    return ApplicationId.fromString(applicationIdStr);
104  }
105
106  /*
107   * This method is deprecated, use {@link ContainerId#toString()} instead.
108   */
109  @Public
110  @Deprecated
111  public static String toString(ContainerId cId) {
112    return cId == null ? null : cId.toString();
113  }
114
115  @Private
116  @InterfaceStability.Unstable
117  public static NodeId toNodeIdWithDefaultPort(String nodeIdStr) {
118    if (nodeIdStr.indexOf(":") < 0) {
119      return NodeId.fromString(nodeIdStr + ":0");
120    }
121    return NodeId.fromString(nodeIdStr);
122  }
123
124  /*
125   * This method is deprecated, use {@link NodeId#fromString(String)} instead.
126   */
127  @Public
128  @Deprecated
129  public static NodeId toNodeId(String nodeIdStr) {
130    return NodeId.fromString(nodeIdStr);
131  }
132
133  /*
134   * This method is deprecated, use {@link ContainerId#fromString(String)}
135   * instead.
136   */
137  @Public
138  @Deprecated
139  public static ContainerId toContainerId(String containerIdStr) {
140    return ContainerId.fromString(containerIdStr);
141  }
142  
143  /*
144   * This method is deprecated, use {@link ApplicationAttemptId#toString()}
145   * instead.
146   */
147  @Public
148  @Deprecated
149  public static ApplicationAttemptId toApplicationAttemptId(
150      String applicationAttemptIdStr) {
151    return ApplicationAttemptId.fromString(applicationAttemptIdStr);
152  }
153  
154  /*
155   * This method is deprecated, use {@link ApplicationId#fromString(String)}
156   * instead.
157   */
158  @Public
159  @Deprecated
160  public static ApplicationId toApplicationId(
161      String appIdStr) {
162    return ApplicationId.fromString(appIdStr);
163  }
164
165  /**
166   * Convert a protobuf token into a rpc token and set its service. Supposed
167   * to be used for tokens other than RMDelegationToken. For
168   * RMDelegationToken, use
169   * {@link #convertFromYarn(org.apache.hadoop.yarn.api.records.Token,
170   * org.apache.hadoop.io.Text)} instead.
171   *
172   * @param protoToken the yarn token
173   * @param serviceAddr the connect address for the service
174   * @return rpc token
175   */
176  public static <T extends TokenIdentifier> Token<T> convertFromYarn(
177      org.apache.hadoop.yarn.api.records.Token protoToken,
178      InetSocketAddress serviceAddr) {
179    Token<T> token = new Token<T>(protoToken.getIdentifier().array(),
180                                  protoToken.getPassword().array(),
181                                  new Text(protoToken.getKind()),
182                                  new Text(protoToken.getService()));
183    if (serviceAddr != null) {
184      SecurityUtil.setTokenService(token, serviceAddr);
185    }
186    return token;
187  }
188
189  /**
190   * Convert a protobuf token into a rpc token and set its service.
191   *
192   * @param protoToken the yarn token
193   * @param service the service for the token
194   */
195  public static <T extends TokenIdentifier> Token<T> convertFromYarn(
196      org.apache.hadoop.yarn.api.records.Token protoToken,
197      Text service) {
198    Token<T> token = new Token<T>(protoToken.getIdentifier().array(),
199        protoToken.getPassword().array(),
200        new Text(protoToken.getKind()),
201        new Text(protoToken.getService()));
202
203    if (service != null) {
204      token.setService(service);
205    }
206    return token;
207  }
208}