1 package org.opendaylight.openflowplugin.openflow.md.core.translator;
3 import java.math.BigInteger;
4 import java.util.Collections;
5 import java.util.HashMap;
8 import java.util.Map.Entry;
9 import java.util.concurrent.CopyOnWriteArrayList;
11 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
12 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
13 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
14 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemovedBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.Match;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.MatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeaderBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6LabelBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFieldsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.protocol.match.fields.PbbBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.BosMatchEntry;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DscpMatchEntry;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntry;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntry;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4CodeMatchEntry;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4TypeMatchEntry;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntry;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6TypeMatchEntry;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntry;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntry;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntry;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntry;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsLabelMatchEntry;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OpCodeMatchEntry;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortMatchEntry;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntry;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntry;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntry;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpOp;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSha;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSpa;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTha;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTpa;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthDst;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthSrc;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthType;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Code;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Type;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Code;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Type;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPhyPort;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpDscp;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpEcn;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpProto;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Dst;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Dst;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Exthdr;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Flabel;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdSll;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTarget;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTll;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Src;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MatchField;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Metadata;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsBos;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsLabel;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsTc;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.PbbIsid;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpDst;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpSrc;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpDst;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpSrc;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelId;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpDst;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpSrc;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanPcp;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanVid;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.MatchEntries;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
116 import org.opendaylight.yangtools.yang.binding.DataObject;
117 import org.slf4j.Logger;
118 import org.slf4j.LoggerFactory;
120 public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
122 protected static final Logger LOG = LoggerFactory.getLogger(FlowRemovedTranslator.class);
123 private static final String PREFIX_SEPARATOR = "/";
126 public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sc, OfHeader msg) {
127 if (msg instanceof FlowRemovedMessage) {
128 FlowRemovedMessage ofFlow = (FlowRemovedMessage) msg;
129 List<DataObject> list = new CopyOnWriteArrayList<DataObject>();
130 LOG.debug("Flow Removed Message received: Table Id={}, Flow removed reason={} ", ofFlow.getTableId(),
133 SwitchFlowRemovedBuilder salFlowRemoved = new SwitchFlowRemovedBuilder();
135 salFlowRemoved.setCookie(ofFlow.getCookie());
136 salFlowRemoved.setPriority(ofFlow.getPriority());
138 if (ofFlow.getTableId() != null) {
139 salFlowRemoved.setTableId(ofFlow.getTableId().getValue().shortValue());
142 salFlowRemoved.setDurationSec(ofFlow.getDurationSec());
143 salFlowRemoved.setDurationNsec(ofFlow.getDurationNsec());
144 salFlowRemoved.setIdleTimeout(ofFlow.getIdleTimeout());
145 salFlowRemoved.setHardTimeout(ofFlow.getHardTimeout());
146 salFlowRemoved.setPacketCount(ofFlow.getPacketCount());
147 salFlowRemoved.setByteCount(ofFlow.getByteCount());
148 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.match.grouping.Match ofMatch = ofFlow
150 if (ofMatch != null) {
151 salFlowRemoved.setMatch(fromMatch(ofMatch));
153 salFlowRemoved.setNode(new NodeRef(InventoryDataServiceUtil.identifierFromDatapathId(sc.getFeatures()
155 list.add(salFlowRemoved.build());
158 LOG.error("Message is not a flow removed message ");
159 return Collections.emptyList();
163 public Match fromMatch(
164 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.match.grouping.Match ofMatch) {
165 MatchBuilder matchBuilder = new MatchBuilder();
166 EthernetMatchBuilder ethernetMatch = null;
167 VlanMatchBuilder vlanMatch = null;
168 IpMatchBuilder ipMatch = null;
169 TcpMatchBuilder tcpMatch = null;
170 UdpMatchBuilder udpMatch = null;
171 SctpMatchBuilder sctpMatch = null;
172 Icmpv4MatchBuilder icmpv4Match = null;
173 Icmpv6MatchBuilder icmpv6Match = null;
174 Ipv4MatchBuilder ipv4Match = null;
175 ArpMatchBuilder arpMatch = null;
176 Ipv6MatchBuilder ipv6Match = null;
177 ProtocolMatchFieldsBuilder protocolMatchFields = null;
179 for (MatchEntries entry : ofMatch.getMatchEntries()) {
180 Class<? extends MatchField> field = entry.getOxmMatchField();
181 if (field.equals(InPort.class)) {
182 matchBuilder.setInPort(entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue()
184 } else if (field.equals(InPhyPort.class)) {
185 matchBuilder.setInPhyPort(entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue()
187 } else if (field.equals(Metadata.class)) {
188 MetadataBuilder metadata = new MetadataBuilder();
189 metadata.setMetadata(new BigInteger(entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
190 if (entry.isHasMask()) {
191 metadata.setMetadataMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
193 matchBuilder.setMetadata(metadata.build());
194 } else if (field.equals(EthDst.class) || field.equals(EthSrc.class) || field.equals(EthType.class)) {
195 if (ethernetMatch == null) {
196 ethernetMatch = new EthernetMatchBuilder();
198 if (field.equals(EthDst.class)) {
199 EthernetDestinationBuilder ethDst = new EthernetDestinationBuilder();
200 ethDst.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
201 if (entry.isHasMask()) {
202 ethDst.setMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
204 ethernetMatch.setEthernetDestination(ethDst.build());
205 } else if (field.equals(EthSrc.class)) {
206 EthernetSourceBuilder ethSrc = new EthernetSourceBuilder();
207 ethSrc.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
208 if (entry.isHasMask()) {
209 ethSrc.setMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
211 ethernetMatch.setEthernetSource(ethSrc.build());
212 } else if (field.equals(EthType.class)) {
213 EthernetTypeBuilder ethType = new EthernetTypeBuilder();
214 ethType.setType(new EtherType(entry.getAugmentation(EthTypeMatchEntry.class).getEthType()
215 .getValue().longValue()));
216 ethernetMatch.setEthernetType(ethType.build());
218 } else if (field.equals(VlanVid.class) || field.equals(VlanPcp.class)) {
219 if (vlanMatch == null) {
220 vlanMatch = new VlanMatchBuilder();
222 if (field.equals(VlanVid.class)) {
223 boolean vlanIdPresent = false;
224 VlanIdBuilder vlanId = new VlanIdBuilder();
225 VlanVidMatchEntry vlanVid = entry.getAugmentation(VlanVidMatchEntry.class);
226 Integer vlanVidValue = vlanVid.getVlanVid();
227 if (vlanVid.isCfiBit()) {
228 vlanIdPresent = true;
230 vlanId.setVlanIdPresent(vlanIdPresent);
231 if (vlanVidValue != null) {
232 vlanId.setVlanId(new VlanId(vlanVidValue));
234 vlanMatch.setVlanId(vlanId.build());
235 } else if (field.equals(VlanPcp.class)) {
236 vlanMatch.setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
237 entry.getAugmentation(VlanPcpMatchEntry.class).getVlanPcp().shortValue()));
239 } else if (field.equals(IpDscp.class) || field.equals(IpEcn.class) || field.equals(IpProto.class)) {
240 if (ipMatch == null) {
241 ipMatch = new IpMatchBuilder();
243 if (field.equals(IpDscp.class)) {
244 ipMatch.setIpDscp(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp(
245 entry.getAugmentation(DscpMatchEntry.class).getDscp().getValue()));
246 } else if (field.equals(IpEcn.class)) {
247 ipMatch.setIpEcn(entry.getAugmentation(EcnMatchEntry.class).getEcn());
248 } else if (field.equals(IpProto.class)) {
249 ipMatch.setIpProtocol(entry.getAugmentation(ProtocolNumberMatchEntry.class).getProtocolNumber());
251 } else if (field.equals(TcpSrc.class) || field.equals(TcpDst.class)) {
252 if (tcpMatch == null) {
253 tcpMatch = new TcpMatchBuilder();
255 if (field.equals(TcpSrc.class)) {
256 tcpMatch.setTcpSourcePort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
258 } else if (field.equals(TcpDst.class)) {
259 tcpMatch.setTcpDestinationPort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
262 } else if (field.equals(UdpSrc.class) || field.equals(UdpDst.class)) {
263 if (udpMatch == null) {
264 udpMatch = new UdpMatchBuilder();
266 if (field.equals(UdpSrc.class)) {
267 udpMatch.setUdpSourcePort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
269 } else if (field.equals(UdpDst.class)) {
270 udpMatch.setUdpDestinationPort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
273 } else if (field.equals(SctpSrc.class) || field.equals(SctpDst.class)) {
274 if (sctpMatch == null) {
275 sctpMatch = new SctpMatchBuilder();
277 if (field.equals(SctpSrc.class)) {
278 sctpMatch.setSctpSourcePort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
280 } else if (field.equals(SctpDst.class)) {
281 sctpMatch.setSctpDestinationPort(new PortNumber(entry.getAugmentation(PortMatchEntry.class)
282 .getPort().getValue()));
284 } else if (field.equals(Icmpv4Type.class) || field.equals(Icmpv4Code.class)) {
285 if (icmpv4Match == null) {
286 icmpv4Match = new Icmpv4MatchBuilder();
288 if (field.equals(Icmpv4Type.class)) {
289 icmpv4Match.setIcmpv4Type(entry.getAugmentation(Icmpv4TypeMatchEntry.class).getIcmpv4Type());
290 } else if (field.equals(Icmpv4Code.class)) {
291 icmpv4Match.setIcmpv4Code(entry.getAugmentation(Icmpv4CodeMatchEntry.class).getIcmpv4Code());
293 } else if (field.equals(Icmpv6Type.class) || field.equals(Icmpv6Code.class)) {
294 if (icmpv6Match == null) {
295 icmpv6Match = new Icmpv6MatchBuilder();
297 if (field.equals(Icmpv6Type.class)) {
298 icmpv6Match.setIcmpv6Type(entry.getAugmentation(Icmpv6TypeMatchEntry.class).getIcmpv6Type());
299 } else if (field.equals(Icmpv6Code.class)) {
300 icmpv6Match.setIcmpv6Code(entry.getAugmentation(Icmpv6CodeMatchEntry.class).getIcmpv6Code());
302 } else if (field.equals(Ipv4Src.class) || field.equals(Ipv4Dst.class)) {
303 if (ipv4Match == null) {
304 ipv4Match = new Ipv4MatchBuilder();
306 if (field.equals(Ipv4Src.class)) {
307 ipv4Match.setIpv4Source(toIpv4Prefix(entry));
308 } else if (field.equals(Ipv4Dst.class)) {
309 ipv4Match.setIpv4Destination(toIpv4Prefix(entry));
311 } else if (field.equals(ArpOp.class) || field.equals(ArpSpa.class) || field.equals(ArpTpa.class)
312 || field.equals(ArpSha.class) || field.equals(ArpTha.class)) {
313 if (arpMatch == null) {
314 arpMatch = new ArpMatchBuilder();
316 if (field.equals(ArpOp.class)) {
317 arpMatch.setArpOp(entry.getAugmentation(OpCodeMatchEntry.class).getOpCode());
318 } else if (field.equals(ArpSpa.class)) {
319 arpMatch.setArpSourceTransportAddress(toIpv4Prefix(entry));
320 } else if (field.equals(ArpTpa.class)) {
321 arpMatch.setArpTargetTransportAddress(toIpv4Prefix(entry));
322 } else if (field.equals(ArpSha.class)) {
323 ArpSourceHardwareAddressBuilder arpSha = new ArpSourceHardwareAddressBuilder();
324 arpSha.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
325 if (entry.isHasMask()) {
326 arpSha.setMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
328 arpMatch.setArpSourceHardwareAddress(arpSha.build());
329 } else if (field.equals(ArpTha.class)) {
330 ArpTargetHardwareAddressBuilder arpTha = new ArpTargetHardwareAddressBuilder();
331 arpTha.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
332 if (entry.isHasMask()) {
333 arpTha.setMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
335 arpMatch.setArpTargetHardwareAddress(arpTha.build());
337 } else if (field.equals(Ipv6Src.class) || field.equals(Ipv6Dst.class) || field.equals(Ipv6Flabel.class)
338 || field.equals(Ipv6NdTarget.class) || field.equals(Ipv6NdSll.class)
339 || field.equals(Ipv6NdTll.class) || field.equals(Ipv6Exthdr.class)) {
340 if (ipv6Match == null) {
341 ipv6Match = new Ipv6MatchBuilder();
343 if (field.equals(Ipv6Src.class)) {
344 ipv6Match.setIpv6Source(toIpv6Prefix(entry));
345 } else if (field.equals(Ipv6Dst.class)) {
346 ipv6Match.setIpv6Destination(toIpv6Prefix(entry));
347 } else if (field.equals(Ipv6Flabel.class)) {
348 Ipv6LabelBuilder ipv6Label = new Ipv6LabelBuilder();
349 ipv6Label.setIpv6Flabel(entry.getAugmentation(Ipv6FlabelMatchEntry.class).getIpv6Flabel());
350 if (entry.isHasMask()) {
351 ipv6Label.setFlabelMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
353 ipv6Match.setIpv6Label(ipv6Label.build());
354 } else if (field.equals(Ipv6NdTarget.class)) {
355 ipv6Match.setIpv6NdTarget(entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address());
356 } else if (field.equals(Ipv6NdSll.class)) {
357 ipv6Match.setIpv6NdSll(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
358 } else if (field.equals(Ipv6NdTll.class)) {
359 ipv6Match.setIpv6NdTll(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
360 } else if (field.equals(Ipv6Exthdr.class)) {
362 Ipv6ExtHeaderBuilder ipv6ExtHeaderBuilder = new Ipv6ExtHeaderBuilder();
363 Ipv6ExthdrFlags pseudoField = entry.getAugmentation(PseudoFieldMatchEntry.class).getPseudoField();
364 Map<Integer, Boolean> map = new HashMap<>();
365 map.put(0, pseudoField.isNonext());
366 map.put(1, pseudoField.isEsp());
367 map.put(2, pseudoField.isAuth());
368 map.put(3, pseudoField.isDest());
369 map.put(4, pseudoField.isFrag());
370 map.put(5, pseudoField.isRouter());
371 map.put(6, pseudoField.isHop());
372 map.put(7, pseudoField.isUnrep());
373 map.put(8, pseudoField.isUnseq());
374 int bitmap = fillBitMaskFromMap(map);
375 ipv6ExtHeaderBuilder.setIpv6Exthdr(bitmap);
376 if (entry.isHasMask()) {
377 ipv6ExtHeaderBuilder.setIpv6ExthdrMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
379 ipv6Match.setIpv6ExtHeader(ipv6ExtHeaderBuilder.build());
381 } else if (field.equals(MplsLabel.class) || field.equals(MplsTc.class) || field.equals(MplsBos.class)
382 || field.equals(PbbIsid.class)) {
383 if (protocolMatchFields == null) {
384 protocolMatchFields = new ProtocolMatchFieldsBuilder();
386 if (field.equals(MplsLabel.class)) {
387 protocolMatchFields.setMplsLabel(entry.getAugmentation(MplsLabelMatchEntry.class).getMplsLabel());
388 } else if (field.equals(MplsTc.class)) {
389 protocolMatchFields.setMplsTc(entry.getAugmentation(TcMatchEntry.class).getTc());
390 } else if (field.equals(MplsBos.class)) {
392 .setMplsBos((short) (entry.getAugmentation(BosMatchEntry.class).isBos() ? 1 : 0));
393 } else if (field.equals(PbbIsid.class)) {
394 PbbBuilder pbb = new PbbBuilder();
395 pbb.setPbbIsid(entry.getAugmentation(IsidMatchEntry.class).getIsid());
396 if (entry.isHasMask()) {
397 pbb.setPbbMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
399 protocolMatchFields.setPbb(pbb.build());
401 } else if (field.equals(TunnelId.class)) {
402 TunnelBuilder tunnel = new TunnelBuilder();
403 tunnel.setTunnelId(new BigInteger(entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
404 if (entry.isHasMask()) {
405 tunnel.setTunnelMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
407 matchBuilder.setTunnel(tunnel.build());
411 if (ethernetMatch != null) {
412 matchBuilder.setEthernetMatch(ethernetMatch.build());
414 if (vlanMatch != null) {
415 matchBuilder.setVlanMatch(vlanMatch.build());
417 if (ipMatch != null) {
418 matchBuilder.setIpMatch(ipMatch.build());
421 if (tcpMatch != null) {
422 matchBuilder.setLayer4Match(tcpMatch.build());
423 } else if (udpMatch != null) {
424 matchBuilder.setLayer4Match(udpMatch.build());
425 } else if (sctpMatch != null) {
426 matchBuilder.setLayer4Match(sctpMatch.build());
429 if (icmpv4Match != null) {
430 matchBuilder.setIcmpv4Match(icmpv4Match.build());
431 } else if (icmpv6Match != null) {
432 matchBuilder.setIcmpv6Match(icmpv6Match.build());
435 if (ipv4Match != null) {
436 matchBuilder.setLayer3Match(ipv4Match.build());
437 } else if (arpMatch != null) {
438 matchBuilder.setLayer3Match(arpMatch.build());
439 } else if (ipv6Match != null) {
440 matchBuilder.setLayer3Match(ipv6Match.build());
442 if (protocolMatchFields != null) {
443 matchBuilder.setProtocolMatchFields(protocolMatchFields.build());
445 return matchBuilder.build();
449 * Fills the bitmask from boolean map where key is bit position
452 * bit to boolean mapping
455 private int fillBitMaskFromMap(Map<Integer, Boolean> booleanMap) {
458 for (Entry<Integer, Boolean> iterator : booleanMap.entrySet()) {
459 if (iterator.getValue() != null && iterator.getValue().booleanValue()) {
460 bitmask |= 1 << iterator.getKey();
466 private Ipv4Prefix toIpv4Prefix(MatchEntries entry) {
467 String ipv4Prefix = entry.getAugmentation(Ipv4AddressMatchEntry.class).getIpv4Address().toString();
468 if (entry.isHasMask()) {
469 byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();
470 ipv4Prefix = ipv4Prefix + PREFIX_SEPARATOR + countBits(mask);
472 return new Ipv4Prefix(ipv4Prefix);
475 private Ipv6Prefix toIpv6Prefix(MatchEntries entry) {
476 String ipv6Prefix = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().toString();
477 if (entry.isHasMask()) {
478 byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();
479 ipv6Prefix = ipv6Prefix + PREFIX_SEPARATOR + countBits(mask);
481 return new Ipv6Prefix(ipv6Prefix);
484 private int toInt(byte b) {
485 return b < 0 ? b + 256 : b;
488 private int countBits(byte[] mask) {
490 for (byte b : mask) {
491 netmask += Integer.bitCount(toInt(b));