Sonar: Move trailing comment on previous empty line
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / Ipv4PrefixSerializer.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.Ipv4Serializer.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.Ipv4PrefixAfi;
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.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
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 IPv4 prefixes from/to String representation.
33  *
34  * @author Lorand Jakab
35  *
36  */
37 public final class Ipv4PrefixSerializer extends LispAddressSerializer {
38
39     protected static final Logger LOG = LoggerFactory.getLogger(Ipv4PrefixSerializer.class);
40
41     private static final Ipv4PrefixSerializer INSTANCE = new Ipv4PrefixSerializer();
42
43     // Private constructor prevents instantiation from other classes
44     private Ipv4PrefixSerializer() {
45     }
46
47     public static Ipv4PrefixSerializer getInstance() {
48         return INSTANCE;
49     }
50
51     @Override
52     public int getAddressSize(LispAddress lispAddress) {
53         // XXX does this need to worry about the mask too?
54         return Length.IPV4;
55     }
56
57     @Override
58     public int getAddressSize(SimpleAddress simpleAddress) {
59         // XXX does this need to worry about the mask too?
60         return Length.IPV4;
61     }
62
63     @Override
64     protected short getAfi() {
65         return (short) AddressFamily.IpV4.getIntValue();
66     }
67
68     @Override
69     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
70         Ipv4Prefix prefix = (Ipv4Prefix) lispAddress.getAddress();
71         String address = MaskUtil.getAddressStringForIpv4Prefix(prefix);
72         buffer.put(InetAddresses.forString(address).getAddress());
73     }
74
75     @Override
76     protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
77         buffer.put(InetAddresses.forString(MaskUtil.getAddressStringForIpPrefix(address.getIpPrefix())).getAddress());
78     }
79
80     @Override
81     protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
82         EidBuilder eb = new EidBuilder();
83         eb.setAddressType(Ipv4PrefixAfi.class);
84         eb.setVirtualNetworkId(getVni(ctx));
85         eb.setAddress(new Ipv4PrefixBuilder().setIpv4Prefix(deserializeData(buffer, ctx)).build());
86         return eb.build();
87     }
88
89     @Override
90     protected Rloc deserializeRlocData(ByteBuffer buffer) {
91         throw new LispSerializationException("RLOCs should not be deserialized into prefixes!");
92     }
93
94     @Override
95     protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
96         return new SimpleAddress(new IpPrefix(deserializeData(buffer, ctx)));
97     }
98
99     private static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix
100             deserializeData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
101         byte[] ipBuffer = new byte[4];
102         InetAddress address = null;
103         buffer.get(ipBuffer);
104         try {
105             address = InetAddress.getByAddress(ipBuffer);
106         } catch (UnknownHostException e) {
107             LOG.debug("Unknown host {}", ipBuffer, e);
108         }
109         return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix(
110                 address.getHostAddress() + "/" + ctx.getMaskLen());
111     }
112 }