Checkstyle: fix issues and enforce on lisp-proto
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / ExplicitLocatorPathSerializer.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 java.util.ArrayList;
12 import java.util.List;
13
14 import org.apache.commons.lang3.BooleanUtils;
15 import org.opendaylight.lispflowmapping.lisp.type.LispCanonicalAddressFormatEnum;
16 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
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.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.ExplicitLocatorPathBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop.LrsBits;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.HopBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
31
32 public final class ExplicitLocatorPathSerializer extends LcafSerializer {
33
34     private static final ExplicitLocatorPathSerializer INSTANCE = new ExplicitLocatorPathSerializer();
35
36     // Private constructor prevents instantiation from other classes
37     private ExplicitLocatorPathSerializer() {
38     }
39
40     public static ExplicitLocatorPathSerializer getInstance() {
41         return INSTANCE;
42     }
43
44     @Override
45     protected byte getLcafType() {
46         return LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING.getLispCode();
47     }
48
49     @Override
50     protected short getLcafLength(LispAddress lispAddress) {
51         short totalSize = 0;
52         ExplicitLocatorPath elp = (ExplicitLocatorPath) lispAddress.getAddress();
53         if (elp.getExplicitLocatorPath() != null) {
54             for (Hop hop : elp.getExplicitLocatorPath().getHop()) {
55                 totalSize += SimpleAddressSerializer.getInstance().getAddressSize(hop.getAddress()) + 2;
56             }
57         }
58         return totalSize;
59     }
60
61     @Override
62     protected short getAfi() {
63         return (short) AddressFamily.LispCanonicalAddressFormat.getIntValue();
64     }
65
66     @Override
67     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
68         ExplicitLocatorPath elp = (ExplicitLocatorPath) lispAddress.getAddress();
69         if (elp.getExplicitLocatorPath() != null) {
70             for (Hop hop : elp.getExplicitLocatorPath().getHop()) {
71                 buffer.put((byte) 0);
72                 if (hop.getLrsBits() != null) {
73                     buffer.put((byte) (
74                             ByteUtil.boolToBit(BooleanUtils.isTrue(hop.getLrsBits().isLookup()), Flags.LOOKUP)
75                           | ByteUtil.boolToBit(BooleanUtils.isTrue(hop.getLrsBits().isRlocProbe()), Flags.RLOC_PROBE)
76                           | ByteUtil.boolToBit(BooleanUtils.isTrue(hop.getLrsBits().isStrict()), Flags.STRICT)));
77                 } else {
78                     buffer.put((byte) 0);
79                 }
80                 SimpleAddressSerializer.getInstance().serialize(buffer, hop.getAddress());
81             }
82         }
83     }
84
85     @Override
86     protected Eid deserializeLcafEidData(ByteBuffer buffer, byte res2, short length, LispAddressSerializerContext ctx) {
87         EidBuilder eb = new EidBuilder();
88         eb.setAddressType(ExplicitLocatorPathLcaf.class);
89         eb.setVirtualNetworkId(getVni(ctx));
90         eb.setAddress(deserializeData(buffer, length, ctx));
91         return eb.build();
92     }
93
94     @Override
95     protected Rloc deserializeLcafRlocData(ByteBuffer buffer, byte res2, short length,
96             LispAddressSerializerContext ctx) {
97         RlocBuilder rb = new RlocBuilder();
98         rb.setAddressType(ExplicitLocatorPathLcaf.class);
99         rb.setVirtualNetworkId(null);
100         rb.setAddress(deserializeData(buffer, length, ctx));
101         return rb.build();
102     }
103
104     private Address deserializeData(ByteBuffer buffer, short lcafLength, LispAddressSerializerContext ctx) {
105         List<Hop> hops = new ArrayList<Hop>();
106         short length = lcafLength;
107         while (length > 0) {
108             byte flags = (byte) buffer.getShort();
109             boolean lookup = ByteUtil.extractBit(flags, Flags.LOOKUP);
110             boolean rlocProbe = ByteUtil.extractBit(flags, Flags.RLOC_PROBE);
111             boolean strict = ByteUtil.extractBit(flags, Flags.STRICT);
112             SimpleAddress address = SimpleAddressSerializer.getInstance().deserialize(buffer, ctx);
113             HopBuilder builder = new HopBuilder();
114             builder.setLrsBits(new LrsBits(lookup, rlocProbe, strict));
115             builder.setAddress(address);
116             builder.setHopId("Hop " + (hops.size() + 1));
117             length -= SimpleAddressSerializer.getInstance().getAddressSize(address) + 2;
118             hops.add(builder.build());
119         }
120         return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
121                 .lisp.address.address.ExplicitLocatorPathBuilder()
122                 .setExplicitLocatorPath(new ExplicitLocatorPathBuilder().setHop(hops).build()).build();
123     }
124
125     private interface Flags {
126         int LOOKUP = 0x04;
127         int RLOC_PROBE = 0x02;
128         int STRICT = 0x01;
129     }
130
131 }