22fd3ba8477c67e8c99ea188e3a86157d2e9fbbd
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / Ipv6PrefixSerializer.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.lispflowmapping.lisp.serializer.address.Ipv6Serializer.Length;
15 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
16 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6PrefixAfi;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6PrefixBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Class to (de)serialize IPv6 prefixes from/to String representation.
32  *
33  * @author Lorand Jakab
34  */
35 public final class Ipv6PrefixSerializer extends LispAddressSerializer {
36
37     protected static final Logger LOG = LoggerFactory.getLogger(Ipv6PrefixSerializer.class);
38
39     private static final Ipv6PrefixSerializer INSTANCE = new Ipv6PrefixSerializer();
40
41     // Private constructor prevents instantiation from other classes
42     private Ipv6PrefixSerializer() {
43     }
44
45     public static Ipv6PrefixSerializer getInstance() {
46         return INSTANCE;
47     }
48
49     @Override
50     public int getAddressSize(LispAddress lispAddress) {
51         // XXX does this need to worry about the mask too?
52         return Length.IPV6;
53     }
54
55     @Override
56     public int getAddressSize(SimpleAddress simpleAddress) {
57         // XXX does this need to worry about the mask too?
58         return Length.IPV6;
59     }
60
61     @Override
62     protected short getAfi() {
63         return (short) AddressFamily.IpV6.getIntValue();
64     }
65
66     @Override
67     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
68         Ipv6Prefix prefix = (Ipv6Prefix) lispAddress.getAddress();
69         String address = MaskUtil.getAddressStringForIpv6Prefix(prefix);
70         buffer.put(InetAddresses.forString(address).getAddress());
71     }
72
73     @Override
74     protected void serializeData(ByteBuffer buffer, IpPrefix prefix) {
75         buffer.put(InetAddresses.forString(prefix.getIpv6Prefix().getValue()).getAddress());
76     }
77
78     @Override
79     protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
80         buffer.put(InetAddresses.forString(MaskUtil.getAddressStringForIpPrefix(address.getIpPrefix())).getAddress());
81     }
82
83     @Override
84     protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
85         EidBuilder eb = new EidBuilder();
86         eb.setAddressType(Ipv6PrefixAfi.class);
87         eb.setVirtualNetworkId(getVni(ctx));
88         eb.setAddress(new Ipv6PrefixBuilder().setIpv6Prefix(deserializeData(buffer, ctx)).build());
89         return eb.build();
90     }
91
92     @Override
93     protected Rloc deserializeRlocData(ByteBuffer buffer) {
94         throw new LispSerializationException("RLOCs should not be deserialized into prefixes!");
95     }
96
97     @Override
98     protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
99         return new SimpleAddress(new IpPrefix(deserializeData(buffer, ctx)));
100     }
101
102     private static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix
103             deserializeData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
104         byte[] ipBuffer = new byte[16];
105         InetAddress address = null;
106         buffer.get(ipBuffer);
107         try {
108             address = InetAddress.getByAddress(ipBuffer);
109         } catch (UnknownHostException e) {
110             LOG.debug("Unknown host {}", ipBuffer, e);
111         }
112         return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix(
113                 address.getHostAddress() + "/" + ctx.getMaskLen());
114     }
115 }