Bug 2280 - MatchConvertorImpl bugs found during test creation
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorImpl.java
1 /**
2  * Copyright (c) 2013-2014 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 com.google.common.base.Optional;
12 import com.google.common.base.Splitter;
13 import java.math.BigInteger;
14 import java.nio.ByteBuffer;
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18 import org.opendaylight.openflowjava.util.ByteBufUtils;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
21 import org.opendaylight.openflowplugin.extension.api.ConverterExtensionKey;
22 import org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava;
23 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionResolvers;
24 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
25 import org.opendaylight.openflowplugin.openflow.md.util.ActionUtil;
26 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
27 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6FlowLabel;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetField;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestination;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSource;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeader;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeaderBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6Label;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6LabelBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4Match;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6Match;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFields;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFieldsBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatch;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
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.augments.rev131002.BosMatchEntry;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.BosMatchEntryBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DscpMatchEntry;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DscpMatchEntryBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntry;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntryBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntry;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntryBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4CodeMatchEntry;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4CodeMatchEntryBuilder;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4TypeMatchEntry;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4TypeMatchEntryBuilder;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntry;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntryBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6TypeMatchEntry;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6TypeMatchEntryBuilder;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntryBuilder;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntryBuilder;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntry;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntryBuilder;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntry;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntryBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntry;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntryBuilder;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntry;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntryBuilder;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsLabelMatchEntry;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsLabelMatchEntryBuilder;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OpCodeMatchEntry;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OpCodeMatchEntryBuilder;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsAction;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortMatchEntry;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortMatchEntryBuilder;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntryBuilder;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntry;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntryBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntryBuilder;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntry;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntryBuilder;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcpFlagMatchEntry;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntry;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntryBuilder;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntryBuilder;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpOp;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSha;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSpa;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTha;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTpa;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthDst;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthSrc;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthType;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Code;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Type;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Code;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Type;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPhyPort;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpDscp;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpEcn;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpProto;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Dst;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Dst;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Exthdr;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Flabel;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdSll;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTarget;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTll;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Src;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MatchField;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Metadata;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsBos;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsLabel;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsTc;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.PbbIsid;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpDst;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpSrc;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpDst;
179 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpFlag;
180 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpSrc;
181 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelId;
182 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Dst;
183 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Src;
184 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpDst;
185 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpSrc;
186 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanPcp;
187 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanVid;
188 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
189 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
190 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
191 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
192 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralExtensionListGrouping;
193 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
194 import org.slf4j.Logger;
195 import org.slf4j.LoggerFactory;
196
197 /**
198  * Utility class for converting a MD-SAL Flow into the OF flow mod
199  */
200 public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
201     private static final Logger logger = LoggerFactory.getLogger(MatchConvertorImpl.class);
202     static final String PREFIX_SEPARATOR = "/";
203     static final Splitter PREFIX_SPLITTER = Splitter.on('/');
204     private static final byte[] VLAN_VID_MASK = new byte[]{16, 0};
205     private static final short PROTO_TCP = 6;
206     private static final short PROTO_UDP = 17;
207     private static final String noIp = "0.0.0.0/0";
208
209     @Override
210     public List<MatchEntries> convert(
211             final org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match match, final BigInteger datapathid) {
212         List<MatchEntries> matchEntriesList = new ArrayList<>();
213         if (match == null) return matchEntriesList;
214         if (match.getInPort() != null) {
215             //TODO: currently this matchconverter is mapped to OF1.3 in MatchReactorMappingFactory. Will need to revisit during 1.4+
216             matchEntriesList.add(toOfPort(InPort.class,
217                     InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF13, match.getInPort())));
218         }
219
220         if (match.getInPhyPort() != null) {
221             //TODO: currently this matchconverter is mapped to OF1.3 in MatchReactorMappingFactory. Will need to revisit during 1.4+
222             matchEntriesList.add(toOfPort(InPhyPort.class,
223                     InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF13, match.getInPhyPort())));
224         }
225
226         org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Metadata metadata = match
227                 .getMetadata();
228         if (metadata != null) {
229             matchEntriesList.add(toOfMetadata(Metadata.class, metadata.getMetadata(), metadata.getMetadataMask()));
230         }
231
232         EthernetMatch ethernetMatch = match.getEthernetMatch();
233         if (ethernetMatch != null) {
234             EthernetDestination ethernetDestination = ethernetMatch.getEthernetDestination();
235             if (ethernetDestination != null) {
236                 matchEntriesList.add(toOfMacAddress(EthDst.class, ethernetDestination.getAddress(),
237                         ethernetDestination.getMask()));
238             }
239
240             EthernetSource ethernetSource = ethernetMatch.getEthernetSource();
241             if (ethernetSource != null) {
242                 matchEntriesList
243                         .add(toOfMacAddress(EthSrc.class, ethernetSource.getAddress(), ethernetSource.getMask()));
244             }
245
246             if (ethernetMatch.getEthernetType() != null) {
247                 matchEntriesList.add(toOfEthernetType(ethernetMatch.getEthernetType()));
248             }
249         }
250
251         VlanMatch vlanMatch = match.getVlanMatch();
252         if (vlanMatch != null) {
253             if (vlanMatch.getVlanId() != null) {
254                 matchEntriesList.add(toOfVlanVid(vlanMatch.getVlanId()));
255             }
256
257             if (vlanMatch.getVlanPcp() != null) {
258                 matchEntriesList.add(toOfVlanPcp(vlanMatch.getVlanPcp()));
259             }
260         }
261
262         IpMatch ipMatch = match.getIpMatch();
263         if (ipMatch != null) {
264             if (ipMatch.getIpDscp() != null) {
265                 matchEntriesList.add(toOfIpDscp(ipMatch.getIpDscp()));
266             }
267
268             if (ipMatch.getIpEcn() != null) {
269                 matchEntriesList.add(toOfIpEcn(ipMatch.getIpEcn()));
270             }
271
272             if (ipMatch.getIpProtocol() != null) {
273                 matchEntriesList.add(toOfIpProto(ipMatch.getIpProtocol()));
274             }
275
276         }
277
278         Layer4Match layer4Match = match.getLayer4Match();
279         if (layer4Match != null) {
280             if (layer4Match instanceof TcpMatch) {
281                 TcpMatch tcpMatch = (TcpMatch) layer4Match;
282                 if (tcpMatch.getTcpSourcePort() != null) {
283                     matchEntriesList.add(toOfLayer3Port(TcpSrc.class, tcpMatch.getTcpSourcePort()));
284                 }
285
286                 if (tcpMatch.getTcpDestinationPort() != null) {
287                     matchEntriesList.add(toOfLayer3Port(TcpDst.class, tcpMatch.getTcpDestinationPort()));
288                 }
289             } else if (layer4Match instanceof UdpMatch) {
290                 UdpMatch udpMatch = (UdpMatch) layer4Match;
291                 if (udpMatch.getUdpSourcePort() != null) {
292                     matchEntriesList.add(toOfLayer3Port(UdpSrc.class, udpMatch.getUdpSourcePort()));
293                 }
294
295                 if (udpMatch.getUdpDestinationPort() != null) {
296                     matchEntriesList.add(toOfLayer3Port(UdpDst.class, udpMatch.getUdpDestinationPort()));
297                 }
298             } else if (layer4Match instanceof SctpMatch) {
299                 SctpMatch sctpMatch = (SctpMatch) layer4Match;
300                 if (sctpMatch.getSctpSourcePort() != null) {
301                     matchEntriesList.add(toOfLayer3Port(SctpSrc.class, sctpMatch.getSctpSourcePort()));
302                 }
303
304                 if (sctpMatch.getSctpDestinationPort() != null) {
305                     matchEntriesList.add(toOfLayer3Port(SctpDst.class, sctpMatch.getSctpDestinationPort()));
306                 }
307             }
308         }
309
310         Icmpv4Match icmpv4Match = match.getIcmpv4Match();
311         if (icmpv4Match != null) {
312             if (icmpv4Match.getIcmpv4Type() != null) {
313                 matchEntriesList.add(toOfIcmpv4Type(icmpv4Match.getIcmpv4Type()));
314             }
315
316             if (icmpv4Match.getIcmpv4Code() != null) {
317                 matchEntriesList.add(toOfIcmpv4Code(icmpv4Match.getIcmpv4Code()));
318             }
319         }
320
321         Icmpv6Match icmpv6Match = match.getIcmpv6Match();
322         if (icmpv6Match != null) {
323             if (icmpv6Match.getIcmpv6Type() != null) {
324                 matchEntriesList.add(toOfIcmpv6Type(icmpv6Match.getIcmpv6Type()));
325             }
326
327             if (icmpv6Match.getIcmpv6Code() != null) {
328                 matchEntriesList.add(toOfIcmpv6Code(icmpv6Match.getIcmpv6Code()));
329             }
330         }
331
332         Layer3Match layer3Match = match.getLayer3Match();
333         if (layer3Match != null) {
334             if (layer3Match instanceof Ipv4Match) {
335                 Ipv4Match ipv4Match = (Ipv4Match) layer3Match;
336                 if (ipv4Match.getIpv4Source() != null) {
337                     matchEntriesList.add(toOfIpv4Prefix(Ipv4Src.class, ipv4Match.getIpv4Source()));
338                 }
339                 if (ipv4Match.getIpv4Destination() != null) {
340                     matchEntriesList.add(toOfIpv4Prefix(Ipv4Dst.class, ipv4Match.getIpv4Destination()));
341                 }
342             }
343             if (layer3Match instanceof TunnelIpv4Match) {
344                 TunnelIpv4Match tunnelIpv4Src = (TunnelIpv4Match) layer3Match;
345                 if (tunnelIpv4Src.getTunnelIpv4Source() != null) {
346                     matchEntriesList.add(NxmExtensionsConvertor.toNxmIpv4Tunnel(TunnelIpv4Src.class, tunnelIpv4Src.getTunnelIpv4Source()));
347                 }
348                 if (tunnelIpv4Src.getTunnelIpv4Destination() != null) {
349                     matchEntriesList.add(NxmExtensionsConvertor.toNxmIpv4Tunnel(TunnelIpv4Dst.class, tunnelIpv4Src.getTunnelIpv4Destination()));
350                 }
351             } else if (layer3Match instanceof ArpMatch) {
352                 ArpMatch arpMatch = (ArpMatch) layer3Match;
353                 if (arpMatch.getArpOp() != null) {
354                     matchEntriesList.add(toOfArpOpCode(arpMatch.getArpOp()));
355                 }
356
357                 if (arpMatch.getArpSourceTransportAddress() != null) {
358                     matchEntriesList.add(toOfIpv4Prefix(ArpSpa.class, arpMatch.getArpSourceTransportAddress()));
359                 }
360
361                 if (arpMatch.getArpTargetTransportAddress() != null) {
362                     matchEntriesList.add(toOfIpv4Prefix(ArpTpa.class, arpMatch.getArpTargetTransportAddress()));
363                 }
364
365                 ArpSourceHardwareAddress arpSourceHardwareAddress = arpMatch.getArpSourceHardwareAddress();
366                 if (arpSourceHardwareAddress != null) {
367                     matchEntriesList.add(toOfMacAddress(ArpSha.class, arpSourceHardwareAddress.getAddress(),
368                             arpSourceHardwareAddress.getMask()));
369                 }
370
371                 ArpTargetHardwareAddress arpTargetHardwareAddress = arpMatch.getArpTargetHardwareAddress();
372                 if (arpTargetHardwareAddress != null) {
373                     matchEntriesList.add(toOfMacAddress(ArpTha.class, arpTargetHardwareAddress.getAddress(),
374                             arpTargetHardwareAddress.getMask()));
375                 }
376             } else if (layer3Match instanceof Ipv6Match) {
377                 Ipv6Match ipv6Match = (Ipv6Match) layer3Match;
378                 if (ipv6Match.getIpv6Source() != null) {
379                     matchEntriesList.add(toOfIpv6Prefix(Ipv6Src.class, ipv6Match.getIpv6Source()));
380                 }
381
382                 if (ipv6Match.getIpv6Destination() != null) {
383                     matchEntriesList.add(toOfIpv6Prefix(Ipv6Dst.class, ipv6Match.getIpv6Destination()));
384                 }
385
386                 if (ipv6Match.getIpv6Label() != null) {
387                     matchEntriesList.add(toOfIpv6FlowLabel(ipv6Match.getIpv6Label()));
388                 }
389
390                 if (ipv6Match.getIpv6NdTarget() != null) {
391                     matchEntriesList.add(toOfIpv6Address(ipv6Match.getIpv6NdTarget()));
392                 }
393
394                 if (ipv6Match.getIpv6NdSll() != null) {
395                     matchEntriesList.add(toOfMacAddress(Ipv6NdSll.class, ipv6Match.getIpv6NdSll(), null));
396                 }
397
398                 if (ipv6Match.getIpv6NdTll() != null) {
399                     matchEntriesList.add(toOfMacAddress(Ipv6NdTll.class, ipv6Match.getIpv6NdTll(), null));
400                 }
401
402                 if (ipv6Match.getIpv6ExtHeader() != null) {
403                     matchEntriesList.add(toOfIpv6ExtHeader(ipv6Match.getIpv6ExtHeader()));
404                 }
405             }
406         }
407
408         ProtocolMatchFields protocolMatchFields = match.getProtocolMatchFields();
409         if (protocolMatchFields != null) {
410             if (protocolMatchFields.getMplsLabel() != null) {
411                 matchEntriesList.add(toOfMplsLabel(protocolMatchFields.getMplsLabel()));
412             }
413
414             if (protocolMatchFields.getMplsBos() != null) {
415                 matchEntriesList.add(toOfMplsBos(protocolMatchFields.getMplsBos()));
416             }
417
418             if (protocolMatchFields.getMplsTc() != null) {
419                 matchEntriesList.add(toOfMplsTc(protocolMatchFields.getMplsTc()));
420             }
421
422             if (protocolMatchFields.getPbb() != null) {
423                 matchEntriesList.add(toOfMplsPbb(protocolMatchFields.getPbb()));
424             }
425         }
426
427         //FIXME: move to extensible support
428         // TODO: Move to seperate bundle as soon as OF extensibility is supported by ofplugin/java
429         TcpFlagMatch tcpFlagMatch = match.getTcpFlagMatch();
430         if (tcpFlagMatch != null) {
431             if (tcpFlagMatch.getTcpFlag() != null) {
432                 matchEntriesList.add(NxmExtensionsConvertor.toNxmTcpFlag(tcpFlagMatch.getTcpFlag()));
433             }
434         }
435
436         org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Tunnel tunnel = match
437                 .getTunnel();
438         if (tunnel != null) {
439             matchEntriesList.add(toOfMetadata(TunnelId.class, tunnel.getTunnelId(), tunnel.getTunnelMask()));
440         }
441
442
443         /**
444          * TODO: EXTENSION PROPOSAL (match, MD-SAL to OFJava)
445          * - we might need version for conversion and for key
446          * - sanitize NPE
447          */
448         Optional<GeneralExtensionListGrouping> extensionListOpt = ExtensionResolvers.getMatchExtensionResolver().getExtension(match);
449         if (extensionListOpt.isPresent()) {
450             for (ExtensionList extensionItem : extensionListOpt.get().getExtensionList()) {
451                 // TODO: get real version
452                 ConverterExtensionKey<? extends ExtensionKey> key = new ConverterExtensionKey<>(extensionItem.getExtensionKey(), OFConstants.OFP_VERSION_1_3);
453                 ConvertorToOFJava<MatchEntries> convertor =
454                         OFSessionUtil.getExtensionConvertorProvider().getConverter(key);
455                 MatchEntries ofMatch = convertor.convert(extensionItem.getExtension());
456                 matchEntriesList.add(ofMatch);
457             }
458         }
459
460         return matchEntriesList;
461     }
462
463     /**
464      * Method convert Openflow 1.0 specific flow match to MD-SAL format flow
465      * match
466      *
467      * @param match
468      * @return
469      * @author avishnoi@in.ibm.com
470      */
471     public static Match fromOFMatchV10ToSALMatch(final MatchV10 swMatch, final BigInteger datapathid, final OpenflowVersion ofVersion) {
472         MatchBuilder matchBuilder = new MatchBuilder();
473         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
474         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
475         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
476         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
477         if (!swMatch.getWildcards().isINPORT().booleanValue() && swMatch.getInPort() != null) {
478             matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
479                     (long) swMatch.getInPort(), ofVersion));
480         }
481
482         if (!swMatch.getWildcards().isDLSRC().booleanValue() && swMatch.getDlSrc() != null) {
483             EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
484             ethSrcBuilder.setAddress(swMatch.getDlSrc());
485             ethMatchBuilder.setEthernetSource(ethSrcBuilder.build());
486             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
487         }
488         if (!swMatch.getWildcards().isDLDST().booleanValue() && swMatch.getDlDst() != null) {
489             EthernetDestinationBuilder ethDstBuilder = new EthernetDestinationBuilder();
490             ethDstBuilder.setAddress(swMatch.getDlDst());
491             ethMatchBuilder.setEthernetDestination(ethDstBuilder.build());
492             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
493         }
494         if (!swMatch.getWildcards().isDLTYPE().booleanValue() && swMatch.getDlType() != null) {
495             EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
496             ethTypeBuilder.setType(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType(
497                     (long) swMatch.getDlType()));
498             ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
499             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
500         }
501         if (!swMatch.getWildcards().isDLVLAN().booleanValue() && swMatch.getDlVlan() != null) {
502             VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
503             int vlanId = (swMatch.getDlVlan() == ((int) 0xffff)) ? 0 : swMatch.getDlVlan();
504             vlanIdBuilder.setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(vlanId));
505             vlanIdBuilder.setVlanIdPresent(vlanId == 0 ? false : true);
506             vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
507             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
508         }
509         if (!swMatch.getWildcards().isDLVLANPCP().booleanValue() && swMatch.getDlVlanPcp() != null) {
510             vlanMatchBuilder.setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
511                     swMatch.getDlVlanPcp()));
512             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
513         }
514         if (!swMatch.getWildcards().isDLTYPE().booleanValue() && swMatch.getNwSrc() != null) {
515             String ipv4PrefixStr = swMatch.getNwSrc().getValue();
516             if (swMatch.getNwSrcMask() != null) {
517                 ipv4PrefixStr += PREFIX_SEPARATOR + swMatch.getNwSrcMask();
518             } else {
519                 //Openflow Spec : 1.3.2
520                 //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
521                 // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
522                 // statistics response.
523
524                 ipv4PrefixStr += PREFIX_SEPARATOR + "32";
525             }
526             if (!ipv4PrefixStr.equals(noIp)) {
527                 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
528                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
529             }
530         }
531         if (!swMatch.getWildcards().isDLTYPE().booleanValue() && swMatch.getNwDst() != null) {
532             String ipv4PrefixStr = swMatch.getNwDst().getValue();
533             if (swMatch.getNwDstMask() != null) {
534                 ipv4PrefixStr += PREFIX_SEPARATOR + swMatch.getNwDstMask();
535             } else {
536                 //Openflow Spec : 1.3.2
537                 //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
538                 // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
539                 // statistics response.
540
541                 ipv4PrefixStr += PREFIX_SEPARATOR + "32";
542             }
543             if (!ipv4PrefixStr.equals(noIp)) {
544                 ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
545                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
546             }
547         }
548         if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() != null) {
549             ipMatchBuilder.setIpProtocol(swMatch.getNwProto());
550             matchBuilder.setIpMatch(ipMatchBuilder.build());
551         }
552         if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() == PROTO_TCP) {
553             TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
554             if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
555                 tcpMatchBuilder
556                         .setTcpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
557                                 swMatch.getTpSrc()));
558             }
559             if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
560                 tcpMatchBuilder
561                         .setTcpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
562                                 swMatch.getTpDst()));
563             }
564
565             if (!swMatch.getWildcards().isTPSRC().booleanValue() || !swMatch.getWildcards().isTPDST().booleanValue()) {
566                 matchBuilder.setLayer4Match(tcpMatchBuilder.build());
567             }
568         }
569         if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() == PROTO_UDP) {
570             UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
571             if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
572                 udpMatchBuilder
573                         .setUdpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
574                                 swMatch.getTpSrc()));
575             }
576             if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
577                 udpMatchBuilder
578                         .setUdpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
579                                 swMatch.getTpDst()));
580             }
581
582             if (!swMatch.getWildcards().isTPSRC().booleanValue() || !swMatch.getWildcards().isTPDST().booleanValue()) {
583                 matchBuilder.setLayer4Match(udpMatchBuilder.build());
584             }
585         }
586         if (!swMatch.getWildcards().isNWTOS().booleanValue() && swMatch.getNwTos() != null) {
587             Short dscp = ActionUtil.tosToDscp(swMatch.getNwTos().shortValue());
588             ipMatchBuilder.setIpDscp(new Dscp(dscp));
589             matchBuilder.setIpMatch(ipMatchBuilder.build());
590         }
591
592         return matchBuilder.build();
593     }
594
595     /**
596      * Method converts Openflow 1.3+ specific flow match to MD-SAL format flow
597      * match
598      *
599      * @param match
600      * @param swMatch
601      * @param datapathid
602      * @param ofVersion
603      * @return md-sal match instance
604      * @author avishnoi@in.ibm.com
605      */
606     public static MatchBuilder fromOFMatchToSALMatch(
607             final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match swMatch,
608             final BigInteger datapathid, final OpenflowVersion ofVersion) {
609         return OfMatchToSALMatchConvertor(swMatch.getMatchEntries(), datapathid, ofVersion);
610     }
611
612     private static MatchBuilder OfMatchToSALMatchConvertor(List<MatchEntries> swMatchList, final BigInteger datapathid,
613                                                            OpenflowVersion ofVersion) {
614
615         MatchBuilder matchBuilder = new MatchBuilder();
616         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
617         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
618         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
619         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
620         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
621         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
622         Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder();
623         Icmpv6MatchBuilder icmpv6MatchBuilder = new Icmpv6MatchBuilder();
624         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
625         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
626         Ipv6MatchBuilder ipv6MatchBuilder = new Ipv6MatchBuilder();
627         ProtocolMatchFieldsBuilder protocolMatchFieldsBuilder = new ProtocolMatchFieldsBuilder();
628         TcpFlagMatchBuilder tcpFlagMatchBuilder = new TcpFlagMatchBuilder();
629         TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
630
631         for (MatchEntries ofMatch : swMatchList) {
632
633             if (ofMatch.getOxmMatchField().equals(InPort.class)) {
634                 PortNumberMatchEntry portNumber = ofMatch.getAugmentation(PortNumberMatchEntry.class);
635                 if (portNumber != null) {
636                     Long portNo = portNumber.getPortNumber().getValue();
637                     matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid, portNo, ofVersion));
638                 }
639             } else if (ofMatch.getOxmMatchField().equals(InPhyPort.class)) {
640                 PortNumberMatchEntry portNumber = ofMatch.getAugmentation(PortNumberMatchEntry.class);
641                 matchBuilder.setInPhyPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
642                         portNumber.getPortNumber().getValue(), ofVersion));
643             } else if (ofMatch.getOxmMatchField().equals(Metadata.class)) {
644                 MetadataBuilder metadataBuilder = new MetadataBuilder();
645                 MetadataMatchEntry metadataMatchEntry = ofMatch.getAugmentation(MetadataMatchEntry.class);
646                 if (metadataMatchEntry != null) {
647                     metadataBuilder.setMetadata(new BigInteger(1, metadataMatchEntry.getMetadata()));
648                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
649                     if (maskMatchEntry != null) {
650                         metadataBuilder.setMetadataMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, maskMatchEntry
651                                 .getMask()));
652                     }
653                     matchBuilder.setMetadata(metadataBuilder.build());
654                 }
655             } else if (ofMatch.getOxmMatchField().equals(EthSrc.class)) {
656                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
657                 if (macAddressMatchEntry != null) {
658                     EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
659                     ethSourceBuilder.setAddress(macAddressMatchEntry.getMacAddress());
660                     ethMatchBuilder.setEthernetSource(ethSourceBuilder.build());
661                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
662                 }
663             } else if (ofMatch.getOxmMatchField().equals(EthDst.class)) {
664                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
665                 if (macAddressMatchEntry != null) {
666                     EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
667                     ethDestinationBuilder.setAddress(macAddressMatchEntry.getMacAddress());
668                     ethMatchBuilder.setEthernetDestination(ethDestinationBuilder.build());
669                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
670                 }
671             } else if (ofMatch.getOxmMatchField().equals(EthType.class)) {
672                 EthTypeMatchEntry ethTypeMatchEntry = ofMatch.getAugmentation(EthTypeMatchEntry.class);
673                 if (ethTypeMatchEntry != null) {
674                     EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
675                     ethTypeBuilder
676                             .setType(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType(
677                                     (long) ethTypeMatchEntry.getEthType().getValue()));
678                     ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
679                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
680                 }
681             } else if (ofMatch.getOxmMatchField().equals(VlanVid.class)) {
682                 VlanVidMatchEntry vlanVidMatchEntry = ofMatch.getAugmentation(VlanVidMatchEntry.class);
683                 if (vlanVidMatchEntry != null) {
684                     VlanIdBuilder vlanBuilder = new VlanIdBuilder();
685                     vlanBuilder.setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(
686                             vlanVidMatchEntry.getVlanVid()))
687                             .setVlanIdPresent(vlanVidMatchEntry.isCfiBit());
688                     vlanMatchBuilder.setVlanId(vlanBuilder.build());
689                     matchBuilder.setVlanMatch(vlanMatchBuilder.build());
690                 }
691             } else if (ofMatch.getOxmMatchField().equals(VlanPcp.class)) {
692                 VlanPcpMatchEntry vlanPcpMatchEntry = ofMatch.getAugmentation(VlanPcpMatchEntry.class);
693                 if (vlanPcpMatchEntry != null) {
694                     vlanMatchBuilder
695                             .setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
696                                     vlanPcpMatchEntry.getVlanPcp()));
697                     matchBuilder.setVlanMatch(vlanMatchBuilder.build());
698                 }
699             } else if (ofMatch.getOxmMatchField().equals(IpDscp.class)) {
700                 DscpMatchEntry dscpMatchEntry = ofMatch.getAugmentation(DscpMatchEntry.class);
701                 if (dscpMatchEntry != null) {
702                     ipMatchBuilder.setIpDscp(new Dscp(dscpMatchEntry.getDscp().getValue()));
703                     matchBuilder.setIpMatch(ipMatchBuilder.build());
704                 }
705             } else if (ofMatch.getOxmMatchField().equals(IpEcn.class)) {
706                 EcnMatchEntry ecnMatchEntry = ofMatch.getAugmentation(EcnMatchEntry.class);
707                 if (ecnMatchEntry != null) {
708                     ipMatchBuilder.setIpEcn(ecnMatchEntry.getEcn());
709                     matchBuilder.setIpMatch(ipMatchBuilder.build());
710                 }
711             } else if (ofMatch.getOxmMatchField().equals(IpProto.class)) {
712                 ProtocolNumberMatchEntry protocolNumberMatchEntry = ofMatch
713                         .getAugmentation(ProtocolNumberMatchEntry.class);
714                 if (protocolNumberMatchEntry != null) {
715                     ipMatchBuilder.setIpProtocol(protocolNumberMatchEntry.getProtocolNumber());
716                     matchBuilder.setIpMatch(ipMatchBuilder.build());
717                 }
718             } else if (ofMatch.getOxmMatchField().equals(TcpSrc.class)) {
719                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
720                 if (portMatchEntry != null) {
721                     tcpMatchBuilder.setTcpSourcePort(portMatchEntry.getPort());
722                     matchBuilder.setLayer4Match(tcpMatchBuilder.build());
723                 }
724             } else if (ofMatch.getOxmMatchField().equals(TcpDst.class)) {
725                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
726                 if (portMatchEntry != null) {
727                     tcpMatchBuilder.setTcpDestinationPort(portMatchEntry.getPort());
728                     matchBuilder.setLayer4Match(tcpMatchBuilder.build());
729                 }
730             } else if (ofMatch.getOxmMatchField().equals(UdpSrc.class)) {
731                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
732                 if (portMatchEntry != null) {
733                     udpMatchBuilder.setUdpSourcePort(portMatchEntry.getPort());
734                     matchBuilder.setLayer4Match(udpMatchBuilder.build());
735                 }
736             } else if (ofMatch.getOxmMatchField().equals(UdpDst.class)) {
737                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
738                 if (portMatchEntry != null) {
739                     udpMatchBuilder.setUdpDestinationPort(portMatchEntry.getPort());
740                     matchBuilder.setLayer4Match(udpMatchBuilder.build());
741                 }
742             } else if (ofMatch.getOxmMatchField().equals(SctpSrc.class)) {
743                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
744                 if (portMatchEntry != null) {
745                     sctpMatchBuilder.setSctpSourcePort(portMatchEntry.getPort());
746                     matchBuilder.setLayer4Match(sctpMatchBuilder.build());
747                 }
748             } else if (ofMatch.getOxmMatchField().equals(SctpDst.class)) {
749                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
750                 if (portMatchEntry != null) {
751                     sctpMatchBuilder.setSctpDestinationPort(portMatchEntry.getPort());
752                     matchBuilder.setLayer4Match(sctpMatchBuilder.build());
753                 }
754             } else if (ofMatch.getOxmMatchField().equals(Icmpv4Type.class)) {
755                 Icmpv4TypeMatchEntry icmpv4TypeMatchEntry = ofMatch.getAugmentation(Icmpv4TypeMatchEntry.class);
756                 if (icmpv4TypeMatchEntry != null) {
757                     icmpv4MatchBuilder.setIcmpv4Type(icmpv4TypeMatchEntry.getIcmpv4Type());
758                     matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
759                 }
760             } else if (ofMatch.getOxmMatchField().equals(Icmpv4Code.class)) {
761                 Icmpv4CodeMatchEntry icmpv4CodeMatchEntry = ofMatch.getAugmentation(Icmpv4CodeMatchEntry.class);
762                 if (icmpv4CodeMatchEntry != null) {
763                     icmpv4MatchBuilder.setIcmpv4Code(icmpv4CodeMatchEntry.getIcmpv4Code());
764                     matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
765                 }
766             } else if (ofMatch.getOxmMatchField().equals(Icmpv6Type.class)) {
767                 Icmpv6TypeMatchEntry icmpv6TypeMatchEntry = ofMatch.getAugmentation(Icmpv6TypeMatchEntry.class);
768                 if (icmpv6TypeMatchEntry != null) {
769                     icmpv6MatchBuilder.setIcmpv6Type(icmpv6TypeMatchEntry.getIcmpv6Type());
770                     matchBuilder.setIcmpv6Match(icmpv6MatchBuilder.build());
771                 }
772             } else if (ofMatch.getOxmMatchField().equals(Icmpv6Code.class)) {
773                 Icmpv6CodeMatchEntry icmpv6CodeMatchEntry = ofMatch.getAugmentation(Icmpv6CodeMatchEntry.class);
774                 if (icmpv6CodeMatchEntry != null) {
775                     icmpv6MatchBuilder.setIcmpv6Code(icmpv6CodeMatchEntry.getIcmpv6Code());
776                     matchBuilder.setIcmpv6Match(icmpv6MatchBuilder.build());
777                 }
778             } else if (ofMatch.getOxmMatchField().equals(Ipv4Src.class)
779                     || ofMatch.getOxmMatchField().equals(Ipv4Dst.class)) {
780                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = ofMatch.getAugmentation(Ipv4AddressMatchEntry.class);
781                 if (ipv4AddressMatchEntry != null) {
782                     String ipv4PrefixStr = ipv4AddressMatchEntry.getIpv4Address().getValue();
783                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
784                     if (maskMatchEntry != null) {
785                         ipv4PrefixStr += MatchConvertorUtil.getIpv4Mask(maskMatchEntry);
786                     } else {
787                         //Openflow Spec : 1.3.2
788                         //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
789                         // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
790                         // statistics response.
791
792                         ipv4PrefixStr += PREFIX_SEPARATOR + "32";
793                     }
794                     if (ofMatch.getOxmMatchField().equals(Ipv4Src.class)) {
795                         ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
796                     }
797                     if (ofMatch.getOxmMatchField().equals(Ipv4Dst.class)) {
798                         ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
799                     }
800                     matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
801                 }
802             } else if (ofMatch.getOxmMatchField().equals(TunnelIpv4Dst.class)
803                     || ofMatch.getOxmMatchField().equals(TunnelIpv4Src.class)) {
804                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = ofMatch.getAugmentation(Ipv4AddressMatchEntry.class);
805                 if (ipv4AddressMatchEntry != null) {
806                     String ipv4PrefixStr = ipv4AddressMatchEntry.getIpv4Address().getValue();
807                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
808                     if (maskMatchEntry != null) {
809                         ipv4PrefixStr += PREFIX_SEPARATOR + ByteBuffer.wrap(maskMatchEntry.getMask()).getInt();
810                     } else {
811                         //Openflow Spec : 1.3.2
812                         //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
813                         // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
814                         // statistics response.
815
816                         ipv4PrefixStr += PREFIX_SEPARATOR + "32";
817                     }
818                     if (ofMatch.getOxmMatchField().equals(TunnelIpv4Dst.class)) {
819                         tunnelIpv4MatchBuilder.setTunnelIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
820                     }
821                     if (ofMatch.getOxmMatchField().equals(TunnelIpv4Src.class)) {
822                         tunnelIpv4MatchBuilder.setTunnelIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
823                     }
824                     matchBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
825                 }
826             } else if (ofMatch.getOxmMatchField().equals(ArpOp.class)) {
827                 OpCodeMatchEntry opCodeMatchEntry = ofMatch.getAugmentation(OpCodeMatchEntry.class);
828                 if (opCodeMatchEntry != null) {
829                     arpMatchBuilder.setArpOp(opCodeMatchEntry.getOpCode());
830                     matchBuilder.setLayer3Match(arpMatchBuilder.build());
831                 }
832             } else if (ofMatch.getOxmMatchField().equals(ArpSpa.class)
833                     || ofMatch.getOxmMatchField().equals(ArpTpa.class)) {
834                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = ofMatch.getAugmentation(Ipv4AddressMatchEntry.class);
835                 if (ipv4AddressMatchEntry != null) {
836                     String ipv4PrefixStr = ipv4AddressMatchEntry.getIpv4Address().getValue();
837                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
838                     if (maskMatchEntry != null) {
839                         ipv4PrefixStr += MatchConvertorUtil.getIpv4Mask(maskMatchEntry);
840                     } else {
841                         //Openflow Spec : 1.3.2
842                         //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
843                         // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
844                         // statistics response.
845
846                         ipv4PrefixStr += PREFIX_SEPARATOR + "32";
847                     }
848                     if (ofMatch.getOxmMatchField().equals(ArpSpa.class)) {
849                         arpMatchBuilder.setArpSourceTransportAddress(new Ipv4Prefix(ipv4PrefixStr));
850                     }
851                     if (ofMatch.getOxmMatchField().equals(ArpTpa.class)) {
852                         arpMatchBuilder.setArpTargetTransportAddress(new Ipv4Prefix(ipv4PrefixStr));
853                     }
854                     matchBuilder.setLayer3Match(arpMatchBuilder.build());
855                 }
856             } else if (ofMatch.getOxmMatchField().equals(ArpSha.class)
857                     || ofMatch.getOxmMatchField().equals(ArpTha.class)) {
858                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
859                 if (macAddressMatchEntry != null) {
860                     if (ofMatch.getOxmMatchField().equals(ArpSha.class)) {
861                         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
862                         arpSourceHardwareAddressBuilder.setAddress(macAddressMatchEntry.getMacAddress());
863                         MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
864                         if (maskMatchEntry != null) {
865                             arpSourceHardwareAddressBuilder.setMask(new MacAddress(ByteBufUtils
866                                     .macAddressToString(maskMatchEntry.getMask())));
867                         }
868                         arpMatchBuilder.setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder.build());
869                         matchBuilder.setLayer3Match(arpMatchBuilder.build());
870                     }
871                     if (ofMatch.getOxmMatchField().equals(ArpTha.class)) {
872                         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
873                         arpTargetHardwareAddressBuilder.setAddress(macAddressMatchEntry.getMacAddress());
874                         MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
875                         if (maskMatchEntry != null) {
876                             arpTargetHardwareAddressBuilder.setMask(new MacAddress(ByteBufUtils
877                                     .macAddressToString(maskMatchEntry.getMask())));
878                         }
879                         arpMatchBuilder.setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder.build());
880                         matchBuilder.setLayer3Match(arpMatchBuilder.build());
881                     }
882                 }
883             } else if (ofMatch.getOxmMatchField().equals(Ipv6Src.class)
884                     || ofMatch.getOxmMatchField().equals(Ipv6Dst.class)) {
885                 Ipv6AddressMatchEntry ipv6AddressMatchEntry = ofMatch.getAugmentation(Ipv6AddressMatchEntry.class);
886                 if (ipv6AddressMatchEntry != null) {
887                     String ipv6PrefixStr = ipv6AddressMatchEntry.getIpv6Address().getValue();
888                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
889                     if (maskMatchEntry != null) {
890                         ipv6PrefixStr += PREFIX_SEPARATOR
891                                 + MatchConvertorUtil.ipv6NetmaskArrayToCIDRValue(maskMatchEntry.getMask());
892                     }
893
894                     if (ofMatch.getOxmMatchField().equals(Ipv6Src.class)) {
895                         ipv6MatchBuilder.setIpv6Source(new Ipv6Prefix(ipv6PrefixStr));
896                     }
897                     if (ofMatch.getOxmMatchField().equals(Ipv6Dst.class)) {
898                         ipv6MatchBuilder.setIpv6Destination(new Ipv6Prefix(ipv6PrefixStr));
899                     }
900                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
901                 }
902             } else if (ofMatch.getOxmMatchField().equals(Ipv6Flabel.class)) {
903                 Ipv6FlabelMatchEntry ipv6FlabelMatchEntry = ofMatch.getAugmentation(Ipv6FlabelMatchEntry.class);
904                 if (ipv6FlabelMatchEntry != null) {
905                     Ipv6LabelBuilder ipv6LabelBuilder = new Ipv6LabelBuilder();
906                     ipv6LabelBuilder.setIpv6Flabel(new Ipv6FlowLabel(ipv6FlabelMatchEntry.getIpv6Flabel()));
907                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
908                     if (maskMatchEntry != null) {
909                         ipv6LabelBuilder.setFlabelMask(new Ipv6FlowLabel(Long.valueOf(ByteUtil
910                                 .bytesToUnsignedInt(maskMatchEntry.getMask()))));
911                     }
912                     ipv6MatchBuilder.setIpv6Label(ipv6LabelBuilder.build());
913                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
914                 }
915             } else if (ofMatch.getOxmMatchField().equals(Ipv6NdTarget.class)) {
916                 Ipv6AddressMatchEntry ipv6AddressMatchEntry = ofMatch.getAugmentation(Ipv6AddressMatchEntry.class);
917                 if (ipv6AddressMatchEntry != null) {
918                     ipv6MatchBuilder.setIpv6NdTarget(ipv6AddressMatchEntry.getIpv6Address());
919                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
920                 }
921             } else if (ofMatch.getOxmMatchField().equals(Ipv6NdSll.class)) {
922                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
923                 if (macAddressMatchEntry != null) {
924                     ipv6MatchBuilder.setIpv6NdSll(macAddressMatchEntry.getMacAddress());
925                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
926                 }
927             } else if (ofMatch.getOxmMatchField().equals(Ipv6NdTll.class)) {
928                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
929                 if (macAddressMatchEntry != null) {
930                     ipv6MatchBuilder.setIpv6NdTll(macAddressMatchEntry.getMacAddress());
931                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
932                 }
933             } else if (ofMatch.getOxmMatchField().equals(Ipv6Exthdr.class)) {
934                 PseudoFieldMatchEntry pseudoFieldMatchEntry = ofMatch.getAugmentation(PseudoFieldMatchEntry.class);
935                 if (pseudoFieldMatchEntry != null) {
936                     Ipv6ExtHeaderBuilder ipv6ExtHeaderBuilder = new Ipv6ExtHeaderBuilder();
937
938                     Ipv6ExthdrFlags pField = pseudoFieldMatchEntry.getPseudoField();
939                     Integer bitmap = MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField);
940
941                     ipv6ExtHeaderBuilder.setIpv6Exthdr(bitmap);
942                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
943                     if (maskMatchEntry != null) {
944                         ipv6ExtHeaderBuilder.setIpv6ExthdrMask(ByteUtil.bytesToUnsignedShort(maskMatchEntry.getMask()));
945                     }
946                     ipv6MatchBuilder.setIpv6ExtHeader(ipv6ExtHeaderBuilder.build());
947                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
948                 }
949             } else if (ofMatch.getOxmMatchField().equals(MplsLabel.class)) {
950                 MplsLabelMatchEntry mplsLabelMatchEntry = ofMatch.getAugmentation(MplsLabelMatchEntry.class);
951                 if (mplsLabelMatchEntry != null) {
952                     protocolMatchFieldsBuilder.setMplsLabel(mplsLabelMatchEntry.getMplsLabel());
953                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
954                 }
955             } else if (ofMatch.getOxmMatchField().equals(MplsBos.class)) {
956                 BosMatchEntry bosMatchEntry = ofMatch.getAugmentation(BosMatchEntry.class);
957                 if (bosMatchEntry != null) {
958                     protocolMatchFieldsBuilder.setMplsBos(bosMatchEntry.isBos() ? (short) 1 : (short) 0);
959                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
960                 }
961             } else if (ofMatch.getOxmMatchField().equals(MplsTc.class)) {
962                 TcMatchEntry tcMatchEntry = ofMatch.getAugmentation(TcMatchEntry.class);
963                 if (tcMatchEntry != null) {
964                     protocolMatchFieldsBuilder.setMplsTc(tcMatchEntry.getTc());
965                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
966                 }
967             } else if (ofMatch.getOxmMatchField().equals(PbbIsid.class)) {
968                 IsidMatchEntry isidMatchEntry = ofMatch.getAugmentation(IsidMatchEntry.class);
969                 if (isidMatchEntry != null) {
970                     PbbBuilder pbbBuilder = new PbbBuilder();
971                     pbbBuilder.setPbbIsid(isidMatchEntry.getIsid());
972                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
973                     if (maskMatchEntry != null) {
974                         pbbBuilder.setPbbMask(ByteUtil.bytesToUnsignedMedium(maskMatchEntry.getMask()));
975                     }
976                     protocolMatchFieldsBuilder.setPbb(pbbBuilder.build());
977                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
978                 }
979             } else if (ofMatch.getOxmMatchField().equals(TunnelId.class)) {
980                 TunnelBuilder tunnelBuilder = new TunnelBuilder();
981                 MetadataMatchEntry metadataMatchEntry = ofMatch.getAugmentation(MetadataMatchEntry.class);
982                 if (metadataMatchEntry != null) {
983                     tunnelBuilder.setTunnelId(new BigInteger(1, metadataMatchEntry.getMetadata()));
984                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
985                     if (maskMatchEntry != null) {
986                         tunnelBuilder.setTunnelMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, maskMatchEntry
987                                 .getMask()));
988                     }
989                     matchBuilder.setTunnel(tunnelBuilder.build());
990                 }
991             } else if (ofMatch.getOxmMatchField().equals(TcpFlag.class)) {
992                 //FIXME: move to extensible support
993                 TcpFlagMatchEntry tcpFlagMatch = ofMatch.getAugmentation(TcpFlagMatchEntry.class);
994                 if (tcpFlagMatch != null) {
995                     tcpFlagMatchBuilder.setTcpFlag(tcpFlagMatch.getTcpFlag());
996                     matchBuilder.setTcpFlagMatch(tcpFlagMatchBuilder.build());
997                 }
998             }
999         }
1000         return matchBuilder;
1001     }
1002
1003
1004     private static MatchEntries toOfMplsPbb(final Pbb pbb) {
1005         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1006         boolean hasmask = false;
1007         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1008         matchEntriesBuilder.setOxmMatchField(PbbIsid.class);
1009         IsidMatchEntryBuilder isidBuilder = new IsidMatchEntryBuilder();
1010         isidBuilder.setIsid(pbb.getPbbIsid());
1011         matchEntriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build());
1012         if (pbb.getPbbMask() != null) {
1013             hasmask = true;
1014             addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedMediumToBytes(pbb.getPbbMask()));
1015         }
1016         matchEntriesBuilder.setHasMask(hasmask);
1017         return matchEntriesBuilder.build();
1018     }
1019
1020     private static MatchEntries toOfMplsTc(final Short mplsTc) {
1021         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1022         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1023         matchEntriesBuilder.setHasMask(false);
1024         matchEntriesBuilder.setOxmMatchField(MplsTc.class);
1025         TcMatchEntryBuilder tcBuilder = new TcMatchEntryBuilder();
1026         tcBuilder.setTc(mplsTc);
1027         matchEntriesBuilder.addAugmentation(TcMatchEntry.class, tcBuilder.build());
1028         return matchEntriesBuilder.build();
1029     }
1030
1031     private static MatchEntries toOfMplsBos(final Short mplsBos) {
1032         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1033         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1034         matchEntriesBuilder.setHasMask(false);
1035         matchEntriesBuilder.setOxmMatchField(MplsBos.class);
1036         BosMatchEntryBuilder bosBuilder = new BosMatchEntryBuilder();
1037         if (mplsBos != 0) {
1038             bosBuilder.setBos(true);
1039         } else {
1040             bosBuilder.setBos(false);
1041         }
1042         matchEntriesBuilder.addAugmentation(BosMatchEntry.class, bosBuilder.build());
1043         return matchEntriesBuilder.build();
1044     }
1045
1046     private static MatchEntries toOfMplsLabel(final Long mplsLabel) {
1047         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1048         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1049         matchEntriesBuilder.setHasMask(false);
1050         matchEntriesBuilder.setOxmMatchField(MplsLabel.class);
1051         MplsLabelMatchEntryBuilder mplsLabelBuilder = new MplsLabelMatchEntryBuilder();
1052         mplsLabelBuilder.setMplsLabel(mplsLabel);
1053         matchEntriesBuilder.addAugmentation(MplsLabelMatchEntry.class, mplsLabelBuilder.build());
1054         return matchEntriesBuilder.build();
1055     }
1056
1057     private static MatchEntries toOfIpv6ExtHeader(final Ipv6ExtHeader ipv6ExtHeader) {
1058         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1059         boolean hasmask = false;
1060         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1061         matchEntriesBuilder.setOxmMatchField(Ipv6Exthdr.class);
1062         PseudoFieldMatchEntryBuilder pseudoBuilder = new PseudoFieldMatchEntryBuilder();
1063         Integer bitmap = ipv6ExtHeader.getIpv6Exthdr();
1064         final Boolean NONEXT = ((bitmap) & (1 << 0)) != 0;
1065         final Boolean ESP = ((bitmap) & (1 << 1)) != 0;
1066         final Boolean AUTH = ((bitmap) & (1 << 2)) != 0;
1067         final Boolean DEST = ((bitmap) & (1 << 3)) != 0;
1068         final Boolean FRAG = ((bitmap) & (1 << 4)) != 0;
1069         final Boolean ROUTER = ((bitmap) & (1 << 5)) != 0;
1070         final Boolean HOP = ((bitmap) & (1 << 6)) != 0;
1071         final Boolean UNREP = ((bitmap) & (1 << 7)) != 0;
1072         final Boolean UNSEQ = ((bitmap) & (1 << 8)) != 0;
1073         pseudoBuilder.setPseudoField(new Ipv6ExthdrFlags(AUTH, DEST, ESP, FRAG, HOP, NONEXT, ROUTER, UNREP, UNSEQ));
1074         matchEntriesBuilder.addAugmentation(PseudoFieldMatchEntry.class, pseudoBuilder.build());
1075         if (ipv6ExtHeader.getIpv6ExthdrMask() != null) {
1076             hasmask = true;
1077             addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedShortToBytes(ipv6ExtHeader.getIpv6ExthdrMask()));
1078         }
1079         matchEntriesBuilder.setHasMask(hasmask);
1080         return matchEntriesBuilder.build();
1081     }
1082
1083     private static MatchEntries toOfIpv6FlowLabel(final Ipv6Label ipv6Label) {
1084         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1085         boolean hasmask = false;
1086         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1087         matchEntriesBuilder.setOxmMatchField(Ipv6Flabel.class);
1088         Ipv6FlabelMatchEntryBuilder ipv6FlabelBuilder = new Ipv6FlabelMatchEntryBuilder();
1089         ipv6FlabelBuilder.setIpv6Flabel(ipv6Label.getIpv6Flabel());
1090         matchEntriesBuilder.addAugmentation(Ipv6FlabelMatchEntry.class, ipv6FlabelBuilder.build());
1091         if (ipv6Label.getFlabelMask() != null) {
1092             hasmask = true;
1093             addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedIntToBytes(ipv6Label.getFlabelMask().getValue()));
1094         }
1095         matchEntriesBuilder.setHasMask(hasmask);
1096         return matchEntriesBuilder.build();
1097     }
1098
1099     private static MatchEntries toOfPort(final Class<? extends MatchField> field, final Long portNumber) {
1100         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1101         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1102         matchEntriesBuilder.setHasMask(false);
1103         matchEntriesBuilder.setOxmMatchField(field);
1104         PortNumberMatchEntryBuilder port = new PortNumberMatchEntryBuilder();
1105         port.setPortNumber(new PortNumber(portNumber));
1106         matchEntriesBuilder.addAugmentation(PortNumberMatchEntry.class, port.build());
1107         return matchEntriesBuilder.build();
1108     }
1109
1110     private static MatchEntries toOfMetadata(final Class<? extends MatchField> field, final BigInteger metadata,
1111                                              final BigInteger metadataMask) {
1112         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1113         boolean hasmask = false;
1114         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1115         matchEntriesBuilder.setOxmMatchField(field);
1116         addMetadataAugmentation(matchEntriesBuilder, metadata);
1117         if (metadataMask != null) {
1118             hasmask = true;
1119             addMaskAugmentation(matchEntriesBuilder,
1120                     ByteUtil.convertBigIntegerToNBytes(metadataMask, OFConstants.SIZE_OF_LONG_IN_BYTES));
1121         }
1122         matchEntriesBuilder.setHasMask(hasmask);
1123         return matchEntriesBuilder.build();
1124     }
1125
1126     public static MatchEntries toOfMacAddress(final Class<? extends MatchField> field,
1127                                               final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress macAddress,
1128                                               final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress mask) {
1129         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1130         boolean hasmask = false;
1131         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1132         matchEntriesBuilder.setOxmMatchField(field);
1133         addMacAddressAugmentation(matchEntriesBuilder, macAddress);
1134         if (mask != null) {
1135             hasmask = true;
1136             addMaskAugmentation(matchEntriesBuilder, ByteBufUtils.macAddressToBytes(mask.getValue()));
1137         }
1138         matchEntriesBuilder.setHasMask(hasmask);
1139         return matchEntriesBuilder.build();
1140     }
1141
1142     private static MatchEntries toOfEthernetType(final EthernetType ethernetType) {
1143         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1144         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1145         matchEntriesBuilder.setHasMask(false);
1146         matchEntriesBuilder.setOxmMatchField(EthType.class);
1147         EthTypeMatchEntryBuilder ethertypeBuilder = new EthTypeMatchEntryBuilder();
1148         ethertypeBuilder.setEthType(new EtherType(ethernetType.getType().getValue().intValue()));
1149         matchEntriesBuilder.addAugmentation(EthTypeMatchEntry.class, ethertypeBuilder.build());
1150         return matchEntriesBuilder.build();
1151     }
1152
1153     private static MatchEntries toOfLayer3Port(final Class<? extends MatchField> field,
1154                                                final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber portNumber) {
1155         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1156         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1157         matchEntriesBuilder.setHasMask(false);
1158         matchEntriesBuilder.setOxmMatchField(field);
1159         PortMatchEntryBuilder portBuilder = new PortMatchEntryBuilder();
1160         portBuilder.setPort(portNumber);
1161         matchEntriesBuilder.addAugmentation(PortMatchEntry.class, portBuilder.build());
1162         return matchEntriesBuilder.build();
1163     }
1164
1165     private static MatchEntries toOfIcmpv4Type(final Short icmpv4Type) {
1166         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1167         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1168         matchEntriesBuilder.setHasMask(false);
1169         matchEntriesBuilder.setOxmMatchField(Icmpv4Type.class);
1170         Icmpv4TypeMatchEntryBuilder icmpv4TypeBuilder = new Icmpv4TypeMatchEntryBuilder();
1171         icmpv4TypeBuilder.setIcmpv4Type(icmpv4Type);
1172         matchEntriesBuilder.addAugmentation(Icmpv4TypeMatchEntry.class, icmpv4TypeBuilder.build());
1173         return matchEntriesBuilder.build();
1174     }
1175
1176     private static MatchEntries toOfIcmpv4Code(final Short icmpv4Code) {
1177         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1178         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1179         matchEntriesBuilder.setHasMask(false);
1180         matchEntriesBuilder.setOxmMatchField(Icmpv4Code.class);
1181         Icmpv4CodeMatchEntryBuilder icmpv4CodeBuilder = new Icmpv4CodeMatchEntryBuilder();
1182         icmpv4CodeBuilder.setIcmpv4Code(icmpv4Code);
1183         matchEntriesBuilder.addAugmentation(Icmpv4CodeMatchEntry.class, icmpv4CodeBuilder.build());
1184         return matchEntriesBuilder.build();
1185     }
1186
1187     private static MatchEntries toOfIcmpv6Type(final Short icmpv6Type) {
1188         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1189         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1190         matchEntriesBuilder.setHasMask(false);
1191         matchEntriesBuilder.setOxmMatchField(Icmpv6Type.class);
1192         Icmpv6TypeMatchEntryBuilder icmpv6TypeBuilder = new Icmpv6TypeMatchEntryBuilder();
1193         icmpv6TypeBuilder.setIcmpv6Type(icmpv6Type);
1194         matchEntriesBuilder.addAugmentation(Icmpv6TypeMatchEntry.class, icmpv6TypeBuilder.build());
1195         return matchEntriesBuilder.build();
1196     }
1197
1198     private static MatchEntries toOfIcmpv6Code(final Short icmpv6Code) {
1199         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1200         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1201         matchEntriesBuilder.setHasMask(false);
1202         matchEntriesBuilder.setOxmMatchField(Icmpv6Code.class);
1203         Icmpv6CodeMatchEntryBuilder icmpv6CodeBuilder = new Icmpv6CodeMatchEntryBuilder();
1204         icmpv6CodeBuilder.setIcmpv6Code(icmpv6Code);
1205         matchEntriesBuilder.addAugmentation(Icmpv6CodeMatchEntry.class, icmpv6CodeBuilder.build());
1206         return matchEntriesBuilder.build();
1207     }
1208
1209     public static MatchEntries toOfIpv4Prefix(final Class<? extends MatchField> field, final Ipv4Prefix ipv4Prefix) {
1210         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1211         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1212         matchEntriesBuilder.setOxmMatchField(field);
1213         boolean hasMask = addIpv4PrefixAugmentation(matchEntriesBuilder, ipv4Prefix);
1214         matchEntriesBuilder.setHasMask(hasMask);
1215         return matchEntriesBuilder.build();
1216     }
1217
1218     private static MatchEntries toOfIpv6Prefix(final Class<? extends MatchField> field, final Ipv6Prefix ipv6Prefix) {
1219         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1220         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1221         matchEntriesBuilder.setOxmMatchField(field);
1222         boolean hasmask = addIpv6PrefixAugmentation(matchEntriesBuilder, ipv6Prefix);
1223         matchEntriesBuilder.setHasMask(hasmask);
1224         return matchEntriesBuilder.build();
1225     }
1226
1227     public static MatchEntries toOfIpDscp(final Dscp ipDscp) {
1228         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1229         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1230         matchEntriesBuilder.setHasMask(false);
1231         matchEntriesBuilder.setOxmMatchField(IpDscp.class);
1232         DscpMatchEntryBuilder dscpBuilder = new DscpMatchEntryBuilder();
1233         dscpBuilder.setDscp(ipDscp);
1234         matchEntriesBuilder.addAugmentation(DscpMatchEntry.class, dscpBuilder.build());
1235         return matchEntriesBuilder.build();
1236     }
1237
1238     public static MatchEntries toOfVlanPcp(
1239             final org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp vlanPcp) {
1240         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1241         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1242         matchEntriesBuilder.setHasMask(false);
1243         matchEntriesBuilder.setOxmMatchField(VlanPcp.class);
1244         VlanPcpMatchEntryBuilder vlanPcpBuilder = new VlanPcpMatchEntryBuilder();
1245         vlanPcpBuilder.setVlanPcp(vlanPcp.getValue());
1246         matchEntriesBuilder.addAugmentation(VlanPcpMatchEntry.class, vlanPcpBuilder.build());
1247         return matchEntriesBuilder.build();
1248     }
1249
1250     private static MatchEntries toOfVlanVid(final VlanId vlanId) {
1251         // TODO: verify
1252         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1253         boolean hasmask = false;
1254         boolean setCfiBit = false;
1255         Integer vidEntryValue = 0;
1256         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1257         matchEntriesBuilder.setOxmMatchField(VlanVid.class);
1258         VlanVidMatchEntryBuilder vlanVidBuilder = new VlanVidMatchEntryBuilder();
1259         if (Boolean.TRUE.equals(vlanId.isVlanIdPresent())) {
1260             setCfiBit = true;
1261             if (vlanId.getVlanId() != null) {
1262                 vidEntryValue = vlanId.getVlanId().getValue();
1263             }
1264             hasmask = (vidEntryValue == 0);
1265         }
1266         vlanVidBuilder.setCfiBit(setCfiBit);
1267         vlanVidBuilder.setVlanVid(vidEntryValue);
1268         matchEntriesBuilder.addAugmentation(VlanVidMatchEntry.class, vlanVidBuilder.build());
1269         if (hasmask) {
1270             addMaskAugmentation(matchEntriesBuilder, VLAN_VID_MASK);
1271         }
1272         matchEntriesBuilder.setHasMask(hasmask);
1273         return matchEntriesBuilder.build();
1274     }
1275
1276     private static MatchEntries toOfIpProto(final Short ipProtocol) {
1277         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1278         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1279         matchEntriesBuilder.setHasMask(false);
1280         matchEntriesBuilder.setOxmMatchField(IpProto.class);
1281         ProtocolNumberMatchEntryBuilder protoNumberBuilder = new ProtocolNumberMatchEntryBuilder();
1282         protoNumberBuilder.setProtocolNumber(ipProtocol);
1283         matchEntriesBuilder.addAugmentation(ProtocolNumberMatchEntry.class, protoNumberBuilder.build());
1284         return matchEntriesBuilder.build();
1285     }
1286
1287     private static MatchEntries toOfIpEcn(final Short ipEcn) {
1288         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1289         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1290         matchEntriesBuilder.setHasMask(false);
1291         matchEntriesBuilder.setOxmMatchField(IpEcn.class);
1292         EcnMatchEntryBuilder ecnBuilder = new EcnMatchEntryBuilder();
1293         ecnBuilder.setEcn(ipEcn);
1294         matchEntriesBuilder.addAugmentation(EcnMatchEntry.class, ecnBuilder.build());
1295         return matchEntriesBuilder.build();
1296     }
1297
1298     private static MatchEntries toOfArpOpCode(final Integer arpOp) {
1299         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1300         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1301         matchEntriesBuilder.setHasMask(false);
1302         matchEntriesBuilder.setOxmMatchField(ArpOp.class);
1303         OpCodeMatchEntryBuilder opcodeBuilder = new OpCodeMatchEntryBuilder();
1304         opcodeBuilder.setOpCode(arpOp);
1305         matchEntriesBuilder.addAugmentation(OpCodeMatchEntry.class, opcodeBuilder.build());
1306         return matchEntriesBuilder.build();
1307     }
1308
1309     private static MatchEntries toOfIpv6Address(final Ipv6Address address) {
1310         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1311         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1312         matchEntriesBuilder.setHasMask(false);
1313         matchEntriesBuilder.setOxmMatchField(Ipv6NdTarget.class);
1314         Ipv6AddressMatchEntryBuilder ipv6AddressBuilder = new Ipv6AddressMatchEntryBuilder();
1315         ipv6AddressBuilder.setIpv6Address(address);
1316         matchEntriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, ipv6AddressBuilder.build());
1317         return matchEntriesBuilder.build();
1318     }
1319
1320     private static void addMaskAugmentation(final MatchEntriesBuilder builder, final byte[] mask) {
1321         MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
1322         maskBuilder.setMask(mask);
1323         builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
1324     }
1325
1326     private static boolean addIpv6PrefixAugmentation(final MatchEntriesBuilder builder, final Ipv6Prefix address) {
1327         boolean hasMask = false;
1328         Iterator<String> addressParts = PREFIX_SPLITTER.split(address.getValue()).iterator();
1329         Ipv6Address ipv6Address = new Ipv6Address(addressParts.next());
1330
1331         Integer prefix = null;
1332         if (addressParts.hasNext()) {
1333             prefix = Integer.parseInt(addressParts.next());
1334         }
1335
1336         Ipv6AddressMatchEntryBuilder ipv6AddressBuilder = new Ipv6AddressMatchEntryBuilder();
1337         ipv6AddressBuilder.setIpv6Address(ipv6Address);
1338         builder.addAugmentation(Ipv6AddressMatchEntry.class, ipv6AddressBuilder.build());
1339         if (prefix != null) {
1340             hasMask = true;
1341             addMaskAugmentation(builder, convertIpv6PrefixToByteArray(prefix));
1342         }
1343         return hasMask;
1344     }
1345
1346     private static byte[] convertIpv6PrefixToByteArray(final int prefix) {
1347         // TODO: Temporary fix. Has performance impacts.
1348         byte[] mask = new byte[16];
1349         int oneCount = prefix;
1350         for (int count = 0; count < 16; count++) {
1351             int byteBits = 0;
1352             if (oneCount >= 8) {
1353                 byteBits = 8;
1354                 oneCount = oneCount - 8;
1355             } else {
1356                 byteBits = oneCount;
1357                 oneCount = 0;
1358             }
1359
1360             mask[count] = (byte) (256 - Math.pow(2, 8 - byteBits));
1361         }
1362         return mask;
1363     }
1364
1365     private static void addMetadataAugmentation(final MatchEntriesBuilder builder, final BigInteger metadata) {
1366         MetadataMatchEntryBuilder metadataMatchEntry = new MetadataMatchEntryBuilder();
1367         metadataMatchEntry.setMetadata(ByteUtil.convertBigIntegerToNBytes(metadata, OFConstants.SIZE_OF_LONG_IN_BYTES));
1368         builder.addAugmentation(MetadataMatchEntry.class, metadataMatchEntry.build());
1369     }
1370
1371     /**
1372      * @return true if Ipv4Prefix contains prefix (and it is used in mask),
1373      * false otherwise
1374      */
1375     private static boolean addIpv4PrefixAugmentation(final MatchEntriesBuilder builder, final Ipv4Prefix address) {
1376         boolean hasMask = false;
1377         Iterator<String> addressParts = PREFIX_SPLITTER.split(address.getValue()).iterator();
1378         Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
1379
1380         final int prefix;
1381         if (addressParts.hasNext()) {
1382             int potentionalPrefix = Integer.parseInt(addressParts.next());
1383             prefix = potentionalPrefix < 32 ? potentionalPrefix : 0;
1384         } else {
1385             prefix = 0;
1386         }
1387
1388         Ipv4AddressMatchEntryBuilder ipv4AddressBuilder = new Ipv4AddressMatchEntryBuilder();
1389         ipv4AddressBuilder.setIpv4Address(ipv4Address);
1390         builder.addAugmentation(Ipv4AddressMatchEntry.class, ipv4AddressBuilder.build());
1391         if (prefix != 0) {
1392             int mask = 0xffffffff << (32 - prefix);
1393             byte[] maskBytes = new byte[]{(byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8),
1394                     (byte) mask};
1395             addMaskAugmentation(builder, maskBytes);
1396             hasMask = true;
1397         }
1398         return hasMask;
1399     }
1400
1401     private static void addMacAddressAugmentation(final MatchEntriesBuilder builder, final MacAddress address) {
1402         MacAddressMatchEntryBuilder macAddress = new MacAddressMatchEntryBuilder();
1403         macAddress.setMacAddress(address);
1404         builder.addAugmentation(MacAddressMatchEntry.class, macAddress.build());
1405     }
1406
1407     /**
1408      * Method converts OF SetField action to SAL SetFiled action.
1409      *
1410      * @param action
1411      * @param ofVersion current ofp version
1412      * @return
1413      */
1414     public static SetField fromOFSetFieldToSALSetFieldAction(
1415             final Action action, OpenflowVersion ofVersion) {
1416         logger.debug("Converting OF SetField action to SAL SetField action");
1417         SetFieldBuilder setField = new SetFieldBuilder();
1418         OxmFieldsAction oxmFields = action.getAugmentation(OxmFieldsAction.class);
1419         MatchBuilder match = OfMatchToSALMatchConvertor(oxmFields.getMatchEntries(), null, ofVersion);
1420         setField.fieldsFrom(match.build());
1421         return setField.build();
1422     }
1423 }