Merge "Bug 8535: Fix IPv6 OXMHeader Mask issue"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / AbstractMatchEntrySerializer.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.impl.protocol.serialization.match;
10
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;
27
28 public abstract class AbstractMatchEntrySerializer implements HeaderSerializer<Match>, MatchEntrySerializer {
29
30     @Override
31     public void serialize(Match match, ByteBuf outBuffer) {
32         serializeHeader(match, outBuffer);
33     }
34
35     @Override
36     public void serializeHeader(Match match, ByteBuf outBuffer) {
37         outBuffer.writeShort(getOxmClassCode());
38
39         int fieldAndMask = getOxmFieldCode() << 1;
40         int length = getValueLength();
41
42         if (getHasMask(match)) {
43             fieldAndMask |= 1;
44             length *= 2;
45         }
46
47         outBuffer.writeByte(fieldAndMask);
48         outBuffer.writeByte(length);
49     }
50
51     /**
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
56      */
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);
61         }
62
63         outBuffer.writeBytes(mask);
64     }
65
66     /**
67      * Serialize Ipv4 address to bytes
68      * @param address Ipv4 address
69      * @param outBuffer output buffer
70      */
71     protected static void writeIpv4Address(final Ipv4Address address, final ByteBuf outBuffer) {
72         outBuffer.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressBytes(address));
73     }
74
75     /**
76      * Serialize Ipv6 address to bytes
77      * @param address Ipv6 address
78      * @param outBuffer output buffer
79      */
80     protected static void writeIpv6Address(final Ipv6Address address, final ByteBuf outBuffer) {
81         outBuffer.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressBytes(address));
82     }
83
84     /**
85      * Serialize Mac address to bytes
86      * @param address Mac address
87      * @param outBuffer output buffer
88      */
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]
91     }
92
93     /**
94      * Serialize Ipv4 prefix (address and mask)
95      * @param prefix Ipv4 prefix
96      * @param outBuffer output buffer
97      */
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);
101
102         // Write address part of prefix
103         writeIpv4Address(new Ipv4Address(addressParts.next()), outBuffer);
104
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));
108     }
109
110     /**
111      * Serialize Ipv6 prefix (address and mask)
112      * @param prefix Ipv6 prefix
113      * @param outBuffer output buffer
114      */
115     protected static void writeIpv6Prefix(final Ipv6Prefix prefix, final ByteBuf outBuffer) {
116         // Write address part of prefix
117         writeIpv6Address(IpConversionUtil.extractIpv6Address(prefix), outBuffer);
118
119         // If prefix had mask, also write prefix
120         Optional.ofNullable(IpConversionUtil.hasIpv6Prefix(prefix)).ifPresent(mask ->
121                 writeMask(IpConversionUtil.convertIpv6PrefixToByteArray(mask), outBuffer,
122                         EncodeConstants.SIZE_OF_IPV6_ADDRESS_IN_BYTES));
123     }
124
125     /**
126      * @param match Openflow match
127      * @return if field has or has not mask
128      */
129     protected abstract boolean getHasMask(final Match match);
130
131     /**
132      * @return numeric representation of oxm_field
133      */
134     protected abstract int getOxmFieldCode();
135
136     /**
137      * @return numeric representation of oxm_class
138      */
139     protected abstract int getOxmClassCode();
140
141     /**
142      * @return match entry value length (without mask length)
143      */
144     protected abstract int getValueLength();
145 }