Reorganize mappingservice.implementation
[lispflowmapping.git] / mappingservice / yangmodel / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / LispIpv6AddressSerializer.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.net.Inet6Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.nio.ByteBuffer;
14
15 import org.opendaylight.lispflowmapping.lisp.type.AddressFamilyNumberEnum;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispIpv6Address;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv6.Ipv6AddressBuilder;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
20
21 public class LispIpv6AddressSerializer extends LispAddressSerializer {
22
23     private static final LispIpv6AddressSerializer INSTANCE = new LispIpv6AddressSerializer();
24
25     // Private constructor prevents instantiation from other classes
26     private LispIpv6AddressSerializer() {
27     }
28
29     public static LispIpv6AddressSerializer getInstance() {
30         return INSTANCE;
31     }
32
33     @Override
34     public int getAddressSize(LispAFIAddress lispAddress) {
35         return Length.IPV6;
36     }
37
38     @Override
39     protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
40         LispIpv6Address lispIpvAddress = (LispIpv6Address) lispAddress;
41         try {
42             buffer.put(Inet6Address.getByName(lispIpvAddress.getIpv6Address().getValue()).getAddress());
43         } catch (UnknownHostException e) {
44         }
45     }
46
47     @Override
48     protected LispIpv6Address deserializeData(ByteBuffer buffer) {
49         byte[] ipBuffer = new byte[16];
50         InetAddress address = null;
51         buffer.get(ipBuffer);
52         try {
53             address = InetAddress.getByAddress(ipBuffer);
54         } catch (UnknownHostException e) {
55         }
56         return new Ipv6AddressBuilder().setIpv6Address(new Ipv6Address(address.getHostAddress())).setAfi((short) AddressFamilyNumberEnum.IP6.getIanaCode())
57                 .build();
58     }
59
60     private interface Length {
61         int IPV6 = 16;
62     }
63
64 }