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 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) {
73 buffer.put((byte) (ByteUtil.boolToBit(BooleanUtils.isTrue(hop.getLrsBits().isLookup()), Flags.LOOKUP) | //
74 ByteUtil.boolToBit(BooleanUtils.isTrue(hop.getLrsBits().isRlocProbe()), Flags.RLOC_PROBE) | //
75 ByteUtil.boolToBit(BooleanUtils.isTrue(hop.getLrsBits().isStrict()), Flags.STRICT)));
79 SimpleAddressSerializer.getInstance().serialize(buffer, hop.getAddress());
85 protected Eid deserializeLcafEidData(ByteBuffer buffer, byte res2, short length, LispAddressSerializerContext ctx) {
86 EidBuilder eb = new EidBuilder();
87 eb.setAddressType(ExplicitLocatorPathLcaf.class);
88 eb.setVirtualNetworkId(getVni(ctx));
89 eb.setAddress(deserializeData(buffer, length, ctx));
94 protected Rloc deserializeLcafRlocData(ByteBuffer buffer, byte res2, short length, LispAddressSerializerContext ctx) {
95 RlocBuilder rb = new RlocBuilder();
96 rb.setAddressType(ExplicitLocatorPathLcaf.class);
97 rb.setVirtualNetworkId(null);
98 rb.setAddress(deserializeData(buffer, length, ctx));
102 private Address deserializeData(ByteBuffer buffer, short length, LispAddressSerializerContext ctx) {
103 List<Hop> hops = new ArrayList<Hop>();
105 byte flags = (byte) buffer.getShort();
106 boolean lookup = ByteUtil.extractBit(flags, Flags.LOOKUP);
107 boolean rlocProbe = ByteUtil.extractBit(flags, Flags.RLOC_PROBE);
108 boolean strict = ByteUtil.extractBit(flags, Flags.STRICT);
109 SimpleAddress address = SimpleAddressSerializer.getInstance().deserialize(buffer, ctx);
110 HopBuilder builder = new HopBuilder();
111 builder.setLrsBits(new LrsBits(lookup, rlocProbe, strict));
112 builder.setAddress(address);
113 builder.setHopId("Hop " + (hops.size()+1));
114 length -= SimpleAddressSerializer.getInstance().getAddressSize(address) + 2;
115 hops.add(builder.build());
117 return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder()
118 .setExplicitLocatorPath(new ExplicitLocatorPathBuilder().setHop(hops).build()).build();
121 private interface Flags {
123 int RLOC_PROBE = 0x02;