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.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;
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 LispAFIConvertor.toAFIfromPrimitive(((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.getLocalPortLow() != null) {
48 buffer.putShort(NumberUtil.asShort(applicationDataAddress.getLocalPortLow().getValue().shortValue()));
50 buffer.putShort((short) 0);
52 if (applicationDataAddress.getLocalPortHigh() != null) {
53 buffer.putShort(NumberUtil.asShort(applicationDataAddress.getLocalPortHigh().getValue().shortValue()));
55 buffer.putShort((short) 0);
57 if (applicationDataAddress.getRemotePortLow() != null) {
58 buffer.putShort(NumberUtil.asShort(applicationDataAddress.getRemotePortLow().getValue().shortValue()));
60 buffer.putShort((short) 0);
62 if (applicationDataAddress.getRemotePortHigh() != null) {
63 buffer.putShort(NumberUtil.asShort(applicationDataAddress.getRemotePortHigh().getValue().shortValue()));
65 buffer.putShort((short) 0);
67 LispAddressSerializer.getInstance().serialize(buffer,
68 LispAFIConvertor.toAFIfromPrimitive(((LcafApplicationDataAddress) lispAddress).getAddress().getPrimitiveAddress()));
72 protected LcafApplicationDataAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
74 LcafApplicationDataAddrBuilder builder = new LcafApplicationDataAddrBuilder();
75 byte[] rawIPTos = new byte[3];
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());
87 return builder.build();
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;
97 int ALL_FIELDS = LOCAL_PORT_LOW + LOCAL_PORT_HIGH + REMOTE_PORT_LOW + REMOTE_PORT_HIGH + TOC + PROTOCOL;