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