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.rest.model;
019
020import java.io.IOException;
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlRootElement;
028import javax.xml.bind.annotation.XmlTransient;
029import org.apache.hadoop.hbase.NamespaceDescriptor;
030import org.apache.hadoop.hbase.client.Admin;
031import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
032import org.apache.hadoop.hbase.rest.protobuf.generated.NamespacePropertiesMessage.NamespaceProperties;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * List a HBase namespace's key/value properties.
037 * <ul>
038 * <li>NamespaceProperties: outer element</li>
039 * <li>properties: sequence property elements</li>
040 * <li>entry</li>
041 * <li>key: property key</li>
042 * <li>value: property value</li>
043 * </ul>
044 */
045@XmlRootElement(name = "NamespaceProperties")
046@XmlAccessorType(XmlAccessType.FIELD)
047@InterfaceAudience.Private
048public class NamespacesInstanceModel implements Serializable, ProtobufMessageHandler {
049
050  private static final long serialVersionUID = 1L;
051
052  // JAX-RS automatically converts Map to XMLAnyElement.
053  private Map<String, String> properties = null;
054
055  @XmlTransient
056  private String namespaceName;
057
058  /**
059   * Default constructor. Do not use.
060   */
061  public NamespacesInstanceModel() {
062  }
063
064  /**
065   * Constructor to use if namespace does not exist in HBASE.
066   * @param namespaceName the namespace name.
067   */
068  public NamespacesInstanceModel(String namespaceName) throws IOException {
069    this(null, namespaceName);
070  }
071
072  /**
073   * Constructor
074   * @param admin         the administrative API
075   * @param namespaceName the namespace name.
076   */
077  public NamespacesInstanceModel(Admin admin, String namespaceName) throws IOException {
078    this.namespaceName = namespaceName;
079    if (admin == null) {
080      return;
081    }
082
083    NamespaceDescriptor nd = admin.getNamespaceDescriptor(namespaceName);
084
085    // For properly formed JSON, if no properties, field has to be null (not just no elements).
086    if (nd.getConfiguration().isEmpty()) {
087      return;
088    }
089
090    properties = new HashMap<>();
091    properties.putAll(nd.getConfiguration());
092  }
093
094  /**
095   * Add property to the namespace.
096   * @param key   attribute name
097   * @param value attribute value
098   */
099  public void addProperty(String key, String value) {
100    if (properties == null) {
101      properties = new HashMap<>();
102    }
103    properties.put(key, value);
104  }
105
106  /** Returns The map of uncategorized namespace properties. */
107  public Map<String, String> getProperties() {
108    if (properties == null) {
109      properties = new HashMap<>();
110    }
111    return properties;
112  }
113
114  public String getNamespaceName() {
115    return namespaceName;
116  }
117
118  /*
119   * (non-Javadoc)
120   * @see java.lang.Object#toString()
121   */
122  @Override
123  public String toString() {
124    StringBuilder sb = new StringBuilder();
125    sb.append("{NAME => \'");
126    sb.append(namespaceName);
127    sb.append("\'");
128    if (properties != null) {
129      for (Map.Entry<String, String> entry : properties.entrySet()) {
130        sb.append(", ");
131        sb.append(entry.getKey());
132        sb.append(" => '");
133        sb.append(entry.getValue());
134        sb.append("\'");
135      }
136    }
137    sb.append("}");
138    return sb.toString();
139  }
140
141  @Override
142  public byte[] createProtobufOutput() {
143    NamespaceProperties.Builder builder = NamespaceProperties.newBuilder();
144    if (properties != null) {
145      for (Map.Entry<String, String> entry : properties.entrySet()) {
146        String key = entry.getKey();
147        NamespaceProperties.Property.Builder property = NamespaceProperties.Property.newBuilder();
148        property.setKey(key);
149        property.setValue(entry.getValue());
150        builder.addProps(property);
151      }
152    }
153    return builder.build().toByteArray();
154  }
155
156  @Override
157  public ProtobufMessageHandler getObjectFromMessage(byte[] message) throws IOException {
158    NamespaceProperties.Builder builder = NamespaceProperties.newBuilder();
159    builder.mergeFrom(message);
160    List<NamespaceProperties.Property> properties = builder.getPropsList();
161    for (NamespaceProperties.Property property : properties) {
162      addProperty(property.getKey(), property.getValue());
163    }
164    return this;
165  }
166
167}