Remove EncodeConstants.SIZE_OF_{BYTE,SHORT,INT,LONG}_IN_BYTES
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / match / AbstractMatchEntryDeserializer.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.deserialization.match;
10
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
13 import org.opendaylight.openflowjava.protocol.impl.deserialization.match.OxmDeserializerHelper;
14 import org.opendaylight.openflowjava.util.ByteBufUtils;
15 import org.opendaylight.openflowplugin.api.openflow.protocol.deserialization.MatchEntryDeserializer;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IpConversionUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20
21 public abstract class AbstractMatchEntryDeserializer implements MatchEntryDeserializer {
22
23     /**
24      * Processes match entry header and returns if it have mask, or not.
25      * @param in input buffer
26      * @return true if match entry has mask, false otherwise
27      */
28     protected static boolean processHeader(ByteBuf in) {
29         in.skipBytes(Short.BYTES); // skip oxm_class
30         boolean hasMask = (in.readUnsignedByte() & 1) != 0;
31         in.skipBytes(Byte.BYTES); // skip match entry length
32         return hasMask;
33     }
34
35     /**
36      * Read Ipv4Prefix from message.
37      * @param message buffered message
38      * @param hasMask determines if prefix has mask or not
39      * @return IPv4 prefix
40      */
41     protected static Ipv4Prefix readPrefix(ByteBuf message, boolean hasMask) {
42         final Ipv4Address address = ByteBufUtils.readIetfIpv4Address(message);
43         int mask = 32;
44
45         if (hasMask) {
46             mask = IpConversionUtil.countBits(
47                     OxmDeserializerHelper.convertMask(message, EncodeConstants.GROUPS_IN_IPV4_ADDRESS));
48         }
49
50         return IpConversionUtil.createPrefix(address, mask);
51     }
52
53     /**
54      * Throw error on malformed match builder input.
55      * @param builder match builder
56      * @param propertyName name of property that already containsData
57      */
58     protected static void throwErrorOnMalformed(MatchBuilder builder, String propertyName) {
59         throw new IllegalArgumentException("Match: " + builder.toString() + " is malformed, "
60                 + builder + "#" + propertyName + " contains invalid data.");
61     }
62
63     /**
64      * Throw error on malformed match builder input.
65      * @param builder match builder
66      * @param propertyName name of property that already containsData
67      */
68     protected static void throwErrorOnMalformed(MatchBuilder builder, String propertyName, String fieldName) {
69         throw new IllegalArgumentException("Match: " + builder.toString() + " is malformed, "
70             + builder + "#" + propertyName + "#" + fieldName + " contains invalid data.");
71     }
72
73 }