Checkstyle: fix issues and enforce on lisp-proto
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / LispAddressSerializer.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  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.iana.afn.safi.rev130704.AddressFamily;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Lcaf;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
23
24 public class LispAddressSerializer {
25
26     private static final LispAddressSerializer INSTANCE = new LispAddressSerializer();
27     public static final InstanceIdType DEFAULT_VNI = new InstanceIdType(0L);
28
29     // Private constructor prevents instantiation from other classes
30     protected LispAddressSerializer() {
31     }
32
33     public static LispAddressSerializer getInstance() {
34         return INSTANCE;
35     }
36
37     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
38         throw new LispSerializationException("Unimplemented method");
39     }
40
41     protected void serializeData(ByteBuffer buffer, SimpleAddress lispAddress) {
42         throw new LispSerializationException("Unimplemented method");
43     }
44
45     protected void serializeData(ByteBuffer buffer, IpPrefix lispAddress) {
46         throw new LispSerializationException("Unimplemented method");
47     }
48
49     protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
50         throw new LispSerializationException("Unimplemented method");
51     }
52
53     protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
54         throw new LispSerializationException("Unimplemented method");
55     }
56
57     protected Eid deserializeLcafEidData(ByteBuffer buffer, byte res2, short length,
58             LispAddressSerializerContext ctx) {
59         throw new LispSerializationException("Unimplemented method");
60     }
61
62     protected Rloc deserializeRlocData(ByteBuffer buffer) {
63         throw new LispSerializationException("Unimplemented method");
64     }
65
66     protected Rloc deserializeLcafRlocData(ByteBuffer buffer, byte res2, short length,
67             LispAddressSerializerContext ctx) {
68         throw new LispSerializationException("Unimplemented method");
69     }
70
71     protected short getAfi() {
72         throw new LispSerializationException("Unimplemented method");
73     }
74
75     protected byte getLcafType() {
76         throw new LispSerializationException("Unimplemented method");
77     }
78
79     protected short getLcafLength(LispAddress lispAddress) {
80         throw new LispSerializationException("Unimplemented method");
81     }
82
83     protected int getAddressSize(SimpleAddress address) {
84         throw new LispSerializationException("Unimplemented method");
85     }
86
87     public int getAddressSize(LispAddress lispAddress) {
88         int size = Length.AFI;
89         if (lispAddress.getVirtualNetworkId() != null) {
90             size += getInstanceIdExtraSize();
91         }
92         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(lispAddress.getAddressType());
93         if (serializer == null) {
94             throw new LispSerializationException("Unknown address type: "
95                     + lispAddress.getAddressType().getSimpleName());
96         }
97         return size + serializer.getAddressSize(lispAddress);
98     }
99
100     protected InstanceIdType getVni(LispAddressSerializerContext ctx) {
101         if (ctx != null) {
102             return ctx.getVni();
103         }
104         return null;
105     }
106
107     public void serialize(ByteBuffer buffer, LispAddress lispAddress) {
108         if (lispAddress.getVirtualNetworkId() != null) {
109             serializeInstanceIdExtra(buffer, lispAddress);
110         }
111         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(lispAddress.getAddressType());
112         if (serializer == null) {
113             throw new LispSerializationException("Unknown address type: "
114                     + lispAddress.getAddressType().getSimpleName());
115         }
116         short afi = serializer.getAfi();
117         if (afi == (short) AddressFamily.LispCanonicalAddressFormat.getIntValue()) {
118             serializer =  LispAddressSerializerFactory.getSerializer(Lcaf.class);
119         }
120         buffer.putShort(afi);
121         serializer.serializeData(buffer, lispAddress);
122     }
123
124     private void serializeInstanceIdExtra(ByteBuffer buffer, LispAddress lispAddress) {
125         buffer.putShort((short) AddressFamily.LispCanonicalAddressFormat.getIntValue());
126         LcafSerializer.getInstance().serializeLCAFAddressHeaderForInstanceId(buffer, lispAddress);
127         InstanceIdSerializer.getInstance().serializeNonLcafAddress(buffer, lispAddress);
128     }
129
130     int getInstanceIdExtraSize() {
131         return LcafSerializer.getInstance().getLcafHeaderSize()
132                 + InstanceIdSerializer.getInstance().getInstanceIdSize()
133                 + Length.AFI;
134     }
135
136     @SuppressWarnings("checkstyle:IllegalCatch")
137     public Eid deserializeEid(ByteBuffer buffer, LispAddressSerializerContext ctx) {
138         short afi = buffer.getShort();
139         // AddressTypeMap indexes IPv4 and IPv6 prefixes (vs simple addresses) with the negative AFI values -1 and -2
140         if ((afi == 1 || afi == 2) && ctx.getMaskLen() != LispAddressSerializerContext.MASK_LEN_MISSING) {
141             afi *= -1;
142         }
143         Class<? extends LispAddressFamily> addressType = AddressTypeMap.getAddressType(afi);
144         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
145         if (serializer == null) {
146             throw new LispSerializationException("Unknown AFI: " + afi);
147         }
148         try {
149             return serializer.deserializeEidData(buffer, ctx);
150         } catch (RuntimeException e) {
151             throw new LispSerializationException("Problem deserializing AFI " + afi + " in EID context", e);
152         }
153     }
154
155     @SuppressWarnings("checkstyle:IllegalCatch")
156     public Rloc deserializeRloc(ByteBuffer buffer) {
157         short afi = buffer.getShort();
158         Class<? extends LispAddressFamily> addressType = AddressTypeMap.getAddressType(afi);
159         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
160         if (serializer == null) {
161             throw new LispSerializationException("Unknown AFI: " + afi);
162         }
163         try {
164             return serializer.deserializeRlocData(buffer);
165         } catch (RuntimeException e) {
166             throw new LispSerializationException("Problem deserializing AFI " + afi + " in RLOC context", e);
167         }
168     }
169
170     private interface Length {
171         int AFI = 2;
172     }
173 }