2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.implementation.serializer.address;
10 import java.nio.ByteBuffer;
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.lispflowmapping.rev131031.LcafApplicationDataAddress;
18 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
19 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder;
20 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafApplicationDataBuilder;
21 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.PrimitiveAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
24 public class LispApplicationDataLCAFAddressSerializer extends LispLCAFAddressSerializer {
26 private static final LispApplicationDataLCAFAddressSerializer INSTANCE = new LispApplicationDataLCAFAddressSerializer();
28 // Private constructor prevents instantiation from other classes
29 private LispApplicationDataLCAFAddressSerializer() {
32 public static LispApplicationDataLCAFAddressSerializer getInstance() {
37 protected short getLcafLength(LispAFIAddress lispAddress) {
38 return (short) (Length.ALL_FIELDS + LispAddressSerializer.getInstance().getAddressSize(
39 (LispAFIAddress) ((LcafApplicationDataAddress) lispAddress).getAddress().getPrimitiveAddress()));
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()));
50 buffer.putShort((short) 0);
52 if (applicationDataAddress.getRemotePort() != null) {
53 buffer.putShort(NumberUtil.asShort(applicationDataAddress.getRemotePort().getValue().shortValue()));
55 buffer.putShort((short) 0);
57 LispAddressSerializer.getInstance().serialize(buffer,
58 (LispAFIAddress) ((LcafApplicationDataAddress) lispAddress).getAddress().getPrimitiveAddress());
62 protected LcafApplicationDataAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
64 LcafApplicationDataBuilder builder = new LcafApplicationDataBuilder();
65 byte[] rawIPTos = new byte[3];
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());
75 return builder.build();
78 private interface Length {
83 int ALL_FIELDS = LOCAL_PORT + REMOTE_PORT + TOC + PROTOCOL;