Prepare the YANG models for RESTCONF
[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.getLocalPort() != null) {
48             buffer.putShort(NumberUtil.asShort(applicationDataAddress.getLocalPort().getValue().shortValue()));
49         } else {
50             buffer.putShort((short) 0);
51         }
52         if (applicationDataAddress.getRemotePort() != null) {
53             buffer.putShort(NumberUtil.asShort(applicationDataAddress.getRemotePort().getValue().shortValue()));
54         } else {
55             buffer.putShort((short) 0);
56         }
57         LispAddressSerializer.getInstance().serialize(buffer,
58                 LispAFIConvertor.toAFIfromPrimitive(((LcafApplicationDataAddress) lispAddress).getAddress().getPrimitiveAddress()));
59     }
60
61     @Override
62     protected LcafApplicationDataAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
63
64         LcafApplicationDataAddrBuilder builder = new LcafApplicationDataAddrBuilder();
65         byte[] rawIPTos = new byte[3];
66         buffer.get(rawIPTos);
67         builder.setIpTos(ByteUtil.getPartialInt(rawIPTos));
68         builder.setProtocol((short) ByteUtil.getUnsignedByte(buffer));
69         builder.setLocalPort(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
70         builder.setRemotePort(new PortNumber(ByteUtil.asUnsignedShort(buffer.getShort())));
71         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(buffer);
72         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
73                 .setAddress(new AddressBuilder().setPrimitiveAddress((PrimitiveAddress) LispAFIConvertor.toPrimitive(address)).build());
74
75         return builder.build();
76     }
77
78     private interface Length {
79         int LOCAL_PORT = 2;
80         int REMOTE_PORT = 2;
81         int TOC = 3;
82         int PROTOCOL = 1;
83         int ALL_FIELDS = LOCAL_PORT + REMOTE_PORT + TOC + PROTOCOL;
84     }
85
86 }