2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.translate.v3po.util;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import com.google.common.base.Splitter;
23 import java.util.List;
24 import java.util.function.BiConsumer;
25 import javax.annotation.Nonnull;
26 import org.apache.commons.codec.DecoderException;
27 import org.apache.commons.codec.binary.Hex;
30 * Trait providing logic for translation of ipv4-related data
32 public interface MacTranslator {
34 Splitter COLON_SPLITTER = Splitter.on(':');
37 * Parse string represented mac address (using ":" as separator) into a byte array
40 default byte[] parseMac(@Nonnull final String macAddress) {
41 final List<String> parts = COLON_SPLITTER.splitToList(macAddress);
42 checkArgument(parts.size() == 6, "Mac address is expected to have 6 parts but was: %s", macAddress);
43 return parseMacLikeString(parts);
46 default byte[] parseMacLikeString(final List<String> strings) {
47 return strings.stream().limit(6).map(this::parseHexByte).collect(
48 () -> new byte[strings.size()],
49 new BiConsumer<byte[], Byte>() {
54 public void accept(final byte[] bytes, final Byte aByte) {
59 throw new UnsupportedOperationException("Parallel collect not supported");
64 * Converts byte array to address string ,not separated with ":"
66 default String byteArrayToMacUnseparated(byte[] address) {
67 checkArgument(address.length == 6, "Illegal array length");
68 return Hex.encodeHexString(address);
72 * Converts byte array to address string ,separated with ":"
74 default String byteArrayToMacSeparated(byte[] address) {
75 checkArgument(address.length == 6, "Illegal array length");
77 String unseparatedAddress = Hex.encodeHexString(address);
78 String separated = "";
80 for (int i = 0; i < unseparatedAddress.length(); i = i + 2) {
81 if (i == (unseparatedAddress.length() - 2)) {
82 separated = separated + unseparatedAddress.substring(0 + i, 2 + i);
84 separated = separated + unseparatedAddress.substring(0 + i, 2 + i) + ":";
92 * Converts MAC string to byte array
94 default byte[] macToByteArray(String mac) {
95 checkNotNull(mac, "MAC cannot be null");
97 mac = mac.replace(":", "");
100 return Hex.decodeHex(mac.toCharArray());
101 } catch (DecoderException e) {
102 throw new IllegalArgumentException("Unable to convert mac", e);
106 default byte parseHexByte(final String aByte) {
107 return (byte) Integer.parseInt(aByte, 16);