HONEYCOMB-145 - Utility Class Refactoring
[honeycomb.git] / vpp-common / vpp-translate-utils / src / main / java / io / fd / honeycomb / translate / v3po / 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.v3po.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.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;
28
29 /**
30  * Trait providing logic for translation of ipv4-related data
31  */
32 public interface MacTranslator {
33
34     Splitter COLON_SPLITTER = Splitter.on(':');
35
36     /**
37      * Parse string represented mac address (using ":" as separator) into a byte array
38      */
39     @Nonnull
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);
44     }
45
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>() {
50
51                     private int i = -1;
52
53                     @Override
54                     public void accept(final byte[] bytes, final Byte aByte) {
55                         bytes[++i] = aByte;
56                     }
57                 },
58                 (bytes, bytes2) -> {
59                     throw new UnsupportedOperationException("Parallel collect not supported");
60                 });
61     }
62
63     /**
64      * Converts byte array to address string ,not separated with ":"
65      */
66     default String byteArrayToMacUnseparated(byte[] address) {
67         checkArgument(address.length == 6, "Illegal array length");
68         return Hex.encodeHexString(address);
69     }
70
71     /**
72      * Converts byte array to address string ,separated with ":"
73      */
74     default String byteArrayToMacSeparated(byte[] address) {
75         checkArgument(address.length == 6, "Illegal array length");
76
77         String unseparatedAddress = Hex.encodeHexString(address);
78         String separated = "";
79
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);
83             } else {
84                 separated = separated + unseparatedAddress.substring(0 + i, 2 + i) + ":";
85             }
86         }
87
88         return separated;
89     }
90
91     /**
92      * Converts MAC string to byte array
93      */
94     default byte[] macToByteArray(String mac) {
95         checkNotNull(mac, "MAC cannot be null");
96
97         mac = mac.replace(":", "");
98
99         try {
100             return Hex.decodeHex(mac.toCharArray());
101         } catch (DecoderException e) {
102             throw new IllegalArgumentException("Unable to convert mac", e);
103         }
104     }
105
106     default byte parseHexByte(final String aByte) {
107         return (byte) Integer.parseInt(aByte, 16);
108     }
109 }