Bug 5540 - ActionConvertor, ActionResponseConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / action / cases / SalToOfSetTpDstActionCase.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.openflow.md.core.sal.convertor.action.cases;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionConvertorData;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorCase;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IPProtocols;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.tp.dst.action._case.SetTpDstAction;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetFieldCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.field._case.SetFieldActionBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv4Code;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv6Code;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TcpDst;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.UdpDst;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4CodeCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv6CodeCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TcpDstCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.UdpDstCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.code._case.Icmpv4CodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv6.code._case.Icmpv6CodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tcp.dst._case.TcpDstBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.udp.dst._case.UdpDstBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class SalToOfSetTpDstActionCase extends ConvertorCase<SetTpDstActionCase, Action, ActionConvertorData> {
44     private static final Logger LOG = LoggerFactory.getLogger(SalToOfSetTpDstActionCase.class);
45
46     public SalToOfSetTpDstActionCase() {
47         super(SetTpDstActionCase.class, true, OFConstants.OFP_VERSION_1_3);
48     }
49
50     @Nonnull
51     @Override
52     public Optional<Action> process(@Nonnull final SetTpDstActionCase source, final ActionConvertorData data) {
53         IPProtocols protocol = null;
54
55         if (data.getIpProtocol() != null) {
56             protocol = IPProtocols.fromProtocolNum(data.getIpProtocol());
57         }
58
59         SetTpDstAction settpdstaction = source.getSetTpDstAction();
60         SetFieldCaseBuilder setFieldCaseBuilder = new SetFieldCaseBuilder();
61         SetFieldActionBuilder setFieldBuilder = new SetFieldActionBuilder();
62
63         MatchEntryBuilder matchBuilder = new MatchEntryBuilder();
64         matchBuilder.setOxmClass(OpenflowBasicClass.class);
65         matchBuilder.setHasMask(false);
66         int port = settpdstaction.getPort().getValue();
67         int code = 0xff & port;
68
69         if (protocol != null) {
70             switch (protocol) {
71                 case ICMP:
72                     matchBuilder.setOxmMatchField(Icmpv4Code.class);
73                     Icmpv4CodeCaseBuilder icmpv4CodeCaseBuilder = new Icmpv4CodeCaseBuilder();
74                     Icmpv4CodeBuilder icmpv4CodeBuilder = new Icmpv4CodeBuilder();
75                     icmpv4CodeBuilder.setIcmpv4Code((short) code);
76                     icmpv4CodeCaseBuilder.setIcmpv4Code(icmpv4CodeBuilder.build());
77                     matchBuilder.setMatchEntryValue(icmpv4CodeCaseBuilder.build());
78                     break;
79                 case ICMPV6:
80                     matchBuilder.setOxmMatchField(Icmpv6Code.class);
81                     Icmpv6CodeCaseBuilder icmpv6CodeCaseBuilder = new Icmpv6CodeCaseBuilder();
82                     Icmpv6CodeBuilder icmpv6CodeBuilder = new Icmpv6CodeBuilder();
83                     icmpv6CodeBuilder.setIcmpv6Code((short) code);
84                     icmpv6CodeCaseBuilder.setIcmpv6Code(icmpv6CodeBuilder.build());
85                     matchBuilder.setMatchEntryValue(icmpv6CodeCaseBuilder.build());
86                     break;
87                 case TCP:
88                     matchBuilder.setOxmMatchField(TcpDst.class);
89                     TcpDstCaseBuilder tcpDstCaseBuilder = new TcpDstCaseBuilder();
90                     TcpDstBuilder tcpDstBuilder = new TcpDstBuilder();
91                     tcpDstBuilder.setPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber(port));
92                     tcpDstCaseBuilder.setTcpDst(tcpDstBuilder.build());
93                     matchBuilder.setMatchEntryValue(tcpDstCaseBuilder.build());
94                     break;
95                 case UDP:
96                     matchBuilder.setOxmMatchField(UdpDst.class);
97                     UdpDstCaseBuilder udpDstCaseBuilder = new UdpDstCaseBuilder();
98                     UdpDstBuilder udpDstBuilder = new UdpDstBuilder();
99                     udpDstBuilder.setPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber(port));
100                     udpDstCaseBuilder.setUdpDst(udpDstBuilder.build());
101                     matchBuilder.setMatchEntryValue(udpDstCaseBuilder.build());
102                     break;
103                 default:
104                     LOG.warn("Unknown protocol with combination of SetSourcePort: {}", protocol);
105                     break;
106             }
107         } else {
108             LOG.warn("Missing protocol with combination of SetSourcePort");
109         }
110
111         List<MatchEntry> entries = new ArrayList<>();
112         entries.add(matchBuilder.build());
113         setFieldBuilder.setMatchEntry(entries);
114         setFieldCaseBuilder.setSetFieldAction(setFieldBuilder.build());
115
116         return Optional.of(new ActionBuilder()
117                 .setActionChoice(setFieldCaseBuilder.build())
118                 .build());
119     }
120 }