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.vpp.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.Arrays;
24 import java.util.List;
25 import java.util.function.BiConsumer;
26 import javax.annotation.Nonnull;
27 import org.apache.commons.codec.DecoderException;
28 import org.apache.commons.codec.binary.Hex;
31 * Trait providing logic for translation of MAC address data
33 public interface MacTranslator {
35 Splitter COLON_SPLITTER = Splitter.on(':');
38 * Parse string represented mac address (using ":" as separator) into a byte array
41 default byte[] parseMac(@Nonnull final String macAddress) {
42 final List<String> parts = COLON_SPLITTER.splitToList(macAddress);
43 checkArgument(parts.size() == 6, "Mac address is expected to have 6 parts but was: %s", macAddress);
44 return parseMacLikeString(parts);
47 default byte[] parseMacLikeString(final List<String> strings) {
48 return strings.stream().limit(6).map(this::parseHexByte).collect(
49 () -> new byte[strings.size()],
50 new BiConsumer<byte[], Byte>() {
55 public void accept(final byte[] bytes, final Byte aByte) {
60 throw new UnsupportedOperationException("Parallel collect not supported");
65 * Converts byte array to address string ,not separated with ":"
67 default String byteArrayToMacUnseparated(byte[] address) {
68 checkArgument(address.length >= 6, "Illegal array length");
69 return Hex.encodeHexString(Arrays.copyOf(address, 6));
73 * Converts byte array to address string ,separated with ":"
75 default String byteArrayToMacSeparated(byte[] address) {
76 checkArgument(address.length >= 6, "Illegal array length");
78 String unseparatedAddress = Hex.encodeHexString(Arrays.copyOf(address, 6));
79 String separated = "";
81 for (int i = 0; i < unseparatedAddress.length(); i = i + 2) {
82 if (i == (unseparatedAddress.length() - 2)) {
83 separated = separated + unseparatedAddress.substring(0 + i, 2 + i);
85 separated = separated + unseparatedAddress.substring(0 + i, 2 + i) + ":";
93 * Converts MAC string to byte array
95 default byte[] macToByteArray(String mac) {
96 checkNotNull(mac, "MAC cannot be null");
98 mac = mac.replace(":", "");
101 return Hex.decodeHex(mac.toCharArray());
102 } catch (DecoderException e) {
103 throw new IllegalArgumentException("Unable to convert mac", e);
107 default byte parseHexByte(final String aByte) {
108 return (byte) Integer.parseInt(aByte, 16);