Migrate lisp-proto implementation to IETF YANG model
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / SourceDestKeySerializer.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
12 import org.opendaylight.lispflowmapping.lisp.type.LispCanonicalAddressFormatEnum;
13 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
14 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKeyBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
26
27 public class SourceDestKeySerializer extends LcafSerializer {
28
29     private static final SourceDestKeySerializer INSTANCE = new SourceDestKeySerializer();
30
31     // Private constructor prevents instantiation from other classes
32     private SourceDestKeySerializer() {
33     }
34
35     public static SourceDestKeySerializer getInstance() {
36         return INSTANCE;
37     }
38
39     @Override
40     protected byte getLcafType() {
41         return LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode();
42     }
43
44     @Override
45     protected short getLcafLength(LispAddress lispAddress) {
46         SourceDestKey sdk = (SourceDestKey) lispAddress.getAddress();
47         return (short) (Length.ALL_FIELDS
48                 + SimpleAddressSerializer.getInstance().getAddressSize(new SimpleAddress(sdk.getSourceDestKey().getSource()))
49                 + SimpleAddressSerializer.getInstance().getAddressSize(new SimpleAddress(sdk.getSourceDestKey().getDest())));
50     }
51
52     @Override
53     protected short getAfi() {
54         return (short) AddressFamily.LispCanonicalAddressFormat.getIntValue();
55     }
56
57     @Override
58     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
59         SourceDestKey sdk = (SourceDestKey) lispAddress.getAddress();
60         buffer.putShort((short) 0);
61         buffer.put((byte) MaskUtil.getMaskForIpPrefix(sdk.getSourceDestKey().getSource()));
62         buffer.put((byte) MaskUtil.getMaskForIpPrefix(sdk.getSourceDestKey().getDest()));
63         SimpleAddressSerializer.getInstance().serialize(buffer, new SimpleAddress(sdk.getSourceDestKey().getSource()));
64         SimpleAddressSerializer.getInstance().serialize(buffer, new SimpleAddress(sdk.getSourceDestKey().getDest()));
65     }
66
67     @Override
68     protected Eid deserializeLcafEidData(ByteBuffer buffer, byte res2, short length, LispAddressSerializerContext ctx) {
69         EidBuilder eb = new EidBuilder();
70         eb.setAddressType(SourceDestKeyLcaf.class);
71         eb.setVirtualNetworkId(getVni(ctx));
72         eb.setAddress(deserializeData(buffer, ctx));
73         return eb.build();
74     }
75
76     @Override
77     protected Rloc deserializeLcafRlocData(ByteBuffer buffer, byte res2, short length, LispAddressSerializerContext ctx) {
78         RlocBuilder rb = new RlocBuilder();
79         rb.setAddressType(SourceDestKeyLcaf.class);
80         rb.setVirtualNetworkId(null);
81         rb.setAddress(deserializeData(buffer, ctx));
82         return rb.build();
83     }
84
85     private Address deserializeData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
86         buffer.getShort();  // reserved bytes
87         short srcMaskLength = (short) ByteUtil.getUnsignedByte(buffer);
88         short dstMaskLength = (short) ByteUtil.getUnsignedByte(buffer);
89         ctx.setMaskLen(srcMaskLength);
90         SimpleAddress srcAddress = SimpleAddressSerializer.getInstance().deserialize(buffer, ctx);
91         ctx.setMaskLen(dstMaskLength);
92         SimpleAddress dstAddress = SimpleAddressSerializer.getInstance().deserialize(buffer, ctx);
93         SourceDestKeyBuilder sdb = new SourceDestKeyBuilder();
94         sdb.setSource(srcAddress.getIpPrefix());
95         sdb.setDest(dstAddress.getIpPrefix());
96         return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKeyBuilder()
97                 .setSourceDestKey(sdb.build()).build();
98     }
99
100     private interface Length {
101         int SOURCE_MASK_LENGTH = 1;
102         int DEST_MASK_LENGTH = 1;
103         int RESERVED = 2;
104         int ALL_FIELDS = SOURCE_MASK_LENGTH + DEST_MASK_LENGTH + RESERVED;
105     }
106 }