Rework bit-copying functions and re-enable tests
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / Ipv4DestinationEntrySerializer.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.Objects;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IpConversionUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchArbitraryBitMask;
20
21 public class Ipv4DestinationEntrySerializer extends AbstractMatchEntrySerializer {
22
23     @Override
24     public void serialize(Match match, ByteBuf outBuffer) {
25         super.serialize(match, outBuffer);
26
27         if (isPrefix(match)) {
28             writeIpv4Prefix(Ipv4Match.class.cast(match.getLayer3Match()).getIpv4Destination(), outBuffer);
29         } else if (isArbitrary(match)) {
30             final Ipv4MatchArbitraryBitMask ipv4 = Ipv4MatchArbitraryBitMask.class.cast(match.getLayer3Match());
31             writeIpv4Address(ipv4.getIpv4DestinationAddressNoMask(), outBuffer);
32
33             if (getHasMask(match)) {
34                 writeMask(IpConversionUtil.convertArbitraryMaskToByteArray(ipv4.getIpv4DestinationArbitraryBitmask()),
35                         outBuffer,
36                         getValueLength());
37             }
38         }
39     }
40
41     @Override
42     public boolean matchTypeCheck(Match match) {
43         if (isPrefix(match)) {
44             return Objects.nonNull(match.getLayer3Match())
45                     && Objects.nonNull(Ipv4Match.class.cast(match.getLayer3Match()).getIpv4Destination());
46         } else if (isArbitrary(match)) {
47             return Objects.nonNull(match.getLayer3Match())
48                     && Objects.nonNull(Ipv4MatchArbitraryBitMask.class.cast(match.getLayer3Match())
49                     .getIpv4DestinationAddressNoMask());
50         }
51
52         return false;
53     }
54
55     @Override
56     protected boolean getHasMask(Match match) {
57         if (isPrefix(match)) {
58             // Split address to IP and mask
59             final Iterator<String> addressParts = IpConversionUtil.splitToParts(
60                     Ipv4Match.class.cast(match.getLayer3Match()).getIpv4Destination());
61             addressParts.next();
62
63             // Check if we have mask
64             return addressParts.hasNext() && Integer.parseInt(addressParts.next()) < 32;
65         } else if (isArbitrary(match)) {
66             return Objects.nonNull(Ipv4MatchArbitraryBitMask.class.cast(match.getLayer3Match())
67                     .getIpv4DestinationArbitraryBitmask());
68         }
69
70         return false;
71     }
72
73     private static boolean isPrefix(Match match) {
74         return Ipv4Match.class.isInstance(match.getLayer3Match());
75     }
76
77     private static boolean isArbitrary(Match match) {
78         return Ipv4MatchArbitraryBitMask.class.isInstance(match.getLayer3Match());
79     }
80
81     @Override
82     protected int getOxmFieldCode() {
83         return OxmMatchConstants.IPV4_DST;
84     }
85
86     @Override
87     protected int getOxmClassCode() {
88         return OxmMatchConstants.OPENFLOW_BASIC_CLASS;
89     }
90
91     @Override
92     protected int getValueLength() {
93         return EncodeConstants.SIZE_OF_INT_IN_BYTES;
94     }
95
96 }