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 */ 018 019package org.apache.hadoop.hbase.rsgroup; 020 021import java.util.ArrayList; 022import java.util.List; 023import java.util.stream.Collectors; 024 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.net.Address; 027import org.apache.hadoop.hbase.protobuf.ProtobufUtil; 028import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos; 029import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair; 030import org.apache.hadoop.hbase.protobuf.generated.RSGroupProtos; 031import org.apache.hadoop.hbase.protobuf.generated.TableProtos; 032import org.apache.yetus.audience.InterfaceAudience; 033 034@InterfaceAudience.Private 035final class RSGroupProtobufUtil { 036 private RSGroupProtobufUtil() { 037 } 038 039 static RSGroupInfo toGroupInfo(RSGroupProtos.RSGroupInfo proto) { 040 RSGroupInfo rsGroupInfo = new RSGroupInfo(proto.getName()); 041 for(HBaseProtos.ServerName el: proto.getServersList()) { 042 rsGroupInfo.addServer(Address.fromParts(el.getHostName(), el.getPort())); 043 } 044 for(TableProtos.TableName pTableName: proto.getTablesList()) { 045 rsGroupInfo.addTable(ProtobufUtil.toTableName(pTableName)); 046 } 047 proto.getConfigurationList().forEach(pair -> 048 rsGroupInfo.setConfiguration(pair.getName(), pair.getValue())); 049 return rsGroupInfo; 050 } 051 052 static RSGroupProtos.RSGroupInfo toProtoGroupInfo(RSGroupInfo pojo) { 053 List<TableProtos.TableName> tables = new ArrayList<>(pojo.getTables().size()); 054 for(TableName arg: pojo.getTables()) { 055 tables.add(ProtobufUtil.toProtoTableName(arg)); 056 } 057 List<HBaseProtos.ServerName> hostports = new ArrayList<>(pojo.getServers().size()); 058 for(Address el: pojo.getServers()) { 059 hostports.add(HBaseProtos.ServerName.newBuilder() 060 .setHostName(el.getHostname()) 061 .setPort(el.getPort()) 062 .build()); 063 } 064 List<NameStringPair> configuration = pojo.getConfiguration().entrySet() 065 .stream().map(entry -> NameStringPair.newBuilder() 066 .setName(entry.getKey()).setValue(entry.getValue()).build()) 067 .collect(Collectors.toList()); 068 return RSGroupProtos.RSGroupInfo.newBuilder().setName(pojo.getName()) 069 .addAllServers(hostports).addAllTables(tables).addAllConfiguration(configuration).build(); 070 } 071}