Clean up instance checks and casts
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / match / UdpSourcePortEntryDeserializer.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.deserialization.match;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.Objects;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
17
18 public class UdpSourcePortEntryDeserializer extends AbstractMatchEntryDeserializer {
19
20     @Override
21     public void deserializeEntry(ByteBuf message, MatchBuilder builder) {
22         processHeader(message);
23         final int port = message.readUnsignedShort();
24
25         if (Objects.isNull(builder.getLayer4Match())) {
26             builder.setLayer4Match(new UdpMatchBuilder()
27                     .setUdpSourcePort(new PortNumber(port))
28                     .build());
29         } else if (builder.getLayer4Match() instanceof UdpMatch
30             && Objects.isNull(((UdpMatch) builder.getLayer4Match()).getUdpSourcePort())) {
31             builder.setLayer4Match(new UdpMatchBuilder((UdpMatch) builder.getLayer4Match())
32                     .setUdpSourcePort(new PortNumber(port))
33                     .build());
34         } else {
35             throwErrorOnMalformed(builder, "layer4Match", "udpSourcePort");
36         }
37     }
38
39 }