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.lisp.serializer.address;
10 import static org.opendaylight.yangtools.yang.common.UintConversions.fromJava;
12 import java.nio.ByteBuffer;
14 import org.opendaylight.lispflowmapping.lisp.type.LispCanonicalAddressFormatEnum;
15 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
16 import org.opendaylight.lispflowmapping.lisp.util.NumberUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ApplicationDataLcaf;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.application.data.ApplicationData;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.application.data.ApplicationDataBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
30 public final class ApplicationDataSerializer extends LcafSerializer {
32 private static final ApplicationDataSerializer INSTANCE = new ApplicationDataSerializer();
34 // Private constructor prevents instantiation from other classes
35 private ApplicationDataSerializer() {
38 public static ApplicationDataSerializer getInstance() {
43 protected byte getLcafType() {
44 return LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode();
48 protected short getLcafLength(LispAddress lispAddress) {
49 ApplicationData appData = ((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types
50 .rev151105.lisp.address.address.ApplicationData) lispAddress.getAddress()).getApplicationData();
51 return (short) (Length.ALL_FIELDS + SimpleAddressSerializer.getInstance().getAddressSize(appData.getAddress()));
55 protected short getAfi() {
56 return (short) AddressFamily.LispCanonicalAddressFormat.getIntValue();
60 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
61 ApplicationData appData = ((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types
62 .rev151105.lisp.address.address.ApplicationData) lispAddress.getAddress()).getApplicationData();
63 buffer.put(ByteUtil.partialIntToByteArray(NumberUtil.asInt(appData.getIpTos()), Length.TOC));
64 buffer.put((byte) NumberUtil.asShort(appData.getProtocol().toJava()));
65 if (appData.getLocalPortLow() != null) {
66 buffer.putShort(NumberUtil.asShort(appData.getLocalPortLow().getValue().shortValue()));
68 buffer.putShort((short) 0);
70 if (appData.getLocalPortHigh() != null) {
71 buffer.putShort(NumberUtil.asShort(appData.getLocalPortHigh().getValue().shortValue()));
73 buffer.putShort((short) 0);
75 if (appData.getRemotePortLow() != null) {
76 buffer.putShort(NumberUtil.asShort(appData.getRemotePortLow().getValue().shortValue()));
78 buffer.putShort((short) 0);
80 if (appData.getRemotePortHigh() != null) {
81 buffer.putShort(NumberUtil.asShort(appData.getRemotePortHigh().getValue().shortValue()));
83 buffer.putShort((short) 0);
85 SimpleAddressSerializer.getInstance().serialize(buffer, appData.getAddress());
89 protected Eid deserializeLcafEidData(ByteBuffer buffer, byte res2, short length, LispAddressSerializerContext ctx) {
90 EidBuilder eb = new EidBuilder();
91 eb.setAddressType(ApplicationDataLcaf.class);
92 eb.setVirtualNetworkId(getVni(ctx));
93 eb.setAddress(deserializeData(buffer, ctx));
98 protected Rloc deserializeLcafRlocData(ByteBuffer buffer, byte res2, short length,
99 LispAddressSerializerContext ctx) {
100 RlocBuilder eb = new RlocBuilder();
101 eb.setAddressType(ApplicationDataLcaf.class);
102 eb.setVirtualNetworkId(null);
103 eb.setAddress(deserializeData(buffer, ctx));
107 private Address deserializeData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
108 ApplicationDataBuilder builder = new ApplicationDataBuilder();
109 byte[] rawIPTos = new byte[3];
110 buffer.get(rawIPTos);
111 builder.setIpTos(ByteUtil.getPartialInt(rawIPTos));
112 builder.setProtocol(fromJava((short) ByteUtil.getUnsignedByte(buffer)));
113 builder.setLocalPortLow(new PortNumber(fromJava(ByteUtil.asUnsignedShort(buffer.getShort()))));
114 builder.setLocalPortHigh(new PortNumber(fromJava(ByteUtil.asUnsignedShort(buffer.getShort()))));
115 builder.setRemotePortLow(new PortNumber(fromJava(ByteUtil.asUnsignedShort(buffer.getShort()))));
116 builder.setRemotePortHigh(new PortNumber(fromJava(ByteUtil.asUnsignedShort(buffer.getShort()))));
117 SimpleAddress address = SimpleAddressSerializer.getInstance().deserialize(buffer, ctx);
118 builder.setAddress(address);
119 return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
120 .lisp.address.address.ApplicationDataBuilder().setApplicationData(builder.build()).build();
123 private interface Length {
124 int LOCAL_PORT_LOW = 2;
125 int LOCAL_PORT_HIGH = 2;
126 int REMOTE_PORT_LOW = 2;
127 int REMOTE_PORT_HIGH = 2;
130 int ALL_FIELDS = LOCAL_PORT_LOW + LOCAL_PORT_HIGH + REMOTE_PORT_LOW + REMOTE_PORT_HIGH + TOC + PROTOCOL;