Checkstyle: fix issues and enforce on lisp-proto
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / SimpleAddressSerializer.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 java.nio.ByteBuffer;
11 import org.opendaylight.lispflowmapping.lisp.serializer.address.factory.LispAddressSerializerFactory;
12 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
13 import org.opendaylight.lispflowmapping.lisp.util.AddressTypeMap;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddressFamily;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
16
17 /**
18  * Class to (de)serialize addresses that can be used in an LCAF.
19  *
20  * @author Lorand Jakab
21  *
22  */
23 public class SimpleAddressSerializer {
24
25     private static final SimpleAddressSerializer INSTANCE = new SimpleAddressSerializer();
26
27     // Private constructor prevents instantiation from other classes
28     protected SimpleAddressSerializer() {
29     }
30
31     public static SimpleAddressSerializer getInstance() {
32         return INSTANCE;
33     }
34
35     public int getAddressSize(SimpleAddress address) {
36         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(
37                 AddressTypeMap.getSimpleAddressInnerType(address));
38         return Length.AFI + serializer.getAddressSize(address);
39     }
40
41     public void serialize(ByteBuffer buffer, SimpleAddress address) {
42         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(
43                 AddressTypeMap.getSimpleAddressInnerType(address));
44         buffer.putShort(serializer.getAfi());
45         serializer.serializeData(buffer, address);
46     }
47
48     @SuppressWarnings("checkstyle:IllegalCatch")
49     public SimpleAddress deserialize(ByteBuffer buffer, LispAddressSerializerContext ctx) {
50         short afi = buffer.getShort();
51         // AddressTypeMap indexes IPv4 and IPv6 prefixes (vs simple addresses) with the negative AFI values -1 and -2
52         if ((afi == 1 || afi == 2) && ctx != null
53                 && ctx.getMaskLen() != LispAddressSerializerContext.MASK_LEN_MISSING) {
54             afi *= -1;
55         }
56         Class<? extends LispAddressFamily> addressType = AddressTypeMap.getAddressType(afi);
57         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
58         if (serializer == null) {
59             throw new LispSerializationException("Unknown AFI: " + afi);
60         }
61         try {
62             return serializer.deserializeSimpleAddressData(buffer, ctx);
63         } catch (RuntimeException e) {
64             throw new LispSerializationException("Problem deserializing AFI " + afi + " in SimpleAddress context", e);
65         }
66     }
67
68     private interface Length {
69         int AFI = 2;
70     }
71 }