Merge "BUG-4123: Li:flow statistics are not passed into DS/operational"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorImpl.java
1 /**
2  * Copyright (c) 2013, 2015 Ericsson. 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 static org.opendaylight.openflowjava.util.ByteBufUtils.macAddressToString;
12
13 import com.google.common.base.Optional;
14 import java.math.BigInteger;
15 import java.nio.ByteBuffer;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.openflowjava.util.ByteBufUtils;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
23 import org.opendaylight.openflowplugin.extension.api.ConverterExtensionKey;
24 import org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava;
25 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionResolvers;
26 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.IpConversionUtil;
27 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
28 import org.opendaylight.openflowplugin.openflow.md.util.ActionUtil;
29 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
30 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6FlowLabel;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetField;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddress;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestination;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSource;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeader;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeaderBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6Label;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6LabelBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4Match;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6Match;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFields;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFieldsBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4Match;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4MatchBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.protocol.match.fields.Pbb;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.protocol.match.fields.PbbBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetFieldCase;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.field._case.SetFieldAction;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpOp;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpSha;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpSpa;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpTha;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpTpa;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.EthDst;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.EthSrc;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.EthType;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv4Code;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv4Type;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv6Code;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv6Type;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPhyPort;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPort;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpDscp;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpEcn;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpProto;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv4Dst;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv4Src;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6Dst;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6Exthdr;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6Flabel;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6NdSll;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6NdTarget;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6NdTll;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6Src;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Metadata;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MplsBos;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MplsLabel;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MplsTc;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.PbbIsid;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.SctpDst;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.SctpSrc;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TcpDst;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TcpSrc;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TunnelId;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.UdpDst;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.UdpSrc;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.VlanPcp;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.VlanVid;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpOpCase;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpOpCaseBuilder;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpShaCase;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpShaCaseBuilder;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpSpaCase;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpSpaCaseBuilder;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpThaCase;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpThaCaseBuilder;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpTpaCase;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpTpaCaseBuilder;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthDstCase;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthDstCaseBuilder;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthSrcCase;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthSrcCaseBuilder;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthTypeCase;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthTypeCaseBuilder;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4CodeCase;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4CodeCaseBuilder;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4TypeCase;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4TypeCaseBuilder;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv6CodeCase;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv6CodeCaseBuilder;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv6TypeCase;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv6TypeCaseBuilder;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCase;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCaseBuilder;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCaseBuilder;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpDscpCase;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpDscpCaseBuilder;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpEcnCase;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpEcnCaseBuilder;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpProtoCase;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpProtoCaseBuilder;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv4DstCase;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv4DstCaseBuilder;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv4SrcCase;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv4SrcCaseBuilder;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6DstCase;
179 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6DstCaseBuilder;
180 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6ExthdrCase;
181 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6ExthdrCaseBuilder;
182 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6FlabelCase;
183 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6FlabelCaseBuilder;
184 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6NdSllCase;
185 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6NdSllCaseBuilder;
186 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6NdTargetCase;
187 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6NdTargetCaseBuilder;
188 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6NdTllCase;
189 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6NdTllCaseBuilder;
190 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6SrcCase;
191 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6SrcCaseBuilder;
192 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MetadataCase;
193 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MetadataCaseBuilder;
194 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MplsBosCase;
195 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MplsBosCaseBuilder;
196 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MplsLabelCase;
197 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MplsLabelCaseBuilder;
198 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MplsTcCase;
199 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MplsTcCaseBuilder;
200 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.PbbIsidCase;
201 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.PbbIsidCaseBuilder;
202 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.SctpDstCase;
203 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.SctpDstCaseBuilder;
204 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.SctpSrcCase;
205 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.SctpSrcCaseBuilder;
206 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TcpDstCase;
207 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TcpDstCaseBuilder;
208 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TcpSrcCase;
209 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TcpSrcCaseBuilder;
210 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TunnelIdCase;
211 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TunnelIdCaseBuilder;
212 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.UdpDstCase;
213 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.UdpDstCaseBuilder;
214 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.UdpSrcCase;
215 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.UdpSrcCaseBuilder;
216 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.VlanPcpCase;
217 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.VlanPcpCaseBuilder;
218 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.VlanVidCase;
219 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.VlanVidCaseBuilder;
220 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.op._case.ArpOpBuilder;
221 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.sha._case.ArpShaBuilder;
222 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.spa._case.ArpSpaBuilder;
223 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.tha._case.ArpThaBuilder;
224 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.tpa._case.ArpTpaBuilder;
225 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.eth.dst._case.EthDstBuilder;
226 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.eth.src._case.EthSrcBuilder;
227 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.eth.type._case.EthTypeBuilder;
228 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.code._case.Icmpv4CodeBuilder;
229 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.type._case.Icmpv4TypeBuilder;
230 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv6.code._case.Icmpv6CodeBuilder;
231 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv6.type._case.Icmpv6TypeBuilder;
232 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.phy.port._case.InPhyPortBuilder;
233 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPortBuilder;
234 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.dscp._case.IpDscpBuilder;
235 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.ecn._case.IpEcnBuilder;
236 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.proto._case.IpProtoBuilder;
237 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv4.dst._case.Ipv4DstBuilder;
238 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv4.src._case.Ipv4SrcBuilder;
239 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.dst._case.Ipv6DstBuilder;
240 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.exthdr._case.Ipv6ExthdrBuilder;
241 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.flabel._case.Ipv6FlabelBuilder;
242 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.nd.sll._case.Ipv6NdSllBuilder;
243 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.nd.target._case.Ipv6NdTargetBuilder;
244 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.nd.tll._case.Ipv6NdTllBuilder;
245 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.src._case.Ipv6SrcBuilder;
246 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.mpls.bos._case.MplsBosBuilder;
247 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.mpls.label._case.MplsLabelBuilder;
248 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.mpls.tc._case.MplsTcBuilder;
249 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.pbb.isid._case.PbbIsidBuilder;
250 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.sctp.dst._case.SctpDstBuilder;
251 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.sctp.src._case.SctpSrcBuilder;
252 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tcp.dst._case.TcpDstBuilder;
253 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tcp.src._case.TcpSrcBuilder;
254 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tunnel.id._case.TunnelIdBuilder;
255 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.udp.dst._case.UdpDstBuilder;
256 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.udp.src._case.UdpSrcBuilder;
257 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.pcp._case.VlanPcpBuilder;
258 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.vid._case.VlanVidBuilder;
259 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
260 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
261 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralExtensionListGrouping;
262 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
263 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TunnelIpv4Dst;
264 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TunnelIpv4Src;
265 import org.slf4j.Logger;
266 import org.slf4j.LoggerFactory;
267
268 /**
269  * Utility class for converting a MD-SAL Flow into the OF flow mod
270  */
271 public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
272     private static final Logger logger = LoggerFactory.getLogger(MatchConvertorImpl.class);
273     private static final byte[] VLAN_VID_MASK = new byte[]{16, 0};
274     private static final short PROTO_TCP = 6;
275     private static final short PROTO_UDP = 17;
276     private static final short PROTO_ICMPV4 = 1;
277     private static final String NO_IP = "0.0.0.0/0";
278
279     @Override
280     public List<MatchEntry> convert(
281             final org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match match, final BigInteger datapathid) {
282         List<MatchEntry> matchEntryList = new ArrayList<>();
283         if (match == null) {
284             return matchEntryList;
285         }
286         if (match.getInPort() != null) {
287             //TODO: currently this matchconverter is mapped to OF1.3 in MatchReactorMappingFactory. Will need to revisit during 1.4+
288             matchEntryList.add(toOfPort(InPort.class,
289                     InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF13, match.getInPort())));
290         }
291
292         if (match.getInPhyPort() != null) {
293             //TODO: currently this matchconverter is mapped to OF1.3 in MatchReactorMappingFactory. Will need to revisit during 1.4+
294             matchEntryList.add(toOfPhyPort(InPhyPort.class,
295                     InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF13, match.getInPhyPort())));
296         }
297
298         org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Metadata metadata = match
299                 .getMetadata();
300         if (metadata != null) {
301             matchEntryList.add(toOfMetadata(Metadata.class, metadata.getMetadata(), metadata.getMetadataMask()));
302         }
303
304         EthernetMatch ethernetMatch = match.getEthernetMatch();
305         if (ethernetMatch != null) {
306             EthernetDestination ethernetDestination = ethernetMatch.getEthernetDestination();
307             if (ethernetDestination != null) {
308                 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
309                 matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
310                 matchEntryBuilder.setOxmMatchField(EthDst.class);
311                 EthDstCaseBuilder ethDstCaseBuilder = new EthDstCaseBuilder();
312                 EthDstBuilder ethDstBuilder = new EthDstBuilder();
313                 ethDstBuilder.setMacAddress(ethernetDestination.getAddress());
314                 boolean hasMask = false;
315                 if (null != ethernetDestination.getMask()) {
316                     ethDstBuilder.setMask(ByteBufUtils.macAddressToBytes(ethernetDestination.getMask().getValue()));
317                     hasMask = true;
318                 }
319                 ethDstCaseBuilder.setEthDst(ethDstBuilder.build());
320                 matchEntryBuilder.setMatchEntryValue(ethDstCaseBuilder.build());
321                 matchEntryBuilder.setHasMask(hasMask);
322                 matchEntryList.add(matchEntryBuilder.build());
323             }
324
325             EthernetSource ethernetSource = ethernetMatch.getEthernetSource();
326             if (ethernetSource != null) {
327                 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
328                 matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
329                 matchEntryBuilder.setOxmMatchField(EthSrc.class);
330
331                 EthSrcCaseBuilder ethSrcCaseBuilder = new EthSrcCaseBuilder();
332                 EthSrcBuilder ethDstBuilder = new EthSrcBuilder();
333                 ethDstBuilder.setMacAddress(ethernetSource.getAddress());
334                 boolean hasMask = false;
335                 if (null != ethernetSource.getMask()) {
336                     ethDstBuilder.setMask(ByteBufUtils.macAddressToBytes(ethernetSource.getMask().getValue()));
337                     hasMask = true;
338                 }
339                 ethSrcCaseBuilder.setEthSrc(ethDstBuilder.build());
340                 matchEntryBuilder.setMatchEntryValue(ethSrcCaseBuilder.build());
341                 matchEntryBuilder.setHasMask(hasMask);
342                 matchEntryList.add(matchEntryBuilder.build());
343             }
344
345             if (ethernetMatch.getEthernetType() != null) {
346                 matchEntryList.add(toOfEthernetType(ethernetMatch.getEthernetType()));
347             }
348         }
349
350         VlanMatch vlanMatch = match.getVlanMatch();
351         if (vlanMatch != null) {
352             if (vlanMatch.getVlanId() != null) {
353                 VlanId vlanId = vlanMatch.getVlanId();
354                 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
355                 matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
356                 matchEntryBuilder.setOxmMatchField(VlanVid.class);
357
358                 VlanVidCaseBuilder vlanVidCaseBuilder = new VlanVidCaseBuilder();
359                 VlanVidBuilder vlanVidBuilder = new VlanVidBuilder();
360                 boolean setCfiBit = false;
361                 Integer vidEntryValue = 0;
362                 boolean hasmask = false;
363                 if (Boolean.TRUE.equals(vlanId.isVlanIdPresent())) {
364                     setCfiBit = true;
365                     if (vlanId.getVlanId() != null) {
366                         vidEntryValue = vlanId.getVlanId().getValue();
367                     }
368                     hasmask = (vidEntryValue == 0);
369                     if (hasmask) {
370                         vlanVidBuilder.setMask(VLAN_VID_MASK);
371                     }
372                 }
373
374                 vlanVidBuilder.setCfiBit(setCfiBit);
375                 vlanVidBuilder.setVlanVid(vidEntryValue);
376                 vlanVidCaseBuilder.setVlanVid(vlanVidBuilder.build());
377                 matchEntryBuilder.setMatchEntryValue(vlanVidCaseBuilder.build());
378                 matchEntryBuilder.setHasMask(hasmask);
379                 matchEntryList.add(matchEntryBuilder.build());
380             }
381
382             if (vlanMatch.getVlanPcp() != null) {
383                 matchEntryList.add(toOfVlanPcp(vlanMatch.getVlanPcp()));
384             }
385         }
386
387         IpMatch ipMatch = match.getIpMatch();
388         if (ipMatch != null) {
389             if (ipMatch.getIpDscp() != null) {
390                 matchEntryList.add(toOfIpDscp(ipMatch.getIpDscp()));
391             }
392
393             if (ipMatch.getIpEcn() != null) {
394                 matchEntryList.add(toOfIpEcn(ipMatch.getIpEcn()));
395             }
396
397             if (ipMatch.getIpProtocol() != null) {
398                 matchEntryList.add(toOfIpProto(ipMatch.getIpProtocol()));
399             }
400
401         }
402
403         Layer4Match layer4Match = match.getLayer4Match();
404         if (layer4Match != null) {
405             if (layer4Match instanceof TcpMatch) {
406                 TcpMatch tcpMatch = (TcpMatch) layer4Match;
407
408                 if (tcpMatch.getTcpSourcePort() != null) {
409                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
410                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
411                     matchEntryBuilder.setOxmMatchField(TcpSrc.class);
412
413                     TcpSrcCaseBuilder tcpSrcCaseBuilder = new TcpSrcCaseBuilder();
414                     TcpSrcBuilder tcpSrcBuilder = new TcpSrcBuilder();
415                     tcpSrcBuilder.setPort(tcpMatch.getTcpSourcePort());
416                     tcpSrcCaseBuilder.setTcpSrc(tcpSrcBuilder.build());
417
418                     matchEntryBuilder.setMatchEntryValue(tcpSrcCaseBuilder.build());
419                     matchEntryBuilder.setHasMask(false);
420                     matchEntryList.add(matchEntryBuilder.build());
421                 }
422                 if (tcpMatch.getTcpDestinationPort() != null) {
423                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
424                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
425                     matchEntryBuilder.setOxmMatchField(TcpDst.class);
426
427                     TcpDstCaseBuilder tcpDstCaseBuilder = new TcpDstCaseBuilder();
428                     TcpDstBuilder tcpDstBuilder = new TcpDstBuilder();
429                     tcpDstBuilder.setPort(tcpMatch.getTcpDestinationPort());
430                     tcpDstCaseBuilder.setTcpDst(tcpDstBuilder.build());
431                     matchEntryBuilder.setMatchEntryValue(tcpDstCaseBuilder.build());
432                     matchEntryBuilder.setHasMask(false);
433                     matchEntryList.add(matchEntryBuilder.build());
434                 }
435             } else if (layer4Match instanceof UdpMatch) {
436                 UdpMatch udpMatch = (UdpMatch) layer4Match;
437                 if (udpMatch.getUdpSourcePort() != null) {
438                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
439                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
440                     matchEntryBuilder.setOxmMatchField(UdpSrc.class);
441
442                     UdpSrcCaseBuilder udpSrcCaseBuilder = new UdpSrcCaseBuilder();
443                     UdpSrcBuilder udpSrcBuilder = new UdpSrcBuilder();
444                     boolean hasMask = false;
445                     udpSrcBuilder.setPort(udpMatch.getUdpSourcePort());
446                     udpSrcCaseBuilder.setUdpSrc(udpSrcBuilder.build());
447                     matchEntryBuilder.setMatchEntryValue(udpSrcCaseBuilder.build());
448                     matchEntryBuilder.setHasMask(hasMask);
449                     matchEntryList.add(matchEntryBuilder.build());
450                 }
451
452                 if (udpMatch.getUdpDestinationPort() != null) {
453                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
454                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
455                     matchEntryBuilder.setOxmMatchField(UdpDst.class);
456
457                     UdpDstCaseBuilder udpDstCaseBuilder = new UdpDstCaseBuilder();
458                     UdpDstBuilder udpDstBuilder = new UdpDstBuilder();
459                     udpDstBuilder.setPort(udpMatch.getUdpDestinationPort());
460                     udpDstCaseBuilder.setUdpDst(udpDstBuilder.build());
461                     matchEntryBuilder.setMatchEntryValue(udpDstCaseBuilder.build());
462                     matchEntryBuilder.setHasMask(false);
463                     matchEntryList.add(matchEntryBuilder.build());
464                 }
465             } else if (layer4Match instanceof SctpMatch) {
466                 SctpMatch sctpMatch = (SctpMatch) layer4Match;
467                 if (sctpMatch.getSctpSourcePort() != null) {
468                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
469                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
470                     matchEntryBuilder.setOxmMatchField(SctpSrc.class);
471
472                     SctpSrcCaseBuilder sctpSrcCaseBuilder = new SctpSrcCaseBuilder();
473                     SctpSrcBuilder sctpSrcBuilder = new SctpSrcBuilder();
474                     sctpSrcBuilder.setPort(sctpMatch.getSctpSourcePort());
475                     sctpSrcCaseBuilder.setSctpSrc(sctpSrcBuilder.build());
476                     matchEntryBuilder.setMatchEntryValue(sctpSrcCaseBuilder.build());
477                     matchEntryBuilder.setHasMask(false);
478                     matchEntryList.add(matchEntryBuilder.build());
479                 }
480
481                 if (sctpMatch.getSctpDestinationPort() != null) {
482                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
483                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
484                     matchEntryBuilder.setOxmMatchField(SctpDst.class);
485
486                     SctpDstCaseBuilder sctpDstCaseBuilder = new SctpDstCaseBuilder();
487                     SctpDstBuilder sctpDstBuilder = new SctpDstBuilder();
488                     sctpDstBuilder.setPort(sctpMatch.getSctpDestinationPort());
489                     sctpDstCaseBuilder.setSctpDst(sctpDstBuilder.build());
490                     matchEntryBuilder.setMatchEntryValue(sctpDstCaseBuilder.build());
491                     matchEntryBuilder.setHasMask(false);
492                     matchEntryList.add(matchEntryBuilder.build());
493                 }
494             }
495         }
496
497         Icmpv4Match icmpv4Match = match.getIcmpv4Match();
498         if (icmpv4Match != null) {
499             if (icmpv4Match.getIcmpv4Type() != null) {
500                 matchEntryList.add(toOfIcmpv4Type(icmpv4Match.getIcmpv4Type()));
501             }
502
503             if (icmpv4Match.getIcmpv4Code() != null) {
504                 matchEntryList.add(toOfIcmpv4Code(icmpv4Match.getIcmpv4Code()));
505             }
506         }
507
508         Icmpv6Match icmpv6Match = match.getIcmpv6Match();
509         if (icmpv6Match != null) {
510             if (icmpv6Match.getIcmpv6Type() != null) {
511                 matchEntryList.add(toOfIcmpv6Type(icmpv6Match.getIcmpv6Type()));
512             }
513
514             if (icmpv6Match.getIcmpv6Code() != null) {
515                 matchEntryList.add(toOfIcmpv6Code(icmpv6Match.getIcmpv6Code()));
516             }
517         }
518
519         Layer3Match layer3Match = match.getLayer3Match();
520         if (layer3Match != null) {
521             if (layer3Match instanceof Ipv4Match) {
522                 Ipv4Match ipv4Match = (Ipv4Match) layer3Match;
523                 if (ipv4Match.getIpv4Source() != null) {
524                     Ipv4Prefix ipv4Prefix = ipv4Match.getIpv4Source();
525                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
526                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
527                     matchEntryBuilder.setOxmMatchField(Ipv4Src.class);
528
529                     Ipv4SrcCaseBuilder ipv4SrcCaseBuilder = new Ipv4SrcCaseBuilder();
530                     Ipv4SrcBuilder ipv4SrcBuilder = new Ipv4SrcBuilder();
531
532                     Iterator<String> addressParts = IpConversionUtil.splitToParts(ipv4Prefix);
533                     Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
534                     ipv4SrcBuilder.setIpv4Address(ipv4Address);
535                     boolean hasMask = false;
536                     byte[] mask = extractIpv4Mask(hasMask, addressParts);
537                     if (null != mask) {
538                         ipv4SrcBuilder.setMask(mask);
539                         hasMask = true;
540                     }
541                     matchEntryBuilder.setHasMask(hasMask);
542                     ipv4SrcCaseBuilder.setIpv4Src(ipv4SrcBuilder.build());
543                     matchEntryBuilder.setMatchEntryValue(ipv4SrcCaseBuilder.build());
544                     matchEntryList.add(matchEntryBuilder.build());
545                 }
546                 if (ipv4Match.getIpv4Destination() != null) {
547                     Ipv4Prefix ipv4Prefix = ipv4Match.getIpv4Destination();
548                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
549                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
550                     matchEntryBuilder.setOxmMatchField(Ipv4Dst.class);
551
552                     Ipv4DstCaseBuilder ipv4DstCaseBuilder = new Ipv4DstCaseBuilder();
553                     Ipv4DstBuilder ipv4DstBuilder = new Ipv4DstBuilder();
554
555                     Iterator<String> addressParts = IpConversionUtil.splitToParts(ipv4Prefix);
556                     Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
557                     ipv4DstBuilder.setIpv4Address(ipv4Address);
558                     boolean hasMask = false;
559                     byte[] mask = extractIpv4Mask(hasMask, addressParts);
560                     if (null != mask) {
561                         ipv4DstBuilder.setMask(mask);
562                         hasMask = true;
563                     }
564                     matchEntryBuilder.setHasMask(hasMask);
565                     ipv4DstCaseBuilder.setIpv4Dst(ipv4DstBuilder.build());
566                     matchEntryBuilder.setMatchEntryValue(ipv4DstCaseBuilder.build());
567                     matchEntryList.add(matchEntryBuilder.build());
568                 }
569             }
570             if (layer3Match instanceof TunnelIpv4Match) {
571                 TunnelIpv4Match tunnelIpv4Src = (TunnelIpv4Match) layer3Match;
572                 if (tunnelIpv4Src.getTunnelIpv4Source() != null) {
573                     Ipv4Prefix ipv4Prefix = tunnelIpv4Src.getTunnelIpv4Source();
574                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
575                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
576                     matchEntryBuilder.setOxmMatchField(Ipv4Src.class);
577
578                     Ipv4SrcCaseBuilder ipv4SrcCaseBuilder = new Ipv4SrcCaseBuilder();
579                     Ipv4SrcBuilder ipv4SrcBuilder = new Ipv4SrcBuilder();
580
581                     Iterator<String> addressParts = IpConversionUtil.splitToParts(ipv4Prefix);
582                     Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
583                     ipv4SrcBuilder.setIpv4Address(ipv4Address);
584                     boolean hasMask = false;
585                     byte[] mask = extractIpv4Mask(hasMask, addressParts);
586                     if (null != mask) {
587                         ipv4SrcBuilder.setMask(mask);
588                         hasMask = true;
589                     }
590                     matchEntryBuilder.setHasMask(hasMask);
591                     ipv4SrcCaseBuilder.setIpv4Src(ipv4SrcBuilder.build());
592                     matchEntryBuilder.setMatchEntryValue(ipv4SrcCaseBuilder.build());
593                     matchEntryList.add(matchEntryBuilder.build());
594                 }
595                 if (tunnelIpv4Src.getTunnelIpv4Destination() != null) {
596                     Ipv4Prefix ipv4Prefix = tunnelIpv4Src.getTunnelIpv4Destination();
597                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
598                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
599                     matchEntryBuilder.setOxmMatchField(Ipv4Dst.class);
600
601                     Ipv4DstCaseBuilder ipv4DstCaseBuilder = new Ipv4DstCaseBuilder();
602                     Ipv4DstBuilder ipv4DstBuilder = new Ipv4DstBuilder();
603
604                     Iterator<String> addressParts = IpConversionUtil.splitToParts(ipv4Prefix);
605                     Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
606                     ipv4DstBuilder.setIpv4Address(ipv4Address);
607                     boolean hasMask = false;
608                     byte[] mask = extractIpv4Mask(hasMask, addressParts);
609                     if (null != mask) {
610                         ipv4DstBuilder.setMask(mask);
611                         hasMask = true;
612                     }
613                     matchEntryBuilder.setHasMask(hasMask);
614                     ipv4DstCaseBuilder.setIpv4Dst(ipv4DstBuilder.build());
615                     matchEntryBuilder.setMatchEntryValue(ipv4DstCaseBuilder.build());
616                     matchEntryList.add(matchEntryBuilder.build());
617                 }
618             } else if (layer3Match instanceof ArpMatch) {
619                 ArpMatch arpMatch = (ArpMatch) layer3Match;
620                 if (arpMatch.getArpOp() != null) {
621                     matchEntryList.add(toOfArpOpCode(arpMatch.getArpOp()));
622                 }
623
624                 if (arpMatch.getArpSourceTransportAddress() != null) {
625                     Ipv4Prefix ipv4Prefix = arpMatch.getArpSourceTransportAddress();
626                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
627                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
628                     matchEntryBuilder.setOxmMatchField(ArpSpa.class);
629
630                     ArpSpaCaseBuilder arpSpaCaseBuilder = new ArpSpaCaseBuilder();
631                     ArpSpaBuilder arpSpaBuilder = new ArpSpaBuilder();
632
633                     Iterator<String> addressParts = IpConversionUtil.splitToParts(ipv4Prefix);
634                     Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
635                     arpSpaBuilder.setIpv4Address(ipv4Address);
636                     boolean hasMask = false;
637                     byte[] mask = extractIpv4Mask(hasMask, addressParts);
638                     if (null != mask) {
639                         arpSpaBuilder.setMask(mask);
640                         hasMask = true;
641                     }
642                     matchEntryBuilder.setHasMask(hasMask);
643                     arpSpaCaseBuilder.setArpSpa(arpSpaBuilder.build());
644                     matchEntryBuilder.setMatchEntryValue(arpSpaCaseBuilder.build());
645                     matchEntryList.add(matchEntryBuilder.build());
646                 }
647
648                 if (arpMatch.getArpTargetTransportAddress() != null) {
649                     Ipv4Prefix ipv4Prefix = arpMatch.getArpTargetTransportAddress();
650                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
651                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
652                     matchEntryBuilder.setOxmMatchField(ArpTpa.class);
653
654                     ArpTpaCaseBuilder arpTpaCaseBuilder = new ArpTpaCaseBuilder();
655                     ArpTpaBuilder arpTpaBuilder = new ArpTpaBuilder();
656
657                     Iterator<String> addressParts = IpConversionUtil.splitToParts(ipv4Prefix);
658                     Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
659                     arpTpaBuilder.setIpv4Address(ipv4Address);
660                     boolean hasMask = false;
661                     byte[] mask = extractIpv4Mask(hasMask, addressParts);
662                     if (null != mask) {
663                         arpTpaBuilder.setMask(mask);
664                         hasMask = true;
665                     }
666                     matchEntryBuilder.setHasMask(hasMask);
667                     arpTpaCaseBuilder.setArpTpa(arpTpaBuilder.build());
668                     matchEntryBuilder.setMatchEntryValue(arpTpaCaseBuilder.build());
669                     matchEntryList.add(matchEntryBuilder.build());
670                 }
671
672                 ArpSourceHardwareAddress arpSourceHardwareAddress = arpMatch.getArpSourceHardwareAddress();
673                 if (arpSourceHardwareAddress != null) {
674                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
675                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
676                     matchEntryBuilder.setOxmMatchField(ArpSha.class);
677
678                     ArpShaCaseBuilder arpShaCaseBuilder = new ArpShaCaseBuilder();
679                     ArpShaBuilder arpShaBuilder = new ArpShaBuilder();
680                     arpShaBuilder.setMacAddress(arpSourceHardwareAddress.getAddress());
681                     boolean hasMask = false;
682                     if (null != arpSourceHardwareAddress.getMask()) {
683                         arpShaBuilder.setMask(ByteBufUtils.macAddressToBytes(arpSourceHardwareAddress.getMask().getValue()));
684                         hasMask = true;
685                     }
686                     arpShaCaseBuilder.setArpSha(arpShaBuilder.build());
687                     matchEntryBuilder.setMatchEntryValue(arpShaCaseBuilder.build());
688                     matchEntryBuilder.setHasMask(hasMask);
689                     matchEntryList.add(matchEntryBuilder.build());
690                 }
691
692                 ArpTargetHardwareAddress arpTargetHardwareAddress = arpMatch.getArpTargetHardwareAddress();
693                 if (arpTargetHardwareAddress != null) {
694                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
695                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
696                     matchEntryBuilder.setOxmMatchField(ArpTha.class);
697
698                     ArpThaCaseBuilder arpThaCaseBuilder = new ArpThaCaseBuilder();
699                     ArpThaBuilder arpThaBuilder = new ArpThaBuilder();
700                     arpThaBuilder.setMacAddress(arpTargetHardwareAddress.getAddress());
701                     boolean hasMask = false;
702                     if (null != arpTargetHardwareAddress.getMask()) {
703                         arpThaBuilder.setMask(ByteBufUtils.macAddressToBytes(arpTargetHardwareAddress.getMask().getValue()));
704                         hasMask = true;
705                     }
706                     arpThaCaseBuilder.setArpTha(arpThaBuilder.build());
707                     matchEntryBuilder.setMatchEntryValue(arpThaCaseBuilder.build());
708                     matchEntryBuilder.setHasMask(hasMask);
709                     matchEntryList.add(matchEntryBuilder.build());
710                 }
711             } else if (layer3Match instanceof Ipv6Match) {
712                 Ipv6Match ipv6Match = (Ipv6Match) layer3Match;
713                 if (ipv6Match.getIpv6Source() != null) {
714                     Ipv6Prefix ipv6Prefix = ipv6Match.getIpv6Source();
715                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
716                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
717                     matchEntryBuilder.setOxmMatchField(Ipv6Src.class);
718
719                     Ipv6SrcCaseBuilder ipv6SrcCaseBuilder = new Ipv6SrcCaseBuilder();
720                     Ipv6SrcBuilder ipv6SrcBuilder = new Ipv6SrcBuilder();
721                     final Integer prefix = IpConversionUtil.extractIpv6Prefix(ipv6Prefix);
722                     boolean hasMask = false;
723                     if (null != prefix) {
724                         ipv6SrcBuilder.setMask(IpConversionUtil.convertIpv6PrefixToByteArray(prefix));
725                         hasMask = true;
726                     }
727                     ipv6SrcBuilder.setIpv6Address(IpConversionUtil.extractIpv6Address(ipv6Prefix));
728                     ipv6SrcCaseBuilder.setIpv6Src(ipv6SrcBuilder.build());
729                     matchEntryBuilder.setHasMask(hasMask);
730                     matchEntryBuilder.setMatchEntryValue(ipv6SrcCaseBuilder.build());
731                     matchEntryList.add(matchEntryBuilder.build());
732                 }
733
734                 if (ipv6Match.getIpv6Destination() != null) {
735                     Ipv6Prefix ipv6Prefix = ipv6Match.getIpv6Destination();
736                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
737                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
738                     matchEntryBuilder.setOxmMatchField(Ipv6Dst.class);
739
740                     Ipv6DstCaseBuilder ipv6DstCaseBuilder = new Ipv6DstCaseBuilder();
741                     Ipv6DstBuilder ipv6DstBuilder = new Ipv6DstBuilder();
742                     final Integer prefix = IpConversionUtil.extractIpv6Prefix(ipv6Prefix);
743                     boolean hasMask = false;
744                     if (null != prefix) {
745                         ipv6DstBuilder.setMask(IpConversionUtil.convertIpv6PrefixToByteArray(prefix));
746                         hasMask = true;
747                     }
748                     ipv6DstBuilder.setIpv6Address(IpConversionUtil.extractIpv6Address(ipv6Prefix));
749                     ipv6DstCaseBuilder.setIpv6Dst(ipv6DstBuilder.build());
750                     matchEntryBuilder.setHasMask(hasMask);
751                     matchEntryBuilder.setMatchEntryValue(ipv6DstCaseBuilder.build());
752                     matchEntryList.add(matchEntryBuilder.build());
753                 }
754
755                 if (ipv6Match.getIpv6Label() != null) {
756                     matchEntryList.add(toOfIpv6FlowLabel(ipv6Match.getIpv6Label()));
757                 }
758
759                 if (ipv6Match.getIpv6NdTarget() != null) {
760                     matchEntryList.add(toOfIpv6NdTargetAddress(ipv6Match.getIpv6NdTarget()));
761                 }
762
763                 if (ipv6Match.getIpv6NdSll() != null) {
764                     MacAddress ipv6NdSll = ipv6Match.getIpv6NdSll();
765                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
766                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
767                     matchEntryBuilder.setOxmMatchField(Ipv6NdSll.class);
768
769                     Ipv6NdSllCaseBuilder ipv6NdSllCaseBuilder = new Ipv6NdSllCaseBuilder();
770                     Ipv6NdSllBuilder ipv6NdSllBuilder = new Ipv6NdSllBuilder();
771                     ipv6NdSllBuilder.setMacAddress(ipv6NdSll);
772                     ipv6NdSllCaseBuilder.setIpv6NdSll(ipv6NdSllBuilder.build());
773                     matchEntryBuilder.setMatchEntryValue(ipv6NdSllCaseBuilder.build());
774                     matchEntryBuilder.setHasMask(false);
775                     matchEntryList.add(matchEntryBuilder.build());
776                 }
777
778                 if (ipv6Match.getIpv6NdTll() != null) {
779                     MacAddress ipv6NdSll = ipv6Match.getIpv6NdTll();
780                     MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
781                     matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
782                     matchEntryBuilder.setOxmMatchField(Ipv6NdTll.class);
783
784                     Ipv6NdTllCaseBuilder ipv6NdTllCaseBuilder = new Ipv6NdTllCaseBuilder();
785                     Ipv6NdTllBuilder ipv6NdTllBuilder = new Ipv6NdTllBuilder();
786                     ipv6NdTllBuilder.setMacAddress(ipv6NdSll);
787                     ipv6NdTllCaseBuilder.setIpv6NdTll(ipv6NdTllBuilder.build());
788                     matchEntryBuilder.setMatchEntryValue(ipv6NdTllCaseBuilder.build());
789                     matchEntryBuilder.setHasMask(false);
790                     matchEntryList.add(matchEntryBuilder.build());
791
792                 }
793
794                 if (ipv6Match.getIpv6ExtHeader() != null) {
795                     matchEntryList.add(toOfIpv6ExtHeader(ipv6Match.getIpv6ExtHeader()));
796                 }
797             }
798         }
799
800         ProtocolMatchFields protocolMatchFields = match.getProtocolMatchFields();
801         if (protocolMatchFields != null) {
802             if (protocolMatchFields.getMplsLabel() != null) {
803                 matchEntryList.add(toOfMplsLabel(protocolMatchFields.getMplsLabel()));
804             }
805
806             if (protocolMatchFields.getMplsBos() != null) {
807                 matchEntryList.add(toOfMplsBos(protocolMatchFields.getMplsBos()));
808             }
809
810             if (protocolMatchFields.getMplsTc() != null) {
811                 matchEntryList.add(toOfMplsTc(protocolMatchFields.getMplsTc()));
812             }
813
814             if (protocolMatchFields.getPbb() != null) {
815                 matchEntryList.add(toOfMplsPbb(protocolMatchFields.getPbb()));
816             }
817         }
818
819         org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Tunnel tunnel = match
820                 .getTunnel();
821         if (tunnel != null) {
822
823
824             MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
825             TunnelIdCaseBuilder tunnelIdCaseBuilder = new TunnelIdCaseBuilder();
826             TunnelIdBuilder tunnelIdBuilder = new TunnelIdBuilder();
827             boolean hasMask = false;
828             if (null != tunnel.getTunnelMask()) {
829                 hasMask = true;
830                 tunnelIdBuilder.setMask(ByteUtil.convertBigIntegerToNBytes(tunnel.getTunnelMask(), OFConstants.SIZE_OF_LONG_IN_BYTES));
831             }
832             tunnelIdBuilder.setTunnelId(ByteUtil.convertBigIntegerToNBytes(tunnel.getTunnelId(), OFConstants.SIZE_OF_LONG_IN_BYTES));
833             tunnelIdCaseBuilder.setTunnelId(tunnelIdBuilder.build());
834             matchEntryBuilder.setMatchEntryValue(tunnelIdCaseBuilder.build());
835             matchEntryBuilder.setHasMask(hasMask);
836             matchEntryBuilder.setOxmMatchField(TunnelId.class);
837             matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
838             matchEntryList.add(matchEntryBuilder.build());
839         }
840
841
842         /**
843          * TODO: EXTENSION PROPOSAL (match, MD-SAL to OFJava)
844          * - we might need version for conversion and for key
845          * - sanitize NPE
846          */
847         Optional<GeneralExtensionListGrouping> extensionListOpt = ExtensionResolvers.getMatchExtensionResolver().getExtension(match);
848         if (extensionListOpt.isPresent()) {
849             for (ExtensionList extensionItem : extensionListOpt.get().getExtensionList()) {
850                 // TODO: get real version
851                 ConverterExtensionKey<? extends ExtensionKey> key = new ConverterExtensionKey<>(extensionItem.getExtensionKey(), OFConstants.OFP_VERSION_1_3);
852                 ConvertorToOFJava<MatchEntry> convertor =
853                         OFSessionUtil.getExtensionConvertorProvider().getConverter(key);
854                 MatchEntry ofMatch = convertor.convert(extensionItem.getExtension());
855                 matchEntryList.add(ofMatch);
856             }
857         }
858
859         return matchEntryList;
860     }
861
862
863     private static byte[] extractIpv4Mask(boolean hasMask, final Iterator<String> addressParts) {
864         final int prefix;
865         if (addressParts.hasNext()) {
866             int potentionalPrefix = Integer.parseInt(addressParts.next());
867             prefix = potentionalPrefix < 32 ? potentionalPrefix : 0;
868         } else {
869             prefix = 0;
870         }
871
872         if (prefix != 0) {
873             int mask = 0xffffffff << (32 - prefix);
874             byte[] maskBytes = new byte[]{(byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8),
875                     (byte) mask};
876             hasMask = true;
877             return maskBytes;
878         }
879         return null;
880     }
881
882     /**
883      * Method convert Openflow 1.0 specific flow match to MD-SAL format flow
884      * match
885      *
886      * @param swMatch
887      * @return
888      * @author avishnoi@in.ibm.com
889      */
890     public static MatchBuilder fromOFMatchV10ToSALMatch(@Nonnull final MatchV10 swMatch, @Nonnull final BigInteger datapathid, @Nonnull final OpenflowVersion ofVersion) {
891         MatchBuilder matchBuilder = new MatchBuilder();
892         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
893         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
894         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
895         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
896         if (!swMatch.getWildcards().isINPORT().booleanValue() && swMatch.getInPort() != null) {
897             matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
898                     (long) swMatch.getInPort(), ofVersion));
899         }
900
901         if (!swMatch.getWildcards().isDLSRC().booleanValue() && swMatch.getDlSrc() != null) {
902             EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
903             ethSrcBuilder.setAddress(swMatch.getDlSrc());
904             ethMatchBuilder.setEthernetSource(ethSrcBuilder.build());
905             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
906         }
907         if (!swMatch.getWildcards().isDLDST().booleanValue() && swMatch.getDlDst() != null) {
908             EthernetDestinationBuilder ethDstBuilder = new EthernetDestinationBuilder();
909             ethDstBuilder.setAddress(swMatch.getDlDst());
910             ethMatchBuilder.setEthernetDestination(ethDstBuilder.build());
911             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
912         }
913         if (!swMatch.getWildcards().isDLTYPE().booleanValue() && swMatch.getDlType() != null) {
914             EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
915             ethTypeBuilder.setType(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType(
916                     (long) swMatch.getDlType()));
917             ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
918             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
919         }
920         if (!swMatch.getWildcards().isDLVLAN().booleanValue() && swMatch.getDlVlan() != null) {
921             VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
922             int vlanId = (swMatch.getDlVlan() == (0xffff)) ? 0 : swMatch.getDlVlan();
923             vlanIdBuilder.setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(vlanId));
924             vlanIdBuilder.setVlanIdPresent(vlanId == 0 ? false : true);
925             vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
926             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
927         }
928         if (!swMatch.getWildcards().isDLVLANPCP().booleanValue() && swMatch.getDlVlanPcp() != null) {
929             vlanMatchBuilder.setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
930                     swMatch.getDlVlanPcp()));
931             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
932         }
933         if (!swMatch.getWildcards().isDLTYPE().booleanValue() && swMatch.getNwSrc() != null) {
934             String ipv4PrefixStr = swMatch.getNwSrc().getValue();
935             if (swMatch.getNwSrcMask() != null) {
936                 ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + swMatch.getNwSrcMask();
937             } else {
938                 //Openflow Spec : 1.3.2
939                 //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
940                 // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
941                 // statistics response.
942
943                 ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + "32";
944             }
945             if (!NO_IP.equals(ipv4PrefixStr)) {
946                 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
947                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
948             }
949         }
950         if (!swMatch.getWildcards().isDLTYPE().booleanValue() && swMatch.getNwDst() != null) {
951             String ipv4PrefixStr = swMatch.getNwDst().getValue();
952             if (swMatch.getNwDstMask() != null) {
953                 ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + swMatch.getNwDstMask();
954             } else {
955                 //Openflow Spec : 1.3.2
956                 //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
957                 // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
958                 // statistics response.
959
960                 ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + "32";
961             }
962             if (!NO_IP.equals(ipv4PrefixStr)) {
963                 ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
964                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
965             }
966         }
967         if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() != null) {
968             Short nwProto = swMatch.getNwProto();
969             ipMatchBuilder.setIpProtocol(nwProto);
970             matchBuilder.setIpMatch(ipMatchBuilder.build());
971
972             int proto = nwProto.intValue();
973             if (proto == PROTO_TCP) {
974                 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
975                 boolean hasTcp = false;
976                 if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
977                     tcpMatchBuilder
978                             .setTcpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
979                                     swMatch.getTpSrc()));
980                     hasTcp = true;
981                 }
982                 if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
983                     tcpMatchBuilder
984                             .setTcpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
985                                     swMatch.getTpDst()));
986                     hasTcp = true;
987                 }
988
989                 if (hasTcp) {
990                     matchBuilder.setLayer4Match(tcpMatchBuilder.build());
991                 }
992             } else if (proto == PROTO_UDP) {
993                 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
994                 boolean hasUdp = false;
995                 if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
996                     udpMatchBuilder
997                             .setUdpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
998                                     swMatch.getTpSrc()));
999                     hasUdp = true;
1000                 }
1001                 if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
1002                     udpMatchBuilder
1003                             .setUdpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
1004                                     swMatch.getTpDst()));
1005                     hasUdp = true;
1006                 }
1007
1008                 if (hasUdp) {
1009                     matchBuilder.setLayer4Match(udpMatchBuilder.build());
1010                 }
1011             } else if (proto == PROTO_ICMPV4) {
1012                 Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder();
1013                 boolean hasIcmpv4 = false;
1014                 if (!swMatch.getWildcards().isTPSRC().booleanValue()) {
1015                     Integer type = swMatch.getTpSrc();
1016                     if (type != null) {
1017                         icmpv4MatchBuilder.setIcmpv4Type(type.shortValue());
1018                         hasIcmpv4 = true;
1019                     }
1020                 }
1021                 if (!swMatch.getWildcards().isTPDST().booleanValue()) {
1022                     Integer code = swMatch.getTpDst();
1023                     if (code != null) {
1024                         icmpv4MatchBuilder.setIcmpv4Code(code.shortValue());
1025                         hasIcmpv4 = true;
1026                     }
1027                 }
1028
1029                 if (hasIcmpv4) {
1030                     matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
1031                 }
1032             }
1033         }
1034         if (!swMatch.getWildcards().isNWTOS().booleanValue() && swMatch.getNwTos() != null) {
1035             Short dscp = ActionUtil.tosToDscp(swMatch.getNwTos().shortValue());
1036             ipMatchBuilder.setIpDscp(new Dscp(dscp));
1037             matchBuilder.setIpMatch(ipMatchBuilder.build());
1038         }
1039
1040         return matchBuilder;
1041     }
1042
1043     /**
1044      * Method converts Openflow 1.3+ specific flow match to MD-SAL format flow
1045      * match
1046      *
1047      * @param swMatch
1048      * @param swMatch
1049      * @param datapathid
1050      * @param ofVersion
1051      * @return md-sal match instance
1052      * @author avishnoi@in.ibm.com
1053      */
1054     public static MatchBuilder fromOFMatchToSALMatch(
1055             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match swMatch,
1056             @Nonnull final BigInteger datapathid, @Nonnull final OpenflowVersion ofVersion) {
1057         return OfMatchToSALMatchConvertor(swMatch.getMatchEntry(), datapathid, ofVersion);
1058     }
1059
1060     private static MatchBuilder OfMatchToSALMatchConvertor(final List<MatchEntry> swMatchList, final BigInteger datapathid,
1061                                                            final OpenflowVersion ofVersion) {
1062
1063         MatchBuilder matchBuilder = new MatchBuilder();
1064         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
1065         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
1066         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
1067         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
1068         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
1069         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
1070         Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder();
1071         Icmpv6MatchBuilder icmpv6MatchBuilder = new Icmpv6MatchBuilder();
1072         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
1073         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
1074         Ipv6MatchBuilder ipv6MatchBuilder = new Ipv6MatchBuilder();
1075         ProtocolMatchFieldsBuilder protocolMatchFieldsBuilder = new ProtocolMatchFieldsBuilder();
1076         TcpFlagMatchBuilder tcpFlagMatchBuilder = new TcpFlagMatchBuilder();
1077         TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
1078
1079         for (MatchEntry ofMatch : swMatchList) {
1080
1081             if (ofMatch.getOxmMatchField().equals(InPort.class)) {
1082                 PortNumber portNumber = ((InPortCase) ofMatch.getMatchEntryValue()).getInPort().getPortNumber();
1083                 if (portNumber != null) {
1084                     matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid, portNumber.getValue(), ofVersion));
1085                 }
1086             } else if (ofMatch.getOxmMatchField().equals(InPhyPort.class)) {
1087                 PortNumber portNumber = ((InPhyPortCase) ofMatch.getMatchEntryValue()).getInPhyPort().getPortNumber();
1088                 matchBuilder.setInPhyPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
1089                         portNumber.getValue(), ofVersion));
1090             } else if (ofMatch.getOxmMatchField().equals(Metadata.class)) {
1091                 MetadataBuilder metadataBuilder = new MetadataBuilder();
1092                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.metadata._case.Metadata metadata = ((MetadataCase) ofMatch.getMatchEntryValue()).getMetadata();
1093                 if (metadata != null) {
1094                     metadataBuilder.setMetadata(new BigInteger(OFConstants.SIGNUM_UNSIGNED, metadata.getMetadata()));
1095                     byte[] metadataMask = metadata.getMask();
1096                     if (metadataMask != null) {
1097                         metadataBuilder.setMetadataMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, metadataMask));
1098                     }
1099                     matchBuilder.setMetadata(metadataBuilder.build());
1100                 }
1101             } else if (ofMatch.getOxmMatchField().equals(EthSrc.class)) {
1102                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.eth.src._case.EthSrc ethSrcCase = ((EthSrcCase) ofMatch.getMatchEntryValue()).getEthSrc();
1103                 if (ethSrcCase != null) {
1104                     EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
1105                     ethSourceBuilder.setAddress(ethSrcCase.getMacAddress());
1106                     byte[] mask = ethSrcCase.getMask();
1107                     if (mask != null) {
1108                         ethSourceBuilder.setMask(new MacAddress(macAddressToString(mask)));
1109                     }
1110                     ethMatchBuilder.setEthernetSource(ethSourceBuilder.build());
1111                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
1112                 }
1113             } else if (ofMatch.getOxmMatchField().equals(EthDst.class)) {
1114                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.eth.dst._case.EthDst ethDstCase = ((EthDstCase) ofMatch.getMatchEntryValue()).getEthDst();
1115                 if (ethDstCase != null) {
1116                     EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
1117                     ethDestinationBuilder.setAddress(ethDstCase.getMacAddress());
1118                     byte[] destinationMask = ethDstCase.getMask();
1119                     if (destinationMask != null) {
1120                         ethDestinationBuilder.setMask(new MacAddress(macAddressToString(destinationMask)));
1121                     }
1122                     ethMatchBuilder.setEthernetDestination(ethDestinationBuilder.build());
1123                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
1124                 }
1125             } else if (ofMatch.getOxmMatchField().equals(EthType.class)) {
1126                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.eth.type._case.EthType ethTypeCase = ((EthTypeCase) ofMatch.getMatchEntryValue()).getEthType();
1127                 if (ethTypeCase != null) {
1128                     EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1129                     ethTypeBuilder
1130                             .setType(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType(
1131                                     (long) ethTypeCase.getEthType().getValue()));
1132                     ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
1133                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
1134                 }
1135             } else if (ofMatch.getOxmMatchField().equals(VlanVid.class)) {
1136                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.vid._case.VlanVid vlanVid = ((VlanVidCase) ofMatch.getMatchEntryValue()).getVlanVid();
1137                 if (vlanVid != null) {
1138                     VlanIdBuilder vlanBuilder = new VlanIdBuilder();
1139                     vlanBuilder.setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(
1140                             vlanVid.getVlanVid()))
1141                             .setVlanIdPresent(vlanVid.isCfiBit());
1142                     vlanBuilder.setVlanIdPresent(vlanVid.isCfiBit());
1143                     vlanMatchBuilder.setVlanId(vlanBuilder.build());
1144
1145                     matchBuilder.setVlanMatch(vlanMatchBuilder.build());
1146                 }
1147             } else if (ofMatch.getOxmMatchField().equals(VlanPcp.class)) {
1148                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.pcp._case.VlanPcp vlanPcp = ((VlanPcpCase) ofMatch.getMatchEntryValue()).getVlanPcp();
1149                 if (vlanPcp != null) {
1150                     vlanMatchBuilder
1151                             .setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
1152                                     vlanPcp.getVlanPcp()));
1153                     matchBuilder.setVlanMatch(vlanMatchBuilder.build());
1154                 }
1155             } else if (ofMatch.getOxmMatchField().equals(IpDscp.class)) {
1156                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.dscp._case.IpDscp ipDscp = ((IpDscpCase) ofMatch.getMatchEntryValue()).getIpDscp();
1157                 if (ipDscp != null) {
1158                     ipMatchBuilder.setIpDscp(new Dscp(ipDscp.getDscp().getValue()));
1159                     matchBuilder.setIpMatch(ipMatchBuilder.build());
1160                 }
1161             } else if (ofMatch.getOxmMatchField().equals(IpEcn.class)) {
1162                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.ecn._case.IpEcn ipEcn = ((IpEcnCase) ofMatch.getMatchEntryValue()).getIpEcn();
1163                 if (ipEcn != null) {
1164                     ipMatchBuilder.setIpEcn(ipEcn.getEcn());
1165                     matchBuilder.setIpMatch(ipMatchBuilder.build());
1166                 }
1167             } else if (ofMatch.getOxmMatchField().equals(IpProto.class)) {
1168                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.proto._case.IpProto ipProto = ((IpProtoCase) ofMatch.getMatchEntryValue()).getIpProto();
1169                 Short protocolNumber = ipProto.getProtocolNumber();
1170                 if (protocolNumber != null) {
1171                     ipMatchBuilder.setIpProtocol(protocolNumber);
1172                     matchBuilder.setIpMatch(ipMatchBuilder.build());
1173                 }
1174             } else if (ofMatch.getOxmMatchField().equals(TcpSrc.class)) {
1175                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tcp.src._case.TcpSrc tcpSrc = ((TcpSrcCase) ofMatch.getMatchEntryValue()).getTcpSrc();
1176                 if (tcpSrc != null) {
1177                     tcpMatchBuilder.setTcpSourcePort(tcpSrc.getPort());
1178                     matchBuilder.setLayer4Match(tcpMatchBuilder.build());
1179                 }
1180             } else if (ofMatch.getOxmMatchField().equals(TcpDst.class)) {
1181                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tcp.dst._case.TcpDst tcpDst = ((TcpDstCase) ofMatch.getMatchEntryValue()).getTcpDst();
1182                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber portNumber = tcpDst.getPort();
1183                 if (portNumber != null) {
1184                     tcpMatchBuilder.setTcpDestinationPort(portNumber);
1185                     matchBuilder.setLayer4Match(tcpMatchBuilder.build());
1186                 }
1187             } else if (ofMatch.getOxmMatchField().equals(UdpSrc.class)) {
1188                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.udp.src._case.UdpSrc udpSrc = ((UdpSrcCase) ofMatch.getMatchEntryValue()).getUdpSrc();
1189                 if (udpSrc != null) {
1190                     udpMatchBuilder.setUdpSourcePort(udpSrc.getPort());
1191                     matchBuilder.setLayer4Match(udpMatchBuilder.build());
1192                 }
1193             } else if (ofMatch.getOxmMatchField().equals(UdpDst.class)) {
1194                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.udp.dst._case.UdpDst udpDst = ((UdpDstCase) ofMatch.getMatchEntryValue()).getUdpDst();
1195                 if (udpDst != null) {
1196                     udpMatchBuilder.setUdpDestinationPort(udpDst.getPort());
1197                     matchBuilder.setLayer4Match(udpMatchBuilder.build());
1198                 }
1199             } else if (ofMatch.getOxmMatchField().equals(SctpSrc.class)) {
1200                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.sctp.src._case.SctpSrc sctpSrc = ((SctpSrcCase) ofMatch.getMatchEntryValue()).getSctpSrc();
1201                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber portNumber = sctpSrc.getPort();
1202                 if (portNumber != null) {
1203                     sctpMatchBuilder.setSctpSourcePort(portNumber);
1204                     matchBuilder.setLayer4Match(sctpMatchBuilder.build());
1205                 }
1206             } else if (ofMatch.getOxmMatchField().equals(SctpDst.class)) {
1207                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.sctp.dst._case.SctpDst sctpDst = ((SctpDstCase) ofMatch.getMatchEntryValue()).getSctpDst();
1208                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber portNumber = sctpDst.getPort();
1209                 if (portNumber != null) {
1210                     sctpMatchBuilder.setSctpDestinationPort(portNumber);
1211                     matchBuilder.setLayer4Match(sctpMatchBuilder.build());
1212                 }
1213             } else if (ofMatch.getOxmMatchField().equals(Icmpv4Type.class)) {
1214                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.type._case.Icmpv4Type icmpv4Type = ((Icmpv4TypeCase) ofMatch.getMatchEntryValue()).getIcmpv4Type();
1215                 Short type = icmpv4Type.getIcmpv4Type();
1216                 if (type != null) {
1217                     icmpv4MatchBuilder.setIcmpv4Type(type);
1218                     matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
1219                 }
1220             } else if (ofMatch.getOxmMatchField().equals(Icmpv4Code.class)) {
1221                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.code._case.Icmpv4Code icmpv4Code = ((Icmpv4CodeCase) ofMatch.getMatchEntryValue()).getIcmpv4Code();
1222                 Short v4code = icmpv4Code.getIcmpv4Code();
1223                 if (v4code != null) {
1224                     icmpv4MatchBuilder.setIcmpv4Code(v4code);
1225                     matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
1226                 }
1227             } else if (ofMatch.getOxmMatchField().equals(Icmpv6Type.class)) {
1228                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv6.type._case.Icmpv6Type icmpv6Type = ((Icmpv6TypeCase) ofMatch.getMatchEntryValue()).getIcmpv6Type();
1229                 Short v6type = icmpv6Type.getIcmpv6Type();
1230                 if (v6type != null) {
1231                     icmpv6MatchBuilder.setIcmpv6Type(v6type);
1232                     matchBuilder.setIcmpv6Match(icmpv6MatchBuilder.build());
1233                 }
1234             } else if (ofMatch.getOxmMatchField().equals(Icmpv6Code.class)) {
1235                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv6.code._case.Icmpv6Code icmpv6Code = ((Icmpv6CodeCase) ofMatch.getMatchEntryValue()).getIcmpv6Code();
1236                 Short v6code = icmpv6Code.getIcmpv6Code();
1237                 if (v6code != null) {
1238                     icmpv6MatchBuilder.setIcmpv6Code(v6code);
1239                     matchBuilder.setIcmpv6Match(icmpv6MatchBuilder.build());
1240                 }
1241             } else if (ofMatch.getOxmMatchField().equals(Ipv4Src.class)) {
1242                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv4.src._case.Ipv4Src ipv4Address = ((Ipv4SrcCase) ofMatch.getMatchEntryValue()).getIpv4Src();
1243                 if (ipv4Address != null) {
1244                     byte[] mask = ipv4Address.getMask();
1245                     String ipv4PrefixStr = ipv4Address.getIpv4Address().getValue();
1246                     setIpv4MatchBuilderFields(ipv4MatchBuilder, ofMatch, mask, ipv4PrefixStr);
1247                     matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
1248                 }
1249             } else if (ofMatch.getOxmMatchField().equals(Ipv4Dst.class)) {
1250                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv4.dst._case.Ipv4Dst ipv4Address = ((Ipv4DstCase) ofMatch.getMatchEntryValue()).getIpv4Dst();
1251                 if (ipv4Address != null) {
1252                     byte[] mask = ipv4Address.getMask();
1253                     String ipv4PrefixStr = ipv4Address.getIpv4Address().getValue();
1254                     setIpv4MatchBuilderFields(ipv4MatchBuilder, ofMatch, mask, ipv4PrefixStr);
1255                     matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
1256                 }
1257             } else if (ofMatch.getOxmMatchField().equals(TunnelIpv4Dst.class)
1258                     || ofMatch.getOxmMatchField().equals(TunnelIpv4Src.class)) {
1259                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv4.dst._case.Ipv4Dst tunnelIpv4Dst = ((Ipv4DstCase) ofMatch.getMatchEntryValue()).getIpv4Dst();
1260                 if (tunnelIpv4Dst != null) {
1261                     String ipv4PrefixStr = tunnelIpv4Dst.getIpv4Address().getValue();
1262                     byte[] mask = tunnelIpv4Dst.getMask();
1263                     ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + ByteBuffer.wrap(tunnelIpv4Dst.getMask()).getInt();
1264                     setIpv4MatchBuilderFields(ipv4MatchBuilder, ofMatch, mask, ipv4PrefixStr);
1265                     matchBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
1266                 }
1267             } else if (ofMatch.getOxmMatchField().equals(TunnelIpv4Src.class)) {
1268                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv4.src._case.Ipv4Src tunnelIpv4Dst = ((Ipv4SrcCase) ofMatch.getMatchEntryValue()).getIpv4Src();
1269                 if (tunnelIpv4Dst != null) {
1270                     String ipv4PrefixStr = tunnelIpv4Dst.getIpv4Address().getValue();
1271                     byte[] mask = tunnelIpv4Dst.getMask();
1272                     ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + ByteBuffer.wrap(tunnelIpv4Dst.getMask()).getInt();
1273                     setIpv4MatchBuilderFields(ipv4MatchBuilder, ofMatch, mask, ipv4PrefixStr);
1274                     matchBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
1275                 }
1276             } else if (ofMatch.getOxmMatchField().equals(ArpOp.class)) {
1277                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.op._case.ArpOp arpOp = ((ArpOpCase) ofMatch.getMatchEntryValue()).getArpOp();
1278                 if (arpOp != null) {
1279                     arpMatchBuilder.setArpOp(arpOp.getOpCode());
1280
1281                     matchBuilder.setLayer3Match(arpMatchBuilder.build());
1282                 }
1283             } else if (ofMatch.getOxmMatchField().equals(ArpSpa.class)) {
1284                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.spa._case.ArpSpa arpSpa = ((ArpSpaCase) ofMatch.getMatchEntryValue()).getArpSpa();
1285                 if (arpSpa != null) {
1286                     int mask = 32;
1287                     if (null != arpSpa.getMask()){
1288                         mask = IpConversionUtil.countBits(arpSpa.getMask());
1289                     }
1290                     Ipv4Prefix ipv4Prefix = IpConversionUtil.createPrefix(arpSpa.getIpv4Address(), mask);
1291                     arpMatchBuilder.setArpSourceTransportAddress(ipv4Prefix);
1292                     matchBuilder.setLayer3Match(arpMatchBuilder.build());
1293                 }
1294             } else if (ofMatch.getOxmMatchField().equals(ArpTpa.class)) {
1295                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.tpa._case.ArpTpa arpTpa = ((ArpTpaCase) ofMatch.getMatchEntryValue()).getArpTpa();
1296                 if (arpTpa != null) {
1297                     int mask = 32;
1298                     if (null != arpTpa.getMask()){
1299                         mask = IpConversionUtil.countBits(arpTpa.getMask());
1300                     }
1301                     Ipv4Prefix ipv4Prefix = IpConversionUtil.createPrefix(arpTpa.getIpv4Address(), mask);
1302
1303                     arpMatchBuilder.setArpTargetTransportAddress(ipv4Prefix);
1304                     matchBuilder.setLayer3Match(arpMatchBuilder.build());
1305                 }
1306             } else if (ofMatch.getOxmMatchField().equals(ArpSha.class)) {
1307                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.sha._case.ArpSha arpSha = ((ArpShaCase) ofMatch.getMatchEntryValue()).getArpSha();
1308                 MacAddress macAddress = arpSha.getMacAddress();
1309                 if (macAddress != null) {
1310                     if (ofMatch.getOxmMatchField().equals(ArpSha.class)) {
1311                         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
1312                         arpSourceHardwareAddressBuilder.setAddress(macAddress);
1313                         byte[] mask = arpSha.getMask();
1314                         if (mask != null) {
1315                             arpSourceHardwareAddressBuilder.setMask(new MacAddress(ByteBufUtils
1316                                     .macAddressToString(mask)));
1317                         }
1318                         arpMatchBuilder.setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder.build());
1319                         matchBuilder.setLayer3Match(arpMatchBuilder.build());
1320                     }
1321                 }
1322             } else if (ofMatch.getOxmMatchField().equals(ArpTha.class)) {
1323                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.tha._case.ArpTha arpTha = ((ArpThaCase) ofMatch.getMatchEntryValue()).getArpTha();
1324                 MacAddress macAddress = arpTha.getMacAddress();
1325                 if (macAddress != null) {
1326                     if (ofMatch.getOxmMatchField().equals(ArpTha.class)) {
1327                         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
1328                         arpTargetHardwareAddressBuilder.setAddress(macAddress);
1329                         byte[] mask = arpTha.getMask();
1330                         if (mask != null) {
1331                             arpTargetHardwareAddressBuilder.setMask(new MacAddress(ByteBufUtils
1332                                     .macAddressToString(mask)));
1333                         }
1334                         arpMatchBuilder.setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder.build());
1335                         matchBuilder.setLayer3Match(arpMatchBuilder.build());
1336                     }
1337                 }
1338             } else if (ofMatch.getOxmMatchField().equals(Ipv6Src.class)) {
1339                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.src._case.Ipv6Src ipv6Src = ((Ipv6SrcCase) ofMatch.getMatchEntryValue()).getIpv6Src();
1340
1341                 if (ipv6Src != null) {
1342                     String ipv6PrefixStr = ipv6Src.getIpv6Address().getValue();
1343                     byte[] mask = ipv6Src.getMask();
1344                     setIpv6MatchBuilderFields(ipv6MatchBuilder, ofMatch, ipv6PrefixStr, mask);
1345                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1346                 }
1347             } else if (ofMatch.getOxmMatchField().equals(Ipv6Dst.class)) {
1348                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.dst._case.Ipv6Dst ipv6Dst = ((Ipv6DstCase) ofMatch.getMatchEntryValue()).getIpv6Dst();
1349
1350                 if (ipv6Dst != null) {
1351                     String ipv6PrefixStr = ipv6Dst.getIpv6Address().getValue();
1352                     byte[] mask = ipv6Dst.getMask();
1353                     setIpv6MatchBuilderFields(ipv6MatchBuilder, ofMatch, ipv6PrefixStr, mask);
1354                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1355                 }
1356             } else if (ofMatch.getOxmMatchField().equals(Ipv6Flabel.class)) {
1357                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.flabel._case.Ipv6Flabel ipv6Flabel = ((Ipv6FlabelCase) ofMatch.getMatchEntryValue()).getIpv6Flabel();
1358                 if (ipv6Flabel != null) {
1359                     Ipv6LabelBuilder ipv6LabelBuilder = new Ipv6LabelBuilder();
1360                     ipv6LabelBuilder.setIpv6Flabel(new Ipv6FlowLabel(ipv6Flabel.getIpv6Flabel()));
1361                     byte[] mask = ipv6Flabel.getMask();
1362                     if (mask != null) {
1363                         ipv6LabelBuilder.setFlabelMask(new Ipv6FlowLabel(Long.valueOf(ByteUtil
1364                                 .bytesToUnsignedInt(mask))));
1365                     }
1366                     ipv6MatchBuilder.setIpv6Label(ipv6LabelBuilder.build());
1367                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1368                 }
1369             } else if (ofMatch.getOxmMatchField().equals(Ipv6NdTarget.class)) {
1370                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.nd.target._case.Ipv6NdTarget ipv6NdTarget = ((Ipv6NdTargetCase) ofMatch.getMatchEntryValue()).getIpv6NdTarget();
1371                 if (ipv6NdTarget != null) {
1372                     ipv6MatchBuilder.setIpv6NdTarget(ipv6NdTarget.getIpv6Address());
1373                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1374                 }
1375             } else if (ofMatch.getOxmMatchField().equals(Ipv6NdSll.class)) {
1376                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.nd.sll._case.Ipv6NdSll ipv6NdSll = ((Ipv6NdSllCase) ofMatch.getMatchEntryValue()).getIpv6NdSll();
1377                 if (ipv6NdSll != null) {
1378                     ipv6MatchBuilder.setIpv6NdSll(ipv6NdSll.getMacAddress());
1379                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1380                 }
1381             } else if (ofMatch.getOxmMatchField().equals(Ipv6NdTll.class)) {
1382                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.nd.tll._case.Ipv6NdTll ipv6NdTll = ((Ipv6NdTllCase) ofMatch.getMatchEntryValue()).getIpv6NdTll();
1383                 if (ipv6NdTll != null) {
1384                     ipv6MatchBuilder.setIpv6NdTll(ipv6NdTll.getMacAddress());
1385                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1386                 }
1387             } else if (ofMatch.getOxmMatchField().equals(Ipv6Exthdr.class)) {
1388                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.exthdr._case.Ipv6Exthdr ipv6Exthdr = ((Ipv6ExthdrCase) ofMatch.getMatchEntryValue()).getIpv6Exthdr();
1389                 if (ipv6Exthdr != null) {
1390                     Ipv6ExtHeaderBuilder ipv6ExtHeaderBuilder = new Ipv6ExtHeaderBuilder();
1391
1392                     Ipv6ExthdrFlags pField = ipv6Exthdr.getPseudoField();
1393                     Integer bitmap = MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField);
1394
1395                     ipv6ExtHeaderBuilder.setIpv6Exthdr(bitmap);
1396                     byte[] mask = ipv6Exthdr.getMask();
1397                     if (mask != null) {
1398                         ipv6ExtHeaderBuilder.setIpv6ExthdrMask(ByteUtil.bytesToUnsignedShort(mask));
1399                     }
1400                     ipv6MatchBuilder.setIpv6ExtHeader(ipv6ExtHeaderBuilder.build());
1401                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1402                 }
1403             } else if (ofMatch.getOxmMatchField().equals(MplsLabel.class)) {
1404                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.mpls.label._case.MplsLabel mplsLabel = ((MplsLabelCase) ofMatch.getMatchEntryValue()).getMplsLabel();
1405                 if (mplsLabel != null) {
1406                     protocolMatchFieldsBuilder.setMplsLabel(mplsLabel.getMplsLabel());
1407                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
1408                 }
1409             } else if (ofMatch.getOxmMatchField().equals(MplsBos.class)) {
1410                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.mpls.bos._case.MplsBos mplsBos = ((MplsBosCase) ofMatch.getMatchEntryValue()).getMplsBos();
1411                 if (mplsBos != null) {
1412                     protocolMatchFieldsBuilder.setMplsBos(mplsBos.isBos() ? (short) 1 : (short) 0);
1413                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
1414                 }
1415             } else if (ofMatch.getOxmMatchField().equals(MplsTc.class)) {
1416                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.mpls.tc._case.MplsTc mplsTc = ((MplsTcCase) ofMatch.getMatchEntryValue()).getMplsTc();
1417                 if (mplsTc != null) {
1418                     protocolMatchFieldsBuilder.setMplsTc(mplsTc.getTc());
1419                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
1420                 }
1421             } else if (ofMatch.getOxmMatchField().equals(PbbIsid.class)) {
1422                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.pbb.isid._case.PbbIsid pbbIsid = ((PbbIsidCase) ofMatch.getMatchEntryValue()).getPbbIsid();
1423                 if (pbbIsid != null) {
1424                     PbbBuilder pbbBuilder = new PbbBuilder();
1425                     pbbBuilder.setPbbIsid(pbbIsid.getIsid());
1426                     byte[] mask = pbbIsid.getMask();
1427                     if (mask != null) {
1428                         pbbBuilder.setPbbMask(ByteUtil.bytesToUnsignedMedium(mask));
1429                     }
1430                     protocolMatchFieldsBuilder.setPbb(pbbBuilder.build());
1431                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
1432                 }
1433             } else if (ofMatch.getOxmMatchField().equals(TunnelId.class)) {
1434                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tunnel.id._case.TunnelId tunnelId = ((TunnelIdCase) ofMatch.getMatchEntryValue()).getTunnelId();
1435                 TunnelBuilder tunnelBuilder = new TunnelBuilder();
1436                 if (tunnelId.getTunnelId()!= null) {
1437                     tunnelBuilder.setTunnelId(new BigInteger(OFConstants.SIGNUM_UNSIGNED, tunnelId.getTunnelId()));
1438                     byte[] mask = tunnelId.getMask();
1439                     if (null != mask) {
1440                         tunnelBuilder.setTunnelMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, mask));
1441                     }
1442                     matchBuilder.setTunnel(tunnelBuilder.build());
1443                 }
1444             }
1445         }
1446         return matchBuilder;
1447     }
1448
1449     private static void setIpv6MatchBuilderFields(final Ipv6MatchBuilder ipv6MatchBuilder, final MatchEntry ofMatch, final String ipv6PrefixStr, final byte[] mask) {
1450         Ipv6Prefix ipv6Prefix;
1451
1452         if (mask != null) {
1453             ipv6Prefix = IpConversionUtil.createPrefix(new Ipv6Address(ipv6PrefixStr), mask);
1454         } else {
1455             ipv6Prefix = IpConversionUtil.createPrefix(new Ipv6Address(ipv6PrefixStr));
1456         }
1457
1458         if (ofMatch.getOxmMatchField().equals(Ipv6Src.class)) {
1459             ipv6MatchBuilder.setIpv6Source(ipv6Prefix);
1460         }
1461         if (ofMatch.getOxmMatchField().equals(Ipv6Dst.class)) {
1462             ipv6MatchBuilder.setIpv6Destination(ipv6Prefix);
1463         }
1464     }
1465
1466     private static void setIpv4MatchBuilderFields(final Ipv4MatchBuilder ipv4MatchBuilder, final MatchEntry ofMatch, final byte[] mask, final String ipv4PrefixStr) {
1467         Ipv4Prefix ipv4Prefix;
1468         if (mask != null) {
1469             ipv4Prefix = IpConversionUtil.createPrefix(new Ipv4Address(ipv4PrefixStr), mask);
1470         } else {
1471             //Openflow Spec : 1.3.2
1472             //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
1473             // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
1474             // statistics response.
1475             ipv4Prefix = IpConversionUtil.createPrefix(new Ipv4Address(ipv4PrefixStr));
1476         }
1477         if (ofMatch.getOxmMatchField().equals(Ipv4Src.class)) {
1478             ipv4MatchBuilder.setIpv4Source(ipv4Prefix);
1479         }
1480         if (ofMatch.getOxmMatchField().equals(Ipv4Dst.class)) {
1481             ipv4MatchBuilder.setIpv4Destination(ipv4Prefix);
1482         }
1483     }
1484
1485
1486     private static MatchEntry toOfMplsPbb(final Pbb pbb) {
1487         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1488         boolean hasmask = false;
1489         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1490         matchEntryBuilder.setOxmMatchField(PbbIsid.class);
1491         PbbIsidCaseBuilder pbbIsidCaseBuilder = new PbbIsidCaseBuilder();
1492         PbbIsidBuilder pbbIsidBuilder = new PbbIsidBuilder();
1493         pbbIsidBuilder.setIsid(pbb.getPbbIsid());
1494         if (pbb.getPbbMask() != null) {
1495             hasmask = true;
1496             pbbIsidBuilder.setMask(ByteUtil.unsignedMediumToBytes(pbb.getPbbMask()));
1497         }
1498         pbbIsidCaseBuilder.setPbbIsid(pbbIsidBuilder.build());
1499         matchEntryBuilder.setMatchEntryValue(pbbIsidCaseBuilder.build());
1500         matchEntryBuilder.setHasMask(hasmask);
1501         return matchEntryBuilder.build();
1502     }
1503
1504     private static MatchEntry toOfMplsTc(final Short mplsTc) {
1505         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1506         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1507         matchEntryBuilder.setHasMask(false);
1508         matchEntryBuilder.setOxmMatchField(MplsTc.class);
1509         MplsTcCaseBuilder mplsTcCaseBuilder = new MplsTcCaseBuilder();
1510         MplsTcBuilder mplsTcBuilder = new MplsTcBuilder();
1511         mplsTcBuilder.setTc(mplsTc);
1512         mplsTcCaseBuilder.setMplsTc(mplsTcBuilder.build());
1513         matchEntryBuilder.setMatchEntryValue(mplsTcCaseBuilder.build());
1514         return matchEntryBuilder.build();
1515     }
1516
1517     private static MatchEntry toOfMplsBos(final Short mplsBos) {
1518         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1519         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1520         matchEntryBuilder.setHasMask(false);
1521         matchEntryBuilder.setOxmMatchField(MplsBos.class);
1522         MplsBosCaseBuilder mplsBosCaseBuilder = new MplsBosCaseBuilder();
1523         MplsBosBuilder mplsBosBuilder = new MplsBosBuilder();
1524         boolean isBos = false;
1525         if (mplsBos.shortValue() != 0) {
1526             isBos = true;
1527         }
1528         mplsBosBuilder.setBos(isBos);
1529         mplsBosCaseBuilder.setMplsBos(mplsBosBuilder.build());
1530
1531         matchEntryBuilder.setMatchEntryValue(mplsBosCaseBuilder.build());
1532         return matchEntryBuilder.build();
1533     }
1534
1535     private static MatchEntry toOfMplsLabel(final Long mplsLabel) {
1536         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1537         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1538         matchEntryBuilder.setHasMask(false);
1539         matchEntryBuilder.setOxmMatchField(MplsLabel.class);
1540
1541         MplsLabelCaseBuilder mplsLabelCaseBuilder = new MplsLabelCaseBuilder();
1542         MplsLabelBuilder mplsLabelBuilder = new MplsLabelBuilder();
1543         mplsLabelBuilder.setMplsLabel(mplsLabel);
1544         mplsLabelCaseBuilder.setMplsLabel(mplsLabelBuilder.build());
1545         matchEntryBuilder.setMatchEntryValue(mplsLabelCaseBuilder.build());
1546         return matchEntryBuilder.build();
1547     }
1548
1549     private static MatchEntry toOfIpv6ExtHeader(final Ipv6ExtHeader ipv6ExtHeader) {
1550         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1551         boolean hasmask = false;
1552         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1553         matchEntryBuilder.setOxmMatchField(Ipv6Exthdr.class);
1554         Ipv6ExthdrCaseBuilder ipv6ExthdrCaseBuilder = new Ipv6ExthdrCaseBuilder();
1555         Ipv6ExthdrBuilder ipv6ExthdrBuilder = new Ipv6ExthdrBuilder();
1556
1557         Integer bitmap = ipv6ExtHeader.getIpv6Exthdr();
1558         final Boolean NONEXT = ((bitmap) & (1 << 0)) != 0;
1559         final Boolean ESP = ((bitmap) & (1 << 1)) != 0;
1560         final Boolean AUTH = ((bitmap) & (1 << 2)) != 0;
1561         final Boolean DEST = ((bitmap) & (1 << 3)) != 0;
1562         final Boolean FRAG = ((bitmap) & (1 << 4)) != 0;
1563         final Boolean ROUTER = ((bitmap) & (1 << 5)) != 0;
1564         final Boolean HOP = ((bitmap) & (1 << 6)) != 0;
1565         final Boolean UNREP = ((bitmap) & (1 << 7)) != 0;
1566         final Boolean UNSEQ = ((bitmap) & (1 << 8)) != 0;
1567
1568         ipv6ExthdrBuilder.setPseudoField(new Ipv6ExthdrFlags(AUTH, DEST, ESP, FRAG, HOP, NONEXT, ROUTER, UNREP, UNSEQ));
1569         //TODO ipv6ExthdrBuilder.setMask()
1570         if (ipv6ExtHeader.getIpv6ExthdrMask() != null) {
1571             hasmask = true;
1572             ipv6ExthdrBuilder.setMask(ByteUtil.unsignedShortToBytes(ipv6ExtHeader.getIpv6ExthdrMask()));
1573         }
1574         ipv6ExthdrCaseBuilder.setIpv6Exthdr(ipv6ExthdrBuilder.build());
1575         matchEntryBuilder.setMatchEntryValue(ipv6ExthdrCaseBuilder.build());
1576         matchEntryBuilder.setHasMask(hasmask);
1577         return matchEntryBuilder.build();
1578     }
1579
1580     private static MatchEntry toOfIpv6FlowLabel(final Ipv6Label ipv6Label) {
1581         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1582         boolean hasmask = false;
1583         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1584         matchEntryBuilder.setOxmMatchField(Ipv6Flabel.class);
1585         Ipv6FlabelCaseBuilder ipv6FlabelCaseBuilder = new Ipv6FlabelCaseBuilder();
1586         Ipv6FlabelBuilder ipv6FlabelBuilder = new Ipv6FlabelBuilder();
1587         ipv6FlabelBuilder.setIpv6Flabel(ipv6Label.getIpv6Flabel());
1588         if (ipv6Label.getFlabelMask() != null) {
1589             hasmask = true;
1590             ipv6FlabelBuilder.setMask(ByteUtil.unsignedIntToBytes(ipv6Label.getFlabelMask().getValue()));
1591         }
1592         ipv6FlabelCaseBuilder.setIpv6Flabel(ipv6FlabelBuilder.build());
1593         matchEntryBuilder.setMatchEntryValue(ipv6FlabelCaseBuilder.build());
1594         matchEntryBuilder.setHasMask(hasmask);
1595         return matchEntryBuilder.build();
1596     }
1597
1598     private static MatchEntry toOfPort(final Class<? extends MatchField> field, final Long portNumber) {
1599         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1600         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1601         matchEntryBuilder.setHasMask(false);
1602         matchEntryBuilder.setOxmMatchField(field);
1603         InPortCaseBuilder caseBuilder = new InPortCaseBuilder();
1604         InPortBuilder portBuilder = new InPortBuilder();
1605         portBuilder.setPortNumber(new PortNumber(portNumber));
1606         caseBuilder.setInPort(portBuilder.build());
1607         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
1608
1609         return matchEntryBuilder.build();
1610     }
1611
1612     private static MatchEntry toOfPhyPort(final Class<? extends MatchField> field, final Long portNumber) {
1613         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1614         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1615         matchEntryBuilder.setHasMask(false);
1616         matchEntryBuilder.setOxmMatchField(field);
1617         InPhyPortCaseBuilder caseBuilder = new InPhyPortCaseBuilder();
1618         InPhyPortBuilder portBuilder = new InPhyPortBuilder();
1619         portBuilder.setPortNumber(new PortNumber(portNumber));
1620         caseBuilder.setInPhyPort(portBuilder.build());
1621         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
1622
1623         return matchEntryBuilder.build();
1624     }
1625
1626     private static MatchEntry toOfMetadata(final Class<? extends MatchField> field, final BigInteger metadata,
1627                                            final BigInteger metadataMask) {
1628         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1629         boolean hasmask = false;
1630         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1631         matchEntryBuilder.setOxmMatchField(field);
1632         MetadataCaseBuilder metadataCaseBuilder = new MetadataCaseBuilder();
1633         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.metadata._case.MetadataBuilder metadataBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.metadata._case.MetadataBuilder();
1634         metadataBuilder.setMetadata(ByteUtil.convertBigIntegerToNBytes(metadata, OFConstants.SIZE_OF_LONG_IN_BYTES));
1635         if (metadataMask != null) {
1636             hasmask = true;
1637             metadataBuilder.setMask(ByteUtil.convertBigIntegerToNBytes(metadataMask, OFConstants.SIZE_OF_LONG_IN_BYTES));
1638         }
1639         metadataCaseBuilder.setMetadata(metadataBuilder.build());
1640         matchEntryBuilder.setMatchEntryValue(metadataCaseBuilder.build());
1641         matchEntryBuilder.setHasMask(hasmask);
1642         return matchEntryBuilder.build();
1643     }
1644
1645     private static MatchEntry toOfEthernetType(final EthernetType ethernetType) {
1646         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1647         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1648         matchEntryBuilder.setHasMask(false);
1649         matchEntryBuilder.setOxmMatchField(EthType.class);
1650         EthTypeCaseBuilder ethTypeCaseBuilder = new EthTypeCaseBuilder();
1651         EthTypeBuilder ethTypeBuilder = new EthTypeBuilder();
1652         EtherType etherType = new EtherType(ethernetType.getType().getValue().intValue());
1653         ethTypeBuilder.setEthType(etherType);
1654         ethTypeCaseBuilder.setEthType(ethTypeBuilder.build());
1655         matchEntryBuilder.setMatchEntryValue(ethTypeCaseBuilder.build());
1656         return matchEntryBuilder.build();
1657     }
1658
1659     private static MatchEntry toOfIcmpv4Type(final Short icmpv4Type) {
1660         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1661         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1662         matchEntryBuilder.setHasMask(false);
1663         matchEntryBuilder.setOxmMatchField(Icmpv4Type.class);
1664         Icmpv4TypeCaseBuilder icmpv4TypeCaseBuilder = new Icmpv4TypeCaseBuilder();
1665         Icmpv4TypeBuilder icmpv4TypeBuilder = new Icmpv4TypeBuilder();
1666         icmpv4TypeBuilder.setIcmpv4Type(icmpv4Type);
1667         icmpv4TypeCaseBuilder.setIcmpv4Type(icmpv4TypeBuilder.build());
1668         matchEntryBuilder.setMatchEntryValue(icmpv4TypeCaseBuilder.build());
1669         return matchEntryBuilder.build();
1670     }
1671
1672     private static MatchEntry toOfIcmpv4Code(final Short icmpv4Code) {
1673         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1674         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1675         matchEntryBuilder.setHasMask(false);
1676         matchEntryBuilder.setOxmMatchField(Icmpv4Code.class);
1677         Icmpv4CodeCaseBuilder icmpv4CodeCaseBuilder = new Icmpv4CodeCaseBuilder();
1678         Icmpv4CodeBuilder icmpv4CodeBuilder = new Icmpv4CodeBuilder();
1679         icmpv4CodeBuilder.setIcmpv4Code(icmpv4Code);
1680         icmpv4CodeCaseBuilder.setIcmpv4Code(icmpv4CodeBuilder.build());
1681         matchEntryBuilder.setMatchEntryValue(icmpv4CodeCaseBuilder.build());
1682         return matchEntryBuilder.build();
1683     }
1684
1685     private static MatchEntry toOfIcmpv6Type(final Short icmpv6Type) {
1686         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1687         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1688         matchEntryBuilder.setHasMask(false);
1689         matchEntryBuilder.setOxmMatchField(Icmpv6Type.class);
1690         Icmpv6TypeCaseBuilder icmpv6TypeCaseBuilder = new Icmpv6TypeCaseBuilder();
1691         Icmpv6TypeBuilder icmpv6TypeBuilder = new Icmpv6TypeBuilder();
1692         icmpv6TypeBuilder.setIcmpv6Type(icmpv6Type);
1693         icmpv6TypeCaseBuilder.setIcmpv6Type(icmpv6TypeBuilder.build());
1694         matchEntryBuilder.setMatchEntryValue(icmpv6TypeCaseBuilder.build());
1695         return matchEntryBuilder.build();
1696     }
1697
1698     private static MatchEntry toOfIcmpv6Code(final Short icmpv6Code) {
1699         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1700         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1701         matchEntryBuilder.setHasMask(false);
1702         matchEntryBuilder.setOxmMatchField(Icmpv6Code.class);
1703         Icmpv6CodeCaseBuilder icmpv6CodeCaseBuilder = new Icmpv6CodeCaseBuilder();
1704         Icmpv6CodeBuilder icmpv6CodeBuilder = new Icmpv6CodeBuilder();
1705         icmpv6CodeBuilder.setIcmpv6Code(icmpv6Code);
1706         icmpv6CodeCaseBuilder.setIcmpv6Code(icmpv6CodeBuilder.build());
1707         matchEntryBuilder.setMatchEntryValue(icmpv6CodeCaseBuilder.build());
1708         return matchEntryBuilder.build();
1709     }
1710
1711     public static MatchEntry toOfIpDscp(final Dscp ipDscp) {
1712         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1713         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1714         matchEntryBuilder.setHasMask(false);
1715         matchEntryBuilder.setOxmMatchField(IpDscp.class);
1716
1717         IpDscpCaseBuilder ipDscpCaseBuilder = new IpDscpCaseBuilder();
1718         IpDscpBuilder ipDscpBuilder = new IpDscpBuilder();
1719         ipDscpBuilder.setDscp(ipDscp);
1720         ipDscpCaseBuilder.setIpDscp(ipDscpBuilder.build());
1721         matchEntryBuilder.setMatchEntryValue(ipDscpCaseBuilder.build());
1722         return matchEntryBuilder.build();
1723     }
1724
1725     public static MatchEntry toOfVlanPcp(
1726             final org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp vlanPcp) {
1727         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1728         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1729         matchEntryBuilder.setHasMask(false);
1730         matchEntryBuilder.setOxmMatchField(VlanPcp.class);
1731         VlanPcpCaseBuilder vlanPcpCaseBuilder = new VlanPcpCaseBuilder();
1732         VlanPcpBuilder vlanPcpBuilder = new VlanPcpBuilder();
1733         vlanPcpBuilder.setVlanPcp(vlanPcp.getValue());
1734         vlanPcpCaseBuilder.setVlanPcp(vlanPcpBuilder.build());
1735         matchEntryBuilder.setMatchEntryValue(vlanPcpCaseBuilder.build());
1736         return matchEntryBuilder.build();
1737     }
1738
1739     private static MatchEntry toOfVlanVid(final VlanId vlanId) {
1740         // TODO: verify
1741         boolean hasmask = false;
1742         boolean setCfiBit = false;
1743         Integer vidEntryValue = 0;
1744         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1745         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1746         matchEntryBuilder.setOxmMatchField(VlanVid.class);
1747         VlanVidCaseBuilder vlanVidCaseBuilder = new VlanVidCaseBuilder();
1748         VlanVidBuilder vlanVidBuilder = new VlanVidBuilder();
1749         if (Boolean.TRUE.equals(vlanId.isVlanIdPresent())) {
1750             setCfiBit = true;
1751             if (vlanId.getVlanId() != null) {
1752                 vidEntryValue = vlanId.getVlanId().getValue();
1753             }
1754             hasmask = (vidEntryValue == 0);
1755         }
1756         vlanVidBuilder.setCfiBit(setCfiBit);
1757         vlanVidBuilder.setVlanVid(vidEntryValue);
1758         if (hasmask) {
1759             vlanVidBuilder.setMask(VLAN_VID_MASK);
1760         }
1761         vlanVidCaseBuilder.setVlanVid(vlanVidBuilder.build());
1762         matchEntryBuilder.setMatchEntryValue(vlanVidCaseBuilder.build());
1763         matchEntryBuilder.setHasMask(hasmask);
1764         return matchEntryBuilder.build();
1765     }
1766
1767     private static MatchEntry toOfIpProto(final Short ipProtocol) {
1768         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1769         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1770         matchEntryBuilder.setHasMask(false);
1771         matchEntryBuilder.setOxmMatchField(IpProto.class);
1772         IpProtoCaseBuilder ipProtoCaseBuilder = new IpProtoCaseBuilder();
1773         IpProtoBuilder ipProtoBuilder = new IpProtoBuilder();
1774         ipProtoBuilder.setProtocolNumber(ipProtocol);
1775         ipProtoCaseBuilder.setIpProto(ipProtoBuilder.build());
1776         matchEntryBuilder.setMatchEntryValue(ipProtoCaseBuilder.build());
1777         return matchEntryBuilder.build();
1778     }
1779
1780     private static MatchEntry toOfIpEcn(final Short ipEcn) {
1781         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1782         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1783         matchEntryBuilder.setHasMask(false);
1784         matchEntryBuilder.setOxmMatchField(IpEcn.class);
1785         IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
1786         IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
1787         ipEcnBuilder.setEcn(ipEcn);
1788         ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
1789         matchEntryBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
1790         return matchEntryBuilder.build();
1791     }
1792
1793     private static MatchEntry toOfArpOpCode(final Integer arpOp) {
1794         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1795         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1796         matchEntryBuilder.setHasMask(false);
1797         matchEntryBuilder.setOxmMatchField(ArpOp.class);
1798         ArpOpCaseBuilder arpOpCaseBuilder = new ArpOpCaseBuilder();
1799         ArpOpBuilder arpOpBuilder = new ArpOpBuilder();
1800         arpOpBuilder.setOpCode(arpOp);
1801         arpOpCaseBuilder.setArpOp(arpOpBuilder.build());
1802         matchEntryBuilder.setMatchEntryValue(arpOpCaseBuilder.build());
1803         return matchEntryBuilder.build();
1804     }
1805
1806     private static MatchEntry toOfIpv6NdTargetAddress(final Ipv6Address address) {
1807         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
1808         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
1809         matchEntryBuilder.setHasMask(false);
1810         matchEntryBuilder.setOxmMatchField(Ipv6NdTarget.class);
1811
1812         Ipv6NdTargetCaseBuilder ipv6NdTargetCaseBuilder = new Ipv6NdTargetCaseBuilder();
1813         Ipv6NdTargetBuilder ipv6NdTargetBuilder = new Ipv6NdTargetBuilder();
1814         ipv6NdTargetBuilder.setIpv6Address(address);
1815         ipv6NdTargetCaseBuilder.setIpv6NdTarget(ipv6NdTargetBuilder.build());
1816         matchEntryBuilder.setMatchEntryValue(ipv6NdTargetCaseBuilder.build());
1817         return matchEntryBuilder.build();
1818     }
1819
1820
1821
1822
1823     /**
1824      * Method converts OF SetField action to SAL SetFiled action.
1825      *
1826      * @param action
1827      * @param ofVersion current ofp version
1828      * @return
1829      */
1830     public static SetField fromOFSetFieldToSALSetFieldAction(
1831             final Action action, final OpenflowVersion ofVersion) {
1832         logger.debug("Converting OF SetField action to SAL SetField action");
1833         SetFieldCase setFieldCase = (SetFieldCase) action.getActionChoice();
1834         SetFieldAction setFieldAction = setFieldCase.getSetFieldAction();
1835
1836         SetFieldBuilder setField = new SetFieldBuilder();
1837         MatchBuilder match = OfMatchToSALMatchConvertor(setFieldAction.getMatchEntry(), null, ofVersion);
1838         setField.fieldsFrom(match.build());
1839         return setField.build();
1840     }
1841
1842
1843 }