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