Add Action serializers
[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
25 public class SetTpDstActionSerializer extends AbstractSetFieldActionSerializer {
26
27     @Override
28     protected SetFieldCase buildAction(Action input) {
29         final SetTpDstAction setTpDstAction = SetTpDstActionCase.class.cast(input).getSetTpDstAction();
30         final PortNumber port = setTpDstAction.getPort();
31         final SetFieldBuilder builder = new SetFieldBuilder();
32
33         Optional.ofNullable(IPProtocols.fromProtocolNum(setTpDstAction.getIpProtocol())).ifPresent(proto -> {
34             switch (proto) {
35                 case ICMP: {
36                     builder.setIcmpv4Match(new Icmpv4MatchBuilder()
37                             .setIcmpv4Code((short) (0xFF & port.getValue()))
38                             .build());
39                     break;
40                 }
41                 case ICMPV6: {
42                     builder.setIcmpv6Match(new Icmpv6MatchBuilder()
43                             .setIcmpv6Code((short) (0xFF & port.getValue()))
44                             .build());
45                     break;
46                 }
47                 case TCP: {
48                     builder.setLayer4Match(new TcpMatchBuilder()
49                             .setTcpDestinationPort(port)
50                             .build());
51                     break;
52                 }
53                 case UDP: {
54                     builder.setLayer4Match(new UdpMatchBuilder()
55                             .setUdpDestinationPort(port)
56                             .build());
57                     break;
58                 }
59             }
60         });
61
62         return new SetFieldCaseBuilder().setSetField(builder.build()).build();
63     }
64
65 }