14cc289093746e7cf194002f56250d305c31f931
[honeycomb.git] / vpp-common / vpp-translate-utils / src / main / java / io / fd / honeycomb / translate / vpp / util / MacTranslator.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.translate.vpp.util;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21
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;
29
30 /**
31  * Trait providing logic for translation of MAC address data
32  */
33 public interface MacTranslator {
34
35     Splitter COLON_SPLITTER = Splitter.on(':');
36
37     /**
38      * Parse string represented mac address (using ":" as separator) into a byte array
39      */
40     @Nonnull
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);
45     }
46
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>() {
51
52                     private int i = -1;
53
54                     @Override
55                     public void accept(final byte[] bytes, final Byte aByte) {
56                         bytes[++i] = aByte;
57                     }
58                 },
59                 (bytes, bytes2) -> {
60                     throw new UnsupportedOperationException("Parallel collect not supported");
61                 });
62     }
63
64     /**
65      * Converts byte array to address string ,not separated with ":"
66      */
67     default String byteArrayToMacUnseparated(byte[] address) {
68         checkArgument(address.length >= 6, "Illegal array length");
69         return Hex.encodeHexString(Arrays.copyOf(address, 6));
70     }
71
72     /**
73      * Converts byte array to address string ,separated with ":"
74      */
75     default String byteArrayToMacSeparated(byte[] address) {
76         checkArgument(address.length >= 6, "Illegal array length");
77
78         String unseparatedAddress = Hex.encodeHexString(Arrays.copyOf(address, 6));
79         String separated = "";
80
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);
84             } else {
85                 separated = separated + unseparatedAddress.substring(0 + i, 2 + i) + ":";
86             }
87         }
88
89         return separated;
90     }
91
92     /**
93      * Converts MAC string to byte array
94      */
95     default byte[] macToByteArray(String mac) {
96         checkNotNull(mac, "MAC cannot be null");
97
98         mac = mac.replace(":", "");
99
100         try {
101             return Hex.decodeHex(mac.toCharArray());
102         } catch (DecoderException e) {
103             throw new IllegalArgumentException("Unable to convert mac", e);
104         }
105     }
106
107     default byte parseHexByte(final String aByte) {
108         return (byte) Integer.parseInt(aByte, 16);
109     }
110 }