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