Move integration tests to mdsal-it-parent
[lispflowmapping.git] / mappingservice / yangmodel / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / LispMACAddressSerializer.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.lispflowmapping.lisp.serializer.address;
9
10 import java.nio.ByteBuffer;
11
12 import javax.xml.bind.DatatypeConverter;
13
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispMacAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.mac.MacAddressBuilder;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
18
19 public class LispMACAddressSerializer extends LispAddressSerializer {
20
21     private static final LispMACAddressSerializer INSTANCE = new LispMACAddressSerializer();
22
23     // Private constructor prevents instantiation from other classes
24     private LispMACAddressSerializer() {
25     }
26
27     public static LispMACAddressSerializer getInstance() {
28         return INSTANCE;
29     }
30
31     @Override
32     public int getAddressSize(LispAFIAddress lispAddress) {
33         return Length.MAC;
34     }
35
36     @Override
37     protected LispMacAddress deserializeData(ByteBuffer buffer) {
38         byte[] macBuffer = new byte[6];
39         buffer.get(macBuffer);
40         StringBuilder sb = new StringBuilder(17);
41         for (byte b : macBuffer) {
42             if (sb.length() > 0)
43                 sb.append(':');
44             sb.append(String.format("%02x", b));
45         }
46         return new MacAddressBuilder().setMacAddress(new MacAddress(sb.toString())).setAfi((short) 16389).build();
47     }
48
49     @Override
50     protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
51         LispMacAddress lispMACAddress = (LispMacAddress) lispAddress;
52         String macString = lispMACAddress.getMacAddress().getValue();
53         macString = macString.replaceAll(":", "");
54         buffer.put(DatatypeConverter.parseHexBinary(macString));
55     }
56
57     private interface Length {
58         int MAC = 6;
59     }
60
61 }