Remove Objects.{is,non}Null abuse
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / match / Ipv4DestinationEntryDeserializer.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 package org.opendaylight.openflowplugin.impl.protocol.deserialization.match;
9
10 import io.netty.buffer.ByteBuf;
11 import javax.annotation.Nullable;
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.openflow.md.core.sal.convertor.common.IpConversionUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
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 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchArbitraryBitMaskBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
22
23 public class Ipv4DestinationEntryDeserializer extends AbstractMatchEntryDeserializer {
24
25     @Override
26     public void deserializeEntry(ByteBuf message, MatchBuilder builder) {
27         final boolean hasMask = processHeader(message);
28         final Ipv4Address address = ByteBufUtils.readIetfIpv4Address(message);
29
30         if (hasMask) {
31             final byte[] mask = OxmDeserializerHelper.convertMask(message, EncodeConstants.GROUPS_IN_IPV4_ADDRESS);
32
33             if (IpConversionUtil.isArbitraryBitMask(mask)) {
34                 setArbitraryMatch(builder, address, mask);
35             } else {
36                 setPrefixMatch(builder, address, mask);
37             }
38         } else {
39             setPrefixMatch(builder, address, null);
40         }
41     }
42
43     private static void setPrefixMatch(final MatchBuilder builder, final Ipv4Address address, @Nullable final byte[]
44             mask) {
45         if (builder.getLayer3Match() == null) {
46             builder.setLayer3Match(new Ipv4MatchBuilder()
47                     .setIpv4Destination(IpConversionUtil.createPrefix(address, mask))
48                     .build());
49         } else if (builder.getLayer3Match() instanceof Ipv4Match
50                 && ((Ipv4Match) builder.getLayer3Match()).getIpv4Destination() == null) {
51             builder.setLayer3Match(new Ipv4MatchBuilder((Ipv4Match) builder.getLayer3Match())
52                     .setIpv4Destination(IpConversionUtil.createPrefix(address, mask))
53                     .build());
54         } else {
55             throwErrorOnMalformed(builder, "layer3Match", "ipv4Destination");
56         }
57     }
58
59     private static void setArbitraryMatch(final MatchBuilder builder, final Ipv4Address address,
60                                           final byte[] mask) {
61         if (builder.getLayer3Match() == null) {
62             builder.setLayer3Match(new Ipv4MatchArbitraryBitMaskBuilder()
63                     .setIpv4DestinationAddressNoMask(address)
64                     .setIpv4DestinationArbitraryBitmask(IpConversionUtil.createArbitraryBitMask(mask))
65                     .build());
66         } else if (builder.getLayer3Match() instanceof Ipv4MatchArbitraryBitMask
67                 && ((Ipv4MatchArbitraryBitMask) builder.getLayer3Match()).getIpv4DestinationAddressNoMask() == null) {
68             builder.setLayer3Match(new Ipv4MatchArbitraryBitMaskBuilder((Ipv4MatchArbitraryBitMask) builder
69                 .getLayer3Match())
70                     .setIpv4DestinationAddressNoMask(address)
71                     .setIpv4DestinationArbitraryBitmask(IpConversionUtil.createArbitraryBitMask(mask))
72                     .build());
73         } else {
74             throwErrorOnMalformed(builder, "layer3Match", "ipv4DestinationAddressNoMask");
75         }
76     }
77 }