Fix errors in serializers and deserializers
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / TunnelIpv4SourceEntrySerializer.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.TunnelIpv4Match;
19
20 public class TunnelIpv4SourceEntrySerializer extends AbstractMatchEntrySerializer {
21
22     @Override
23     public void serialize(Match match, ByteBuf outBuffer) {
24         super.serialize(match, outBuffer);
25         // TODO: Why is this serialization exactly same as in Ipv4SourceEntrySerializer?
26         writeIpv4Prefix(TunnelIpv4Match.class.cast(match.getLayer3Match()).getTunnelIpv4Source(), outBuffer);
27     }
28
29     @Override
30     public boolean matchTypeCheck(Match match) {
31         return Objects.nonNull(match.getLayer3Match()) &&
32                 TunnelIpv4Match.class.isInstance(match.getLayer3Match()) &&
33                 Objects.nonNull(TunnelIpv4Match.class.cast(match.getLayer3Match()).getTunnelIpv4Source());
34     }
35
36     @Override
37     protected boolean getHasMask(Match match) {
38         // Split address to IP and mask
39         final Iterator<String> addressParts = IpConversionUtil.splitToParts(
40                 TunnelIpv4Match.class.cast(match.getLayer3Match()).getTunnelIpv4Source());
41         addressParts.next();
42
43         // Check if we have mask
44         return addressParts.hasNext() && Integer.parseInt(addressParts.next()) < 32;
45     }
46
47     @Override
48     protected int getOxmFieldCode() {
49         return OxmMatchConstants.IPV4_SRC;
50     }
51
52     @Override
53     protected int getOxmClassCode() {
54         return OxmMatchConstants.OPENFLOW_BASIC_CLASS;
55     }
56
57     @Override
58     protected int getValueLength() {
59         return EncodeConstants.SIZE_OF_INT_IN_BYTES;
60     }
61
62 }