a7f815d3d2de03df54f772dae54f53d3886d1a7f
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispApplicationDataLCAFAddressSerializer.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.implementation.serializer.address;
9
10 import java.nio.ByteBuffer;
11
12 import org.opendaylight.lispflowmapping.implementation.util.ByteUtil;
13 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
14 import org.opendaylight.lispflowmapping.implementation.util.NumberUtil;
15 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
16 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LcafApplicationDataAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcafapplicationdataaddress.AddressBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcafapplicationdata.LcafApplicationDataAddrBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispsimpleaddress.PrimitiveAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
23
24 public class LispApplicationDataLCAFAddressSerializer extends LispLCAFAddressSerializer {
25
26     private static final LispApplicationDataLCAFAddressSerializer INSTANCE = new LispApplicationDataLCAFAddressSerializer();
27
28     // Private constructor prevents instantiation from other classes
29     private LispApplicationDataLCAFAddressSerializer() {
30     }
31
32     public static LispApplicationDataLCAFAddressSerializer getInstance() {
33         return INSTANCE;
34     }
35
36     @Override
37     protected short getLcafLength(LispAFIAddress lispAddress) {
38         return (short) (Length.ALL_FIELDS + LispAddressSerializer.getInstance().getAddressSize(
39                 LispAFIConvertor.toAFIfromPrimitive(((LcafApplicationDataAddress) lispAddress).getAddress().getPrimitiveAddress())));
40     }
41
42     @Override
43     protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
44         LcafApplicationDataAddress applicationDataAddress = ((LcafApplicationDataAddress) lispAddress);
45         buffer.put(ByteUtil.partialIntToByteArray(NumberUtil.asInt(applicationDataAddress.getIpTos()), Length.TOC));
46         buffer.put((byte) NumberUtil.asShort(applicationDataAddress.getProtocol()));
47         if (applicationDataAddress.getLocalPortLow() != null) {
48             buffer.putShort(NumberUtil.asShort(applicationDataAddress.getLocalPortLow().getValue().shortValue()));
49         } else {
50             buffer.putShort((short) 0);
51         }
52         if (applicationDataAddress.getLocalPortHigh() != null) {
53             buffer.putShort(NumberUtil.asShort(applicationDataAddress.getLocalPortHigh().getValue().shortValue()));
54         } else {
55             buffer.putShort((short) 0);
56         }
57         if (applicationDataAddress.getRemotePortLow() != null) {
58             buffer.putShort(NumberUtil.asShort(applicationDataAddress.getRemotePortLow().getValue().shortValue()));
59         } else {
60             buffer.putShort((short) 0);
61         }
62         if (applicationDataAddress.getRemotePortHigh() != null) {
63             buffer.putShort(NumberUtil.asShort(applicationDataAddress.getRemotePortHigh().getValue().shortValue()));
64         } else {
65             buffer.putShort((short) 0);
66         }
67         LispAddressSerializer.getInstance().serialize(buffer,
68                 LispAFIConvertor.toAFIfromPrimitive(((LcafApplicationDataAddress) lispAddress).getAddress().getPrimitiveAddress()));
69     }
70
71     @Override
72     protected LcafApplicationDataAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
73
74         LcafApplicationDataAddrBuilder builder = new LcafApplicationDataAddrBuilder();
75         byte[] rawIPTos = new byte[3];
76         buffer.get(rawIPTos);
77         builder.setIpTos(ByteUtil.getPartialInt(rawIPTos));
78         builder.setProtocol((short) ByteUtil.getUnsignedByte(buffer));
79         builder.setLocalPortLow(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
80         builder.setLocalPortHigh(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
81         builder.setRemotePortLow(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
82         builder.setRemotePortHigh(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
83         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(buffer);
84         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
85                 .setAddress(new AddressBuilder().setPrimitiveAddress((PrimitiveAddress) LispAFIConvertor.toPrimitive(address)).build());
86
87         return builder.build();
88     }
89
90     private interface Length {
91         int LOCAL_PORT_LOW = 2;
92         int LOCAL_PORT_HIGH = 2;
93         int REMOTE_PORT_LOW = 2;
94         int REMOTE_PORT_HIGH = 2;
95         int TOC = 3;
96         int PROTOCOL = 1;
97         int ALL_FIELDS = LOCAL_PORT_LOW + LOCAL_PORT_HIGH + REMOTE_PORT_LOW + REMOTE_PORT_HIGH + TOC + PROTOCOL;
98     }
99
100 }