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