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.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;
21 public class Ipv4SourceEntrySerializer extends AbstractMatchEntrySerializer {
24 public void serialize(Match match, ByteBuf outBuffer) {
25 super.serialize(match, outBuffer);
27 if (isPrefix(match)) {
28 writeIpv4Prefix(((Ipv4Match) match.getLayer3Match()).getIpv4Source(), outBuffer);
29 } else if (isArbitrary(match)) {
30 final Ipv4MatchArbitraryBitMask ipv4 = (Ipv4MatchArbitraryBitMask) match.getLayer3Match();
31 writeIpv4Address(ipv4.getIpv4SourceAddressNoMask(), outBuffer);
33 if (getHasMask(match)) {
34 writeMask(IpConversionUtil.convertArbitraryMaskToByteArray(ipv4.getIpv4SourceArbitraryBitmask()),
42 public boolean matchTypeCheck(Match match) {
43 if (isPrefix(match)) {
44 return Objects.nonNull(match.getLayer3Match())
45 && Objects.nonNull(((Ipv4Match) match.getLayer3Match()).getIpv4Source());
46 } else if (isArbitrary(match)) {
47 return Objects.nonNull(match.getLayer3Match())
48 && Objects.nonNull(((Ipv4MatchArbitraryBitMask) match.getLayer3Match())
49 .getIpv4SourceAddressNoMask());
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) match.getLayer3Match()).getIpv4Source());
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) match.getLayer3Match())
67 .getIpv4SourceArbitraryBitmask());
73 private static boolean isPrefix(Match match) {
74 return match.getLayer3Match() instanceof Ipv4Match;
77 private static boolean isArbitrary(Match match) {
78 return match.getLayer3Match() instanceof Ipv4MatchArbitraryBitMask;
82 protected int getOxmFieldCode() {
83 return OxmMatchConstants.IPV4_SRC;
87 protected int getOxmClassCode() {
88 return OxmMatchConstants.OPENFLOW_BASIC_CLASS;
92 protected int getValueLength() {
93 return EncodeConstants.SIZE_OF_INT_IN_BYTES;