001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.snapshot;
021
022import org.apache.hadoop.hbase.TableName;
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
025import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
026import org.apache.hadoop.hbase.util.Bytes;
027
028/**
029 * Class to help with dealing with a snapshot description on the client side.
030 * There is a corresponding class on the server side.
031 */
032@InterfaceAudience.Private
033public class ClientSnapshotDescriptionUtils {
034  /**
035   * Check to make sure that the description of the snapshot requested is valid
036   * @param snapshot description of the snapshot
037   * @throws IllegalArgumentException if the name of the snapshot or the name of the table to
038   *           snapshot are not valid names.
039   */
040  public static void assertSnapshotRequestIsValid(SnapshotProtos.SnapshotDescription snapshot)
041      throws IllegalArgumentException {
042    // make sure the snapshot name is valid
043    TableName.isLegalTableQualifierName(Bytes.toBytes(snapshot.getName()), true);
044    if(snapshot.hasTable()) {
045      // make sure the table name is valid, this will implicitly check validity
046      TableName tableName = TableName.valueOf(snapshot.getTable());
047
048      if (tableName.isSystemTable()) {
049        throw new IllegalArgumentException("System table snapshots are not allowed");
050      }
051    }
052  }
053
054  /**
055   * Returns a single line (no \n) representation of snapshot metadata.  Use this instead of
056   * {@link org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription#toString()}.  We don't replace SnapshotDescrpition's toString
057   * because it is auto-generated by protoc.
058   * @param ssd
059   * @return Single line string with a summary of the snapshot parameters
060   */
061  public static String toString(SnapshotProtos.SnapshotDescription ssd) {
062    if (ssd == null) {
063      return null;
064    }
065    return "{ ss=" + ssd.getName() +
066           " table=" + (ssd.hasTable()?TableName.valueOf(ssd.getTable()):"") +
067           " type=" + ssd.getType() + " }";
068  }
069}