Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / actions / SetTpSrcActionSerializer.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.serialization.actions;
9
10 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IPProtocols;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCase;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpSrcActionCase;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.tp.src.action._case.SetTpSrcAction;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
22 import org.opendaylight.yangtools.yang.common.Uint8;
23
24 public class SetTpSrcActionSerializer extends AbstractSetFieldActionSerializer {
25     @Override
26     protected SetFieldCase buildAction(final Action input) {
27         final SetTpSrcAction setTpSrcAction = ((SetTpSrcActionCase) input).getSetTpSrcAction();
28         final PortNumber port = setTpSrcAction.getPort();
29         final SetFieldBuilder builder = new SetFieldBuilder();
30         final IPProtocols proto = IPProtocols.fromProtocolNum(setTpSrcAction.getIpProtocol());
31         if (proto != null) {
32             switch (proto) {
33                 case ICMP: {
34                     builder.setIcmpv4Match(new Icmpv4MatchBuilder()
35                             .setIcmpv4Type(Uint8.valueOf(0xFF & port.getValue().toJava()))
36                             .build());
37                     break;
38                 }
39                 case ICMPV6: {
40                     builder.setIcmpv6Match(new Icmpv6MatchBuilder()
41                             .setIcmpv6Type(Uint8.valueOf(0xFF & port.getValue().toJava()))
42                             .build());
43                     break;
44                 }
45                 case TCP: {
46                     builder.setLayer4Match(new TcpMatchBuilder()
47                             .setTcpSourcePort(port)
48                             .build());
49                     break;
50                 }
51                 case UDP: {
52                     builder.setLayer4Match(new UdpMatchBuilder()
53                             .setUdpSourcePort(port)
54                             .build());
55                     break;
56                 }
57                 default:
58                     // no operation
59             }
60         }
61
62         return new SetFieldCaseBuilder().setSetField(builder.build()).build();
63     }
64 }