2 * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 * Contributor: deepthi.v.v@ericsson.com
10 package org.opendaylight.openflowplugin.openflow.md.core.translator;
12 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
13 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
14 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
15 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
16 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
17 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
18 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
19 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowVersion;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6FlowLabel;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemovedBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.RemovedReasonFlags;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.Match;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeaderBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6LabelBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFieldsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.protocol.match.fields.PbbBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.BosMatchEntry;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DscpMatchEntry;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntry;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntry;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4CodeMatchEntry;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4TypeMatchEntry;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntry;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6TypeMatchEntry;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntry;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntry;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntry;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntry;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsLabelMatchEntry;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OpCodeMatchEntry;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortMatchEntry;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntry;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntry;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntry;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpOp;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSha;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSpa;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTha;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTpa;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthDst;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthSrc;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthType;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Code;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Type;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Code;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Type;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPhyPort;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpDscp;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpEcn;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpProto;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Dst;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Dst;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Exthdr;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Flabel;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdSll;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTarget;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTll;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Src;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MatchField;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Metadata;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsBos;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsLabel;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsTc;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.PbbIsid;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpDst;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpSrc;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpDst;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpSrc;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelId;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpDst;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpSrc;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanPcp;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanVid;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
126 import org.opendaylight.yangtools.yang.binding.DataObject;
127 import org.slf4j.Logger;
128 import org.slf4j.LoggerFactory;
130 import java.math.BigInteger;
131 import java.util.Collections;
132 import java.util.HashMap;
133 import java.util.List;
134 import java.util.Map;
135 import java.util.Map.Entry;
136 import java.util.concurrent.CopyOnWriteArrayList;
138 public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
140 protected static final Logger LOG = LoggerFactory.getLogger(FlowRemovedTranslator.class);
141 private static final String PREFIX_SEPARATOR = "/";
144 public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sc, OfHeader msg) {
145 if (msg instanceof FlowRemovedMessage) {
146 FlowRemovedMessage ofFlow = (FlowRemovedMessage) msg;
147 List<DataObject> list = new CopyOnWriteArrayList<DataObject>();
148 LOG.debug("Flow Removed Message received: Table Id={}, Flow removed reason={} ", ofFlow.getTableId(),
151 SwitchFlowRemovedBuilder salFlowRemoved = new SwitchFlowRemovedBuilder();
153 if (ofFlow.getCookie() != null) {
154 salFlowRemoved.setCookie(new FlowCookie(ofFlow.getCookie()));
156 salFlowRemoved.setPriority(ofFlow.getPriority());
158 if (ofFlow.getTableId() != null) {
159 salFlowRemoved.setTableId(ofFlow.getTableId().getValue().shortValue());
162 salFlowRemoved.setDurationSec(ofFlow.getDurationSec());
163 salFlowRemoved.setDurationNsec(ofFlow.getDurationNsec());
164 salFlowRemoved.setIdleTimeout(ofFlow.getIdleTimeout());
165 salFlowRemoved.setHardTimeout(ofFlow.getHardTimeout());
166 salFlowRemoved.setPacketCount(ofFlow.getPacketCount());
167 salFlowRemoved.setByteCount(ofFlow.getByteCount());
168 RemovedReasonFlags removeReasonFlag = new RemovedReasonFlags(
169 FlowRemovedReason.OFPRRDELETE.equals(ofFlow.getReason()),
170 FlowRemovedReason.OFPRRGROUPDELETE.equals(ofFlow.getReason()),
171 FlowRemovedReason.OFPRRHARDTIMEOUT.equals(ofFlow.getReason()),
172 FlowRemovedReason.OFPRRIDLETIMEOUT.equals(ofFlow.getReason())
175 salFlowRemoved.setRemovedReason(removeReasonFlag);
177 OpenflowVersion ofVersion = OpenflowVersion.get(sc.getPrimaryConductor().getVersion());
178 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match ofMatch = ofFlow
180 if (ofMatch != null) {
181 salFlowRemoved.setMatch(fromMatch(ofMatch, sc.getFeatures().getDatapathId(), ofVersion));
183 else if(ofFlow.getMatchV10() != null){
184 MatchBuilder matchBuilder = new MatchBuilder(MatchConvertorImpl.fromOFMatchV10ToSALMatch(ofFlow.getMatchV10(), sc.getFeatures().getDatapathId(), ofVersion));
185 salFlowRemoved.setMatch(matchBuilder.build());
187 salFlowRemoved.setNode(new NodeRef(InventoryDataServiceUtil.identifierFromDatapathId(sc.getFeatures()
189 list.add(salFlowRemoved.build());
192 LOG.error("Message is not a flow removed message ");
193 return Collections.emptyList();
198 public Match fromMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match ofMatch,
199 BigInteger datapathid, OpenflowVersion ofVersion) {
200 MatchBuilder matchBuilder = new MatchBuilder();
201 EthernetMatchBuilder ethernetMatch = null;
202 VlanMatchBuilder vlanMatch = null;
203 IpMatchBuilder ipMatch = null;
204 TcpMatchBuilder tcpMatch = null;
205 UdpMatchBuilder udpMatch = null;
206 SctpMatchBuilder sctpMatch = null;
207 Icmpv4MatchBuilder icmpv4Match = null;
208 Icmpv6MatchBuilder icmpv6Match = null;
209 Ipv4MatchBuilder ipv4Match = null;
210 ArpMatchBuilder arpMatch = null;
211 Ipv6MatchBuilder ipv6Match = null;
212 ProtocolMatchFieldsBuilder protocolMatchFields = null;
214 for (MatchEntries entry : ofMatch.getMatchEntries()) {
215 Class<? extends MatchField> field = entry.getOxmMatchField();
216 if (field.equals(InPort.class)) {
217 matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
218 entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue().longValue(), ofVersion));
219 } else if (field.equals(InPhyPort.class)) {
220 matchBuilder.setInPhyPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
221 entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue().longValue(), ofVersion));
222 } else if (field.equals(Metadata.class)) {
223 MetadataBuilder metadata = new MetadataBuilder();
224 metadata.setMetadata(new BigInteger(1, entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
225 if (entry.isHasMask()) {
226 metadata.setMetadataMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, entry.getAugmentation(MaskMatchEntry.class).getMask()));
228 matchBuilder.setMetadata(metadata.build());
229 } else if (field.equals(EthDst.class) || field.equals(EthSrc.class) || field.equals(EthType.class)) {
230 if (ethernetMatch == null) {
231 ethernetMatch = new EthernetMatchBuilder();
233 if (field.equals(EthDst.class)) {
234 EthernetDestinationBuilder ethDst = new EthernetDestinationBuilder();
235 ethDst.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
236 if (entry.isHasMask()) {
237 ethDst.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(),":")));
239 ethernetMatch.setEthernetDestination(ethDst.build());
240 } else if (field.equals(EthSrc.class)) {
241 EthernetSourceBuilder ethSrc = new EthernetSourceBuilder();
242 ethSrc.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
243 if (entry.isHasMask()) {
244 ethSrc.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(),":")));
246 ethernetMatch.setEthernetSource(ethSrc.build());
247 } else if (field.equals(EthType.class)) {
248 EthernetTypeBuilder ethType = new EthernetTypeBuilder();
249 ethType.setType(new EtherType(entry.getAugmentation(EthTypeMatchEntry.class).getEthType()
250 .getValue().longValue()));
251 ethernetMatch.setEthernetType(ethType.build());
253 } else if (field.equals(VlanVid.class) || field.equals(VlanPcp.class)) {
254 if (vlanMatch == null) {
255 vlanMatch = new VlanMatchBuilder();
257 if (field.equals(VlanVid.class)) {
258 boolean vlanIdPresent = false;
259 VlanIdBuilder vlanId = new VlanIdBuilder();
260 VlanVidMatchEntry vlanVid = entry.getAugmentation(VlanVidMatchEntry.class);
261 Integer vlanVidValue = vlanVid.getVlanVid();
262 if (vlanVid.isCfiBit()) {
263 vlanIdPresent = true;
265 vlanId.setVlanIdPresent(vlanIdPresent);
266 if (vlanVidValue != null) {
267 vlanId.setVlanId(new VlanId(vlanVidValue));
269 vlanMatch.setVlanId(vlanId.build());
270 } else if (field.equals(VlanPcp.class)) {
271 vlanMatch.setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
272 entry.getAugmentation(VlanPcpMatchEntry.class).getVlanPcp().shortValue()));
274 } else if (field.equals(IpDscp.class) || field.equals(IpEcn.class) || field.equals(IpProto.class)) {
275 if (ipMatch == null) {
276 ipMatch = new IpMatchBuilder();
278 if (field.equals(IpDscp.class)) {
279 ipMatch.setIpDscp(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp(
280 entry.getAugmentation(DscpMatchEntry.class).getDscp().getValue()));
281 } else if (field.equals(IpEcn.class)) {
282 ipMatch.setIpEcn(entry.getAugmentation(EcnMatchEntry.class).getEcn());
283 } else if (field.equals(IpProto.class)) {
284 ipMatch.setIpProtocol(entry.getAugmentation(ProtocolNumberMatchEntry.class).getProtocolNumber());
286 } else if (field.equals(TcpSrc.class) || field.equals(TcpDst.class)) {
287 if (tcpMatch == null) {
288 tcpMatch = new TcpMatchBuilder();
290 if (field.equals(TcpSrc.class)) {
291 tcpMatch.setTcpSourcePort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
293 } else if (field.equals(TcpDst.class)) {
294 tcpMatch.setTcpDestinationPort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
297 } else if (field.equals(UdpSrc.class) || field.equals(UdpDst.class)) {
298 if (udpMatch == null) {
299 udpMatch = new UdpMatchBuilder();
301 if (field.equals(UdpSrc.class)) {
302 udpMatch.setUdpSourcePort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
304 } else if (field.equals(UdpDst.class)) {
305 udpMatch.setUdpDestinationPort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
308 } else if (field.equals(SctpSrc.class) || field.equals(SctpDst.class)) {
309 if (sctpMatch == null) {
310 sctpMatch = new SctpMatchBuilder();
312 if (field.equals(SctpSrc.class)) {
313 sctpMatch.setSctpSourcePort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
315 } else if (field.equals(SctpDst.class)) {
316 sctpMatch.setSctpDestinationPort(new PortNumber(entry.getAugmentation(PortMatchEntry.class)
317 .getPort().getValue()));
319 } else if (field.equals(Icmpv4Type.class) || field.equals(Icmpv4Code.class)) {
320 if (icmpv4Match == null) {
321 icmpv4Match = new Icmpv4MatchBuilder();
323 if (field.equals(Icmpv4Type.class)) {
324 icmpv4Match.setIcmpv4Type(entry.getAugmentation(Icmpv4TypeMatchEntry.class).getIcmpv4Type());
325 } else if (field.equals(Icmpv4Code.class)) {
326 icmpv4Match.setIcmpv4Code(entry.getAugmentation(Icmpv4CodeMatchEntry.class).getIcmpv4Code());
328 } else if (field.equals(Icmpv6Type.class) || field.equals(Icmpv6Code.class)) {
329 if (icmpv6Match == null) {
330 icmpv6Match = new Icmpv6MatchBuilder();
332 if (field.equals(Icmpv6Type.class)) {
333 icmpv6Match.setIcmpv6Type(entry.getAugmentation(Icmpv6TypeMatchEntry.class).getIcmpv6Type());
334 } else if (field.equals(Icmpv6Code.class)) {
335 icmpv6Match.setIcmpv6Code(entry.getAugmentation(Icmpv6CodeMatchEntry.class).getIcmpv6Code());
337 } else if (field.equals(Ipv4Src.class) || field.equals(Ipv4Dst.class)) {
338 if (ipv4Match == null) {
339 ipv4Match = new Ipv4MatchBuilder();
341 if (field.equals(Ipv4Src.class)) {
342 ipv4Match.setIpv4Source(toIpv4Prefix(entry));
343 } else if (field.equals(Ipv4Dst.class)) {
344 ipv4Match.setIpv4Destination(toIpv4Prefix(entry));
346 } else if (field.equals(ArpOp.class) || field.equals(ArpSpa.class) || field.equals(ArpTpa.class)
347 || field.equals(ArpSha.class) || field.equals(ArpTha.class)) {
348 if (arpMatch == null) {
349 arpMatch = new ArpMatchBuilder();
351 if (field.equals(ArpOp.class)) {
352 arpMatch.setArpOp(entry.getAugmentation(OpCodeMatchEntry.class).getOpCode());
353 } else if (field.equals(ArpSpa.class)) {
354 arpMatch.setArpSourceTransportAddress(toIpv4Prefix(entry));
355 } else if (field.equals(ArpTpa.class)) {
356 arpMatch.setArpTargetTransportAddress(toIpv4Prefix(entry));
357 } else if (field.equals(ArpSha.class)) {
358 ArpSourceHardwareAddressBuilder arpSha = new ArpSourceHardwareAddressBuilder();
359 arpSha.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
360 if (entry.isHasMask()) {
361 arpSha.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(),":")));
363 arpMatch.setArpSourceHardwareAddress(arpSha.build());
364 } else if (field.equals(ArpTha.class)) {
365 ArpTargetHardwareAddressBuilder arpTha = new ArpTargetHardwareAddressBuilder();
366 arpTha.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
367 if (entry.isHasMask()) {
368 arpTha.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(),":")));
370 arpMatch.setArpTargetHardwareAddress(arpTha.build());
372 } else if (field.equals(Ipv6Src.class) || field.equals(Ipv6Dst.class) || field.equals(Ipv6Flabel.class)
373 || field.equals(Ipv6NdTarget.class) || field.equals(Ipv6NdSll.class)
374 || field.equals(Ipv6NdTll.class) || field.equals(Ipv6Exthdr.class)) {
375 if (ipv6Match == null) {
376 ipv6Match = new Ipv6MatchBuilder();
378 if (field.equals(Ipv6Src.class)) {
379 ipv6Match.setIpv6Source(toIpv6Prefix(entry));
380 } else if (field.equals(Ipv6Dst.class)) {
381 ipv6Match.setIpv6Destination(toIpv6Prefix(entry));
382 } else if (field.equals(Ipv6Flabel.class)) {
383 Ipv6LabelBuilder ipv6Label = new Ipv6LabelBuilder();
384 ipv6Label.setIpv6Flabel(entry.getAugmentation(Ipv6FlabelMatchEntry.class).getIpv6Flabel());
385 if (entry.isHasMask()) {
386 ipv6Label.setFlabelMask(new Ipv6FlowLabel(ByteUtil.bytesToUnsignedInt(entry.getAugmentation(MaskMatchEntry.class).getMask())));
388 ipv6Match.setIpv6Label(ipv6Label.build());
389 } else if (field.equals(Ipv6NdTarget.class)) {
390 ipv6Match.setIpv6NdTarget(entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address());
391 } else if (field.equals(Ipv6NdSll.class)) {
392 ipv6Match.setIpv6NdSll(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
393 } else if (field.equals(Ipv6NdTll.class)) {
394 ipv6Match.setIpv6NdTll(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
395 } else if (field.equals(Ipv6Exthdr.class)) {
397 Ipv6ExtHeaderBuilder ipv6ExtHeaderBuilder = new Ipv6ExtHeaderBuilder();
398 Ipv6ExthdrFlags pseudoField = entry.getAugmentation(PseudoFieldMatchEntry.class).getPseudoField();
399 Map<Integer, Boolean> map = new HashMap<>();
400 map.put(0, pseudoField.isNonext());
401 map.put(1, pseudoField.isEsp());
402 map.put(2, pseudoField.isAuth());
403 map.put(3, pseudoField.isDest());
404 map.put(4, pseudoField.isFrag());
405 map.put(5, pseudoField.isRouter());
406 map.put(6, pseudoField.isHop());
407 map.put(7, pseudoField.isUnrep());
408 map.put(8, pseudoField.isUnseq());
409 int bitmap = fillBitMaskFromMap(map);
410 ipv6ExtHeaderBuilder.setIpv6Exthdr(bitmap);
411 if (entry.isHasMask()) {
412 ipv6ExtHeaderBuilder.setIpv6ExthdrMask(
413 ByteUtil.bytesToUnsignedShort(entry.getAugmentation(MaskMatchEntry.class).getMask()));
415 ipv6Match.setIpv6ExtHeader(ipv6ExtHeaderBuilder.build());
417 } else if (field.equals(MplsLabel.class) || field.equals(MplsTc.class) || field.equals(MplsBos.class)
418 || field.equals(PbbIsid.class)) {
419 if (protocolMatchFields == null) {
420 protocolMatchFields = new ProtocolMatchFieldsBuilder();
422 if (field.equals(MplsLabel.class)) {
423 protocolMatchFields.setMplsLabel(entry.getAugmentation(MplsLabelMatchEntry.class).getMplsLabel());
424 } else if (field.equals(MplsTc.class)) {
425 protocolMatchFields.setMplsTc(entry.getAugmentation(TcMatchEntry.class).getTc());
426 } else if (field.equals(MplsBos.class)) {
428 .setMplsBos((short) (entry.getAugmentation(BosMatchEntry.class).isBos() ? 1 : 0));
429 } else if (field.equals(PbbIsid.class)) {
430 PbbBuilder pbb = new PbbBuilder();
431 pbb.setPbbIsid(entry.getAugmentation(IsidMatchEntry.class).getIsid());
432 if (entry.isHasMask()) {
433 pbb.setPbbMask(ByteUtil.bytesToUnsignedInt(entry.getAugmentation(MaskMatchEntry.class).getMask()));
435 protocolMatchFields.setPbb(pbb.build());
437 } else if (field.equals(TunnelId.class)) {
438 TunnelBuilder tunnel = new TunnelBuilder();
439 tunnel.setTunnelId(new BigInteger(1, entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
440 if (entry.isHasMask()) {
441 tunnel.setTunnelMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, entry.getAugmentation(MaskMatchEntry.class).getMask()));
443 matchBuilder.setTunnel(tunnel.build());
447 if (ethernetMatch != null) {
448 matchBuilder.setEthernetMatch(ethernetMatch.build());
450 if (vlanMatch != null) {
451 matchBuilder.setVlanMatch(vlanMatch.build());
453 if (ipMatch != null) {
454 matchBuilder.setIpMatch(ipMatch.build());
457 if (tcpMatch != null) {
458 matchBuilder.setLayer4Match(tcpMatch.build());
459 } else if (udpMatch != null) {
460 matchBuilder.setLayer4Match(udpMatch.build());
461 } else if (sctpMatch != null) {
462 matchBuilder.setLayer4Match(sctpMatch.build());
465 if (icmpv4Match != null) {
466 matchBuilder.setIcmpv4Match(icmpv4Match.build());
467 } else if (icmpv6Match != null) {
468 matchBuilder.setIcmpv6Match(icmpv6Match.build());
471 if (ipv4Match != null) {
472 matchBuilder.setLayer3Match(ipv4Match.build());
473 } else if (arpMatch != null) {
474 matchBuilder.setLayer3Match(arpMatch.build());
475 } else if (ipv6Match != null) {
476 matchBuilder.setLayer3Match(ipv6Match.build());
478 if (protocolMatchFields != null) {
479 matchBuilder.setProtocolMatchFields(protocolMatchFields.build());
481 return matchBuilder.build();
485 * Fills the bitmask from boolean map where key is bit position
488 * bit to boolean mapping
491 private int fillBitMaskFromMap(Map<Integer, Boolean> booleanMap) {
494 for (Entry<Integer, Boolean> iterator : booleanMap.entrySet()) {
495 if (iterator.getValue() != null && iterator.getValue().booleanValue()) {
496 bitmask |= 1 << iterator.getKey();
502 private Ipv4Prefix toIpv4Prefix(MatchEntries entry) {
503 String ipv4Prefix = entry.getAugmentation(Ipv4AddressMatchEntry.class).getIpv4Address().toString();
504 if (entry.isHasMask()) {
505 byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();
506 ipv4Prefix = ipv4Prefix + PREFIX_SEPARATOR + countBits(mask);
508 return new Ipv4Prefix(ipv4Prefix);
511 private Ipv6Prefix toIpv6Prefix(MatchEntries entry) {
512 String ipv6Prefix = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().toString();
513 if (entry.isHasMask()) {
514 byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();
515 ipv6Prefix = ipv6Prefix + PREFIX_SEPARATOR + countBits(mask);
517 return new Ipv6Prefix(ipv6Prefix);
520 private int toInt(byte b) {
521 return b < 0 ? b + 256 : b;
524 private int countBits(byte[] mask) {
526 for (byte b : mask) {
527 netmask += Integer.bitCount(toInt(b));