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, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.io.network.util; 020 021import com.google.protobuf.ByteString; 022import org.apache.commons.lang.StringUtils; 023import org.apache.reef.io.network.proto.ReefNetworkGroupCommProtos; 024import org.apache.reef.wake.ComparableIdentifier; 025import org.apache.reef.wake.Identifier; 026import org.apache.reef.wake.IdentifierFactory; 027 028import java.net.Inet4Address; 029import java.util.ArrayList; 030import java.util.Collections; 031import java.util.Comparator; 032import java.util.List; 033 034/** 035 * Utility class for REEF IO network module. 036 */ 037public final class Utils { 038 039 private static final String DELIMITER = "-"; 040 041 /** 042 * Parse a string of multiple IDs. 043 * 044 * @param ids A string containing multiple IDs 045 * @param factory An Identifier factory 046 * @param <T> A type 047 * @return A list of identifier 048 */ 049 public static <T extends Identifier> List<T> parseList(final String ids, final IdentifierFactory factory) { 050 final List<T> result = new ArrayList<>(); 051 for (final String token : ids.split(DELIMITER)) { 052 result.add((T) factory.getNewInstance(token.trim())); 053 } 054 return result; 055 } 056 057 public static String listToString(final List<ComparableIdentifier> ids) { 058 return StringUtils.join(ids, DELIMITER); 059 } 060 061 public static List<Integer> createUniformCounts(final int elemSize, final int childSize) { 062 final int remainder = elemSize % childSize; 063 final int quotient = elemSize / childSize; 064 final ArrayList<Integer> result = new ArrayList<>(childSize); 065 result.addAll(Collections.nCopies(remainder, quotient + 1)); 066 result.addAll(Collections.nCopies(childSize - remainder, quotient)); 067 return Collections.unmodifiableList(result); 068 } 069 070 private static class AddressComparator implements Comparator<Inet4Address> { 071 @Override 072 public int compare(final Inet4Address aa, final Inet4Address ba) { 073 final byte[] a = aa.getAddress(); 074 final byte[] b = ba.getAddress(); 075 // local subnet comes after all else. 076 if (a[0] == 127 && b[0] != 127) { 077 return 1; 078 } 079 if (a[0] != 127 && b[0] == 127) { 080 return -1; 081 } 082 for (int i = 0; i < 4; i++) { 083 if (a[i] < b[i]) { 084 return -1; 085 } 086 if (a[i] > b[i]) { 087 return 1; 088 } 089 } 090 return 0; 091 } 092 } 093 094 public static ReefNetworkGroupCommProtos.GroupCommMessage bldGCM( 095 final ReefNetworkGroupCommProtos.GroupCommMessage.Type msgType, 096 final Identifier from, final Identifier to, final byte[]... elements) { 097 098 final ReefNetworkGroupCommProtos.GroupCommMessage.Builder gcmBuilder = 099 ReefNetworkGroupCommProtos.GroupCommMessage.newBuilder() 100 .setType(msgType) 101 .setSrcid(from.toString()) 102 .setDestid(to.toString()); 103 104 final ReefNetworkGroupCommProtos.GroupMessageBody.Builder bodyBuilder = 105 ReefNetworkGroupCommProtos.GroupMessageBody.newBuilder(); 106 107 for (final byte[] element : elements) { 108 bodyBuilder.setData(ByteString.copyFrom(element)); 109 gcmBuilder.addMsgs(bodyBuilder.build()); 110 } 111 112 return gcmBuilder.build(); 113 } 114 115 /** 116 * Empty private constructor to prohibit instantiation of utility class. 117 */ 118 private Utils() { 119 } 120}