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