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 */
018package org.apache.hadoop.hbase.snapshot;
019
020import org.apache.hadoop.hbase.TableName;
021import org.apache.hadoop.hbase.util.Bytes;
022import org.apache.yetus.audience.InterfaceAudience;
023
024import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
025
026/**
027 * Class to help with dealing with a snapshot description on the client side. There is a
028 * corresponding class on the server side.
029 */
030@InterfaceAudience.Private
031public final class ClientSnapshotDescriptionUtils {
032  private ClientSnapshotDescriptionUtils() {
033  }
034
035  /**
036   * Check to make sure that the description of the snapshot requested is valid
037   * @param snapshot description of the snapshot
038   * @throws IllegalArgumentException if the name of the snapshot or the name of the table to
039   *                                  snapshot are not valid names
040   */
041  public static void assertSnapshotRequestIsValid(SnapshotProtos.SnapshotDescription snapshot)
042    throws IllegalArgumentException {
043    // make sure the snapshot name is valid
044    TableName.isLegalTableQualifierName(Bytes.toBytes(snapshot.getName()), true);
045    if (snapshot.hasTable()) {
046      // make sure the table name is valid, this will implicitly check validity
047      TableName tableName = TableName.valueOf(snapshot.getTable());
048
049      if (tableName.isSystemTable()) {
050        throw new IllegalArgumentException("System table snapshots are not allowed");
051      }
052    }
053  }
054
055  /**
056   * Returns a single line (no \n) representation of snapshot metadata. Use this instead of the
057   * {@code toString} method of
058   * {@link org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotDescription}.
059   * We don't replace
060   * {@link org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotDescription}'s
061   * {@code toString}, because it is auto-generated by protoc.
062   * @param snapshot description of the snapshot
063   * @return single line string with a summary of the snapshot parameters
064   */
065  public static String toString(SnapshotProtos.SnapshotDescription snapshot) {
066    if (snapshot == null) {
067      return null;
068    }
069
070    return new StringBuilder("{ ss=").append(snapshot.getName()).append(" table=")
071      .append(snapshot.hasTable() ? TableName.valueOf(snapshot.getTable()) : "").append(" type=")
072      .append(snapshot.getType()).append(" ttl=").append(snapshot.getTtl()).append(" }").toString();
073  }
074}