e5c306a05b51f9bd4e004ac07611586773bef314
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / ApplicationDataSerializer.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.NumberUtil;
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.inet.types.rev130715.PortNumber;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ApplicationDataLcaf;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.application.data.ApplicationData;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.application.data.ApplicationDataBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
27
28 public final class ApplicationDataSerializer extends LcafSerializer {
29
30     private static final ApplicationDataSerializer INSTANCE = new ApplicationDataSerializer();
31
32     // Private constructor prevents instantiation from other classes
33     private ApplicationDataSerializer() {
34     }
35
36     public static ApplicationDataSerializer getInstance() {
37         return INSTANCE;
38     }
39
40     @Override
41     protected byte getLcafType() {
42         return LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode();
43     }
44
45     @Override
46     protected short getLcafLength(LispAddress lispAddress) {
47         ApplicationData appData = ((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types
48                 .rev151105.lisp.address.address.ApplicationData) lispAddress.getAddress()).getApplicationData();
49         return (short) (Length.ALL_FIELDS + SimpleAddressSerializer.getInstance().getAddressSize(appData.getAddress()));
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         ApplicationData appData = ((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types
60                 .rev151105.lisp.address.address.ApplicationData) lispAddress.getAddress()).getApplicationData();
61         buffer.put(ByteUtil.partialIntToByteArray(NumberUtil.asInt(appData.getIpTos()), Length.TOC));
62         buffer.put((byte) NumberUtil.asShort(appData.getProtocol()));
63         if (appData.getLocalPortLow() != null) {
64             buffer.putShort(NumberUtil.asShort(appData.getLocalPortLow().getValue().shortValue()));
65         } else {
66             buffer.putShort((short) 0);
67         }
68         if (appData.getLocalPortHigh() != null) {
69             buffer.putShort(NumberUtil.asShort(appData.getLocalPortHigh().getValue().shortValue()));
70         } else {
71             buffer.putShort((short) 0);
72         }
73         if (appData.getRemotePortLow() != null) {
74             buffer.putShort(NumberUtil.asShort(appData.getRemotePortLow().getValue().shortValue()));
75         } else {
76             buffer.putShort((short) 0);
77         }
78         if (appData.getRemotePortHigh() != null) {
79             buffer.putShort(NumberUtil.asShort(appData.getRemotePortHigh().getValue().shortValue()));
80         } else {
81             buffer.putShort((short) 0);
82         }
83         SimpleAddressSerializer.getInstance().serialize(buffer, appData.getAddress());
84     }
85
86     @Override
87     protected Eid deserializeLcafEidData(ByteBuffer buffer, byte res2, short length, LispAddressSerializerContext ctx) {
88         EidBuilder eb = new EidBuilder();
89         eb.setAddressType(ApplicationDataLcaf.class);
90         eb.setVirtualNetworkId(getVni(ctx));
91         eb.setAddress(deserializeData(buffer, ctx));
92         return eb.build();
93     }
94
95     @Override
96     protected Rloc deserializeLcafRlocData(ByteBuffer buffer, byte res2, short length,
97             LispAddressSerializerContext ctx) {
98         RlocBuilder eb = new RlocBuilder();
99         eb.setAddressType(ApplicationDataLcaf.class);
100         eb.setVirtualNetworkId(null);
101         eb.setAddress(deserializeData(buffer, ctx));
102         return eb.build();
103     }
104
105     private Address deserializeData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
106         ApplicationDataBuilder builder = new ApplicationDataBuilder();
107         byte[] rawIPTos = new byte[3];
108         buffer.get(rawIPTos);
109         builder.setIpTos(ByteUtil.getPartialInt(rawIPTos));
110         builder.setProtocol((short) ByteUtil.getUnsignedByte(buffer));
111         builder.setLocalPortLow(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
112         builder.setLocalPortHigh(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
113         builder.setRemotePortLow(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
114         builder.setRemotePortHigh(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
115         SimpleAddress address = SimpleAddressSerializer.getInstance().deserialize(buffer, ctx);
116         builder.setAddress(address);
117         return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
118                 .lisp.address.address.ApplicationDataBuilder().setApplicationData(builder.build()).build();
119     }
120
121     private interface Length {
122         int LOCAL_PORT_LOW = 2;
123         int LOCAL_PORT_HIGH = 2;
124         int REMOTE_PORT_LOW = 2;
125         int REMOTE_PORT_HIGH = 2;
126         int TOC = 3;
127         int PROTOCOL = 1;
128         int ALL_FIELDS = LOCAL_PORT_LOW + LOCAL_PORT_HIGH + REMOTE_PORT_LOW + REMOTE_PORT_HIGH + TOC + PROTOCOL;
129     }
130
131 }