2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.lisp.serializer.address;
10 import java.nio.ByteBuffer;
11 import java.util.ArrayList;
12 import java.util.List;
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;
32 public final class ExplicitLocatorPathSerializer extends LcafSerializer {
34 private static final ExplicitLocatorPathSerializer INSTANCE = new ExplicitLocatorPathSerializer();
36 // Private constructor prevents instantiation from other classes
37 private ExplicitLocatorPathSerializer() {
40 public static ExplicitLocatorPathSerializer getInstance() {
45 protected byte getLcafType() {
46 return LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING.getLispCode();
50 protected short getLcafLength(LispAddress lispAddress) {
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;
62 protected short getAfi() {
63 return (short) AddressFamily.LispCanonicalAddressFormat.getIntValue();
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()) {
72 if (hop.getLrsBits() != null) {
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)));
80 SimpleAddressSerializer.getInstance().serialize(buffer, hop.getAddress());
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));
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));
104 private Address deserializeData(ByteBuffer buffer, short lcafLength, LispAddressSerializerContext ctx) {
105 List<Hop> hops = new ArrayList<Hop>();
106 short length = lcafLength;
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());
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();
125 private interface Flags {
127 int RLOC_PROBE = 0x02;