Merge "Fix checkstyle warnings for impl/device package"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / actions / SetTpDstActionSerializer.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.actions;
10
11 import java.util.Optional;
12 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IPProtocols;
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.action.types.rev131112.action.Action;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCase;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.tp.dst.action._case.SetTpDstAction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class SetTpDstActionSerializer extends AbstractSetFieldActionSerializer {
28     private static final Logger LOG = LoggerFactory.getLogger(SetTpDstActionSerializer.class);
29
30     @Override
31     protected SetFieldCase buildAction(Action input) {
32         final SetTpDstAction setTpDstAction = SetTpDstActionCase.class.cast(input).getSetTpDstAction();
33         final PortNumber port = setTpDstAction.getPort();
34         final SetFieldBuilder builder = new SetFieldBuilder();
35
36         Optional.ofNullable(IPProtocols.fromProtocolNum(setTpDstAction.getIpProtocol())).ifPresent(proto -> {
37             switch (proto) {
38                 case ICMP: {
39                     builder.setIcmpv4Match(new Icmpv4MatchBuilder()
40                             .setIcmpv4Code((short) (0xFF & port.getValue()))
41                             .build());
42                     break;
43                 }
44                 case ICMPV6: {
45                     builder.setIcmpv6Match(new Icmpv6MatchBuilder()
46                             .setIcmpv6Code((short) (0xFF & port.getValue()))
47                             .build());
48                     break;
49                 }
50                 case TCP: {
51                     builder.setLayer4Match(new TcpMatchBuilder()
52                             .setTcpDestinationPort(port)
53                             .build());
54                     break;
55                 }
56                 case UDP: {
57                     builder.setLayer4Match(new UdpMatchBuilder()
58                             .setUdpDestinationPort(port)
59                             .build());
60                     break;
61                 }
62                 default:
63                     // no operation
64             }
65         });
66
67         return new SetFieldCaseBuilder().setSetField(builder.build()).build();
68     }
69
70 }