Bug 5540 - PortConvertor, MatchConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchV10ResponseConvertor.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.match;
10
11 import java.math.BigInteger;
12 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
13 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ParametrizedConvertor;
14 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
15 import org.opendaylight.openflowplugin.openflow.md.util.ActionUtil;
16 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
33
34 /**
35  * Converts Openflow 1.0 specific flow match to MD-SAL format flow
36  * match
37  *
38  * Example usage:
39  * <pre>
40  * {@code
41  * VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(version);
42  * data.setDatapathId(datapathId);
43  * Optional<MatchBuilder> salMatch = ConvertorManager.getInstance().convert(ofMatchV10, data);
44  * }
45  * </pre>
46  */
47 public class MatchV10ResponseConvertor implements ParametrizedConvertor<MatchV10, MatchBuilder, VersionDatapathIdConvertorData> {
48     private static final short PROTO_TCP = 6;
49     private static final short PROTO_UDP = 17;
50     private static final short PROTO_ICMPV4 = 1;
51     private static final String NO_IP = "0.0.0.0/0";
52
53     @Override
54     public Class<?> getType() {
55         return MatchV10.class;
56     }
57
58     @Override
59     public MatchBuilder convert(MatchV10 source, VersionDatapathIdConvertorData datapathIdConvertorData) {
60         MatchBuilder matchBuilder = new MatchBuilder();
61         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
62         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
63         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
64         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
65         OpenflowVersion ofVersion = OpenflowVersion.get(datapathIdConvertorData.getVersion());
66         BigInteger datapathid = datapathIdConvertorData.getDatapathId();
67
68         if (!source.getWildcards().isINPORT() && source.getInPort() != null) {
69             matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
70                     (long) source.getInPort(), ofVersion));
71         }
72
73         if (!source.getWildcards().isDLSRC() && source.getDlSrc() != null) {
74             EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
75             ethSrcBuilder.setAddress(source.getDlSrc());
76             ethMatchBuilder.setEthernetSource(ethSrcBuilder.build());
77             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
78         }
79         if (!source.getWildcards().isDLDST() && source.getDlDst() != null) {
80             EthernetDestinationBuilder ethDstBuilder = new EthernetDestinationBuilder();
81             ethDstBuilder.setAddress(source.getDlDst());
82             ethMatchBuilder.setEthernetDestination(ethDstBuilder.build());
83             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
84         }
85         if (!source.getWildcards().isDLTYPE() && source.getDlType() != null) {
86             EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
87             ethTypeBuilder.setType(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType(
88                     (long) source.getDlType()));
89             ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
90             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
91         }
92         if (!source.getWildcards().isDLVLAN() && source.getDlVlan() != null) {
93             VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
94             int vlanId = (source.getDlVlan() == (0xffff)) ? 0 : source.getDlVlan();
95             vlanIdBuilder.setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(vlanId));
96             vlanIdBuilder.setVlanIdPresent(vlanId != 0);
97             vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
98             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
99         }
100         if (!source.getWildcards().isDLVLANPCP() && source.getDlVlanPcp() != null) {
101             vlanMatchBuilder.setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
102                     source.getDlVlanPcp()));
103             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
104         }
105         if (!source.getWildcards().isDLTYPE() && source.getNwSrc() != null) {
106             final Ipv4Prefix prefix;
107             if (source.getNwSrcMask() != null) {
108                 prefix = IetfInetUtil.INSTANCE.ipv4PrefixFor(source.getNwSrc(), source.getNwSrcMask());
109             } else {
110                 //Openflow Spec : 1.3.2
111                 //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
112                 // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
113                 // statistics response.
114                 prefix = IetfInetUtil.INSTANCE.ipv4PrefixFor(source.getNwSrc());
115             }
116             if (!NO_IP.equals(prefix.getValue())) {
117                 ipv4MatchBuilder.setIpv4Source(prefix);
118                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
119             }
120         }
121         if (!source.getWildcards().isDLTYPE() && source.getNwDst() != null) {
122             final Ipv4Prefix prefix;
123             if (source.getNwDstMask() != null) {
124                 prefix = IetfInetUtil.INSTANCE.ipv4PrefixFor(source.getNwDst(), source.getNwDstMask());
125             } else {
126                 //Openflow Spec : 1.3.2
127                 //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
128                 // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
129                 // statistics response.
130                 prefix = IetfInetUtil.INSTANCE.ipv4PrefixFor(source.getNwDst());
131             }
132             if (!NO_IP.equals(prefix.getValue())) {
133                 ipv4MatchBuilder.setIpv4Destination(prefix);
134                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
135             }
136         }
137         if (!source.getWildcards().isNWPROTO() && source.getNwProto() != null) {
138             Short nwProto = source.getNwProto();
139             ipMatchBuilder.setIpProtocol(nwProto);
140             matchBuilder.setIpMatch(ipMatchBuilder.build());
141
142             int proto = nwProto.intValue();
143             if (proto == PROTO_TCP) {
144                 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
145                 boolean hasTcp = false;
146                 if (!source.getWildcards().isTPSRC() && source.getTpSrc() != null) {
147                     tcpMatchBuilder
148                             .setTcpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber(
149                                     source.getTpSrc()));
150                     hasTcp = true;
151                 }
152                 if (!source.getWildcards().isTPDST() && source.getTpDst() != null) {
153                     tcpMatchBuilder
154                             .setTcpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber(
155                                     source.getTpDst()));
156                     hasTcp = true;
157                 }
158
159                 if (hasTcp) {
160                     matchBuilder.setLayer4Match(tcpMatchBuilder.build());
161                 }
162             } else if (proto == PROTO_UDP) {
163                 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
164                 boolean hasUdp = false;
165                 if (!source.getWildcards().isTPSRC() && source.getTpSrc() != null) {
166                     udpMatchBuilder
167                             .setUdpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber(
168                                     source.getTpSrc()));
169                     hasUdp = true;
170                 }
171                 if (!source.getWildcards().isTPDST() && source.getTpDst() != null) {
172                     udpMatchBuilder
173                             .setUdpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber(
174                                     source.getTpDst()));
175                     hasUdp = true;
176                 }
177
178                 if (hasUdp) {
179                     matchBuilder.setLayer4Match(udpMatchBuilder.build());
180                 }
181             } else if (proto == PROTO_ICMPV4) {
182                 Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder();
183                 boolean hasIcmpv4 = false;
184                 if (!source.getWildcards().isTPSRC()) {
185                     Integer type = source.getTpSrc();
186                     if (type != null) {
187                         icmpv4MatchBuilder.setIcmpv4Type(type.shortValue());
188                         hasIcmpv4 = true;
189                     }
190                 }
191                 if (!source.getWildcards().isTPDST()) {
192                     Integer code = source.getTpDst();
193                     if (code != null) {
194                         icmpv4MatchBuilder.setIcmpv4Code(code.shortValue());
195                         hasIcmpv4 = true;
196                     }
197                 }
198
199                 if (hasIcmpv4) {
200                     matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
201                 }
202             }
203         }
204         if (!source.getWildcards().isNWTOS() && source.getNwTos() != null) {
205             Short dscp = ActionUtil.tosToDscp(source.getNwTos());
206             ipMatchBuilder.setIpDscp(new Dscp(dscp));
207             matchBuilder.setIpMatch(ipMatchBuilder.build());
208         }
209
210         return matchBuilder;
211     }
212 }