Migrate lisp-proto implementation to IETF YANG model
[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 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) (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)));
76                 } else {
77                     buffer.put((byte) 0);
78                 }
79                 SimpleAddressSerializer.getInstance().serialize(buffer, hop.getAddress());
80             }
81         }
82     }
83
84     @Override
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));
90         return eb.build();
91     }
92
93     @Override
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));
99         return rb.build();
100     }
101
102     private Address deserializeData(ByteBuffer buffer, short length, LispAddressSerializerContext ctx) {
103         List<Hop> hops = new ArrayList<Hop>();
104         while (length > 0) {
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());
116         }
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();
119     }
120
121     private interface Flags {
122         int LOOKUP = 0x04;
123         int RLOC_PROBE = 0x02;
124         int STRICT = 0x01;
125     }
126
127 }