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 Ipv4DestinationEntrySerializer extends AbstractMatchEntrySerializer {
24 public void serialize(Match match, ByteBuf outBuffer) {
25 super.serialize(match, outBuffer);
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);
33 if (getHasMask(match)) {
34 writeMask(IpConversionUtil.convertArbitraryMaskToByteArray(ipv4.getIpv4DestinationArbitraryBitmask()),
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());
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());
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());
73 private static boolean isPrefix(Match match) {
74 return Ipv4Match.class.isInstance(match.getLayer3Match());
77 private static boolean isArbitrary(Match match) {
78 return Ipv4MatchArbitraryBitMask.class.isInstance(match.getLayer3Match());
82 protected int getOxmFieldCode() {
83 return OxmMatchConstants.IPV4_DST;
87 protected int getOxmClassCode() {
88 return OxmMatchConstants.OPENFLOW_BASIC_CLASS;
92 protected int getValueLength() {
93 return EncodeConstants.SIZE_OF_INT_IN_BYTES;