Bump versions by x.(y+1).z
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / Ipv6Serializer.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  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 com.google.common.net.InetAddresses;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.nio.ByteBuffer;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6Afi;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Class to (de)serialize IPv6 addresses from/to String representation.
31  *
32  * @author Lorand Jakab
33  */
34 public final class Ipv6Serializer extends LispAddressSerializer {
35
36     protected static final Logger LOG = LoggerFactory.getLogger(Ipv6Serializer.class);
37
38     private static final Ipv6Serializer INSTANCE = new Ipv6Serializer();
39
40     // Private constructor prevents instantiation from other classes
41     private Ipv6Serializer() {
42     }
43
44     public static Ipv6Serializer getInstance() {
45         return INSTANCE;
46     }
47
48     @Override
49     public int getAddressSize(LispAddress lispAddress) {
50         return Length.IPV6;
51     }
52
53     @Override
54     public int getAddressSize(SimpleAddress simpleAddress) {
55         return Length.IPV6;
56     }
57
58     @Override
59     protected short getAfi() {
60         return (short) AddressFamily.IpV6.getIntValue();
61     }
62
63     @Override
64     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
65         Ipv6 address = (Ipv6) lispAddress.getAddress();
66         buffer.put(InetAddresses.forString(address.getIpv6().getValue()).getAddress());
67     }
68
69     @Override
70     protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
71         buffer.put(InetAddresses.forString(address.getIpAddress().getIpv6Address().getValue()).getAddress());
72     }
73
74     @Override
75     protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
76         EidBuilder eb = new EidBuilder();
77         eb.setAddressType(Ipv6Afi.VALUE);
78         eb.setVirtualNetworkId(getVni(ctx));
79         eb.setAddress(new Ipv6Builder().setIpv6(deserializeData(buffer)).build());
80         return eb.build();
81     }
82
83     @Override
84     protected Rloc deserializeRlocData(ByteBuffer buffer) {
85         RlocBuilder rb = new RlocBuilder();
86         rb.setAddressType(Ipv6Afi.VALUE);
87         rb.setVirtualNetworkId(null);
88         rb.setAddress(new Ipv6Builder().setIpv6(deserializeData(buffer)).build());
89         return rb.build();
90     }
91
92     @Override
93     protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
94         return new SimpleAddress(new IpAddress(deserializeData(buffer)));
95     }
96
97     private static Ipv6Address deserializeData(ByteBuffer buffer) {
98         byte[] ipBuffer = new byte[16];
99         InetAddress address = null;
100         buffer.get(ipBuffer);
101         try {
102             address = InetAddress.getByAddress(ipBuffer);
103         } catch (UnknownHostException e) {
104             LOG.debug("Unknown host {}", ipBuffer, e);
105         }
106         return new Ipv6Address(address.getHostAddress());
107     }
108
109     protected interface Length {
110         int IPV6 = 16;
111     }
112
113 }