2 * Copyright (c) 2016 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.impl.protocol.serialization.match;
11 import io.netty.buffer.ByteBuf;
12 import java.util.Iterator;
13 import java.util.Optional;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderSerializer;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.openflowplugin.api.openflow.protocol.serialization.MatchEntrySerializer;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IpConversionUtil;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
28 public abstract class AbstractMatchEntrySerializer implements HeaderSerializer<Match>, MatchEntrySerializer {
31 public void serialize(Match match, ByteBuf outBuffer) {
32 serializeHeader(match, outBuffer);
36 public void serializeHeader(Match match, ByteBuf outBuffer) {
37 outBuffer.writeShort(getOxmClassCode());
39 int fieldAndMask = getOxmFieldCode() << 1;
40 int length = getValueLength();
42 if (getHasMask(match)) {
47 outBuffer.writeByte(fieldAndMask);
48 outBuffer.writeByte(length);
52 * Serialize byte mask to bytes. checking for mask length
53 * @param mask byte mask
54 * @param outBuffer output buffer
55 * @param length mask length
57 protected static void writeMask(byte[] mask, ByteBuf outBuffer, int length) {
58 if (mask != null && mask.length != length) {
59 throw new IllegalArgumentException("incorrect length of mask: "+
60 mask.length + ", expected: " + length);
63 outBuffer.writeBytes(mask);
67 * Serialize Ipv4 address to bytes
68 * @param address Ipv4 address
69 * @param outBuffer output buffer
71 protected static void writeIpv4Address(final Ipv4Address address, final ByteBuf outBuffer) {
72 outBuffer.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressBytes(address));
76 * Serialize Ipv6 address to bytes
77 * @param address Ipv6 address
78 * @param outBuffer output buffer
80 protected static void writeIpv6Address(final Ipv6Address address, final ByteBuf outBuffer) {
81 outBuffer.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressBytes(address));
85 * Serialize Mac address to bytes
86 * @param address Mac address
87 * @param outBuffer output buffer
89 protected static void writeMacAddress(final MacAddress address, final ByteBuf outBuffer) {
90 outBuffer.writeBytes(IetfYangUtil.INSTANCE.bytesFor(address)); // 48 b + mask [OF 1.3.2 spec]
94 * Serialize Ipv4 prefix (address and mask)
95 * @param prefix Ipv4 prefix
96 * @param outBuffer output buffer
98 protected static void writeIpv4Prefix(final Ipv4Prefix prefix, final ByteBuf outBuffer) {
99 // Split address to IP and mask
100 final Iterator<String> addressParts = IpConversionUtil.splitToParts(prefix);
102 // Write address part of prefix
103 writeIpv4Address(new Ipv4Address(addressParts.next()), outBuffer);
105 // If prefix had mask, also write prefix
106 Optional.ofNullable(MatchConvertorUtil.extractIpv4Mask(addressParts)).ifPresent(mask ->
107 writeMask(mask, outBuffer, EncodeConstants.GROUPS_IN_IPV4_ADDRESS));
111 * Serialize Ipv6 prefix (address and mask)
112 * @param prefix Ipv6 prefix
113 * @param outBuffer output buffer
115 protected static void writeIpv6Prefix(final Ipv6Prefix prefix, final ByteBuf outBuffer) {
116 // Write address part of prefix
117 writeIpv6Address(IpConversionUtil.extractIpv6Address(prefix), outBuffer);
119 // If prefix had mask, also write prefix
120 Optional.ofNullable(IpConversionUtil.extractIpv6Prefix(prefix)).ifPresent(mask ->
121 writeMask(IpConversionUtil.convertIpv6PrefixToByteArray(mask), outBuffer,
122 EncodeConstants.SIZE_OF_IPV6_ADDRESS_IN_BYTES));
126 * @param match Openflow match
127 * @return if field has or has not mask
129 protected abstract boolean getHasMask(final Match match);
132 * @return numeric representation of oxm_field
134 protected abstract int getOxmFieldCode();
137 * @return numeric representation of oxm_class
139 protected abstract int getOxmClassCode();
142 * @return match entry value length (without mask length)
144 protected abstract int getValueLength();