2 * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.controller.sal.compatibility;
10 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.CRUDP;
11 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.ETHERNET_ARP;
12 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.TCP;
13 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.UDP;
14 import static org.opendaylight.controller.sal.match.MatchType.DL_DST;
15 import static org.opendaylight.controller.sal.match.MatchType.DL_SRC;
16 import static org.opendaylight.controller.sal.match.MatchType.DL_TYPE;
17 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN;
18 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN_PR;
19 import static org.opendaylight.controller.sal.match.MatchType.NW_DST;
20 import static org.opendaylight.controller.sal.match.MatchType.NW_PROTO;
21 import static org.opendaylight.controller.sal.match.MatchType.NW_SRC;
22 import static org.opendaylight.controller.sal.match.MatchType.NW_TOS;
23 import static org.opendaylight.controller.sal.match.MatchType.TP_DST;
24 import static org.opendaylight.controller.sal.match.MatchType.TP_SRC;
26 import java.net.InetAddress;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
31 import org.opendaylight.controller.sal.action.Controller;
32 import org.opendaylight.controller.sal.action.Drop;
33 import org.opendaylight.controller.sal.action.Flood;
34 import org.opendaylight.controller.sal.action.FloodAll;
35 import org.opendaylight.controller.sal.action.HwPath;
36 import org.opendaylight.controller.sal.action.Loopback;
37 import org.opendaylight.controller.sal.action.Output;
38 import org.opendaylight.controller.sal.action.PopVlan;
39 import org.opendaylight.controller.sal.action.PushVlan;
40 import org.opendaylight.controller.sal.action.SetDlDst;
41 import org.opendaylight.controller.sal.action.SetDlSrc;
42 import org.opendaylight.controller.sal.action.SetDlType;
43 import org.opendaylight.controller.sal.action.SetNextHop;
44 import org.opendaylight.controller.sal.action.SetNwDst;
45 import org.opendaylight.controller.sal.action.SetNwSrc;
46 import org.opendaylight.controller.sal.action.SetNwTos;
47 import org.opendaylight.controller.sal.action.SetTpDst;
48 import org.opendaylight.controller.sal.action.SetTpSrc;
49 import org.opendaylight.controller.sal.action.SetVlanCfi;
50 import org.opendaylight.controller.sal.action.SetVlanId;
51 import org.opendaylight.controller.sal.action.SetVlanPcp;
52 import org.opendaylight.controller.sal.action.SwPath;
53 import org.opendaylight.controller.sal.core.ConstructionException;
54 import org.opendaylight.controller.sal.core.Node;
55 import org.opendaylight.controller.sal.core.NodeConnector;
56 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
57 import org.opendaylight.controller.sal.flowprogrammer.Flow;
58 import org.opendaylight.controller.sal.match.Match;
59 import org.opendaylight.controller.sal.match.MatchType;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.ControllerActionCase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodActionCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAllActionCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.HwPathActionCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.LoopbackActionCase;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCase;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCase;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsActionCase;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbActionCase;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCase;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCase;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCase;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlTypeActionCase;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetMplsTtlActionCase;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNextHopActionCase;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCase;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCase;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTosActionCase;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTtlActionCase;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetQueueActionCase;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCase;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpSrcActionCase;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanCfiActionCase;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCase;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanPcpActionCase;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SwPathActionCase;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.GenericFlowAttributes;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.MacAddressFilter;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddress;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
122 import org.slf4j.Logger;
123 import org.slf4j.LoggerFactory;
125 import com.google.common.net.InetAddresses;
127 public class ToSalConversionsUtils {
129 private static final Logger LOG = LoggerFactory.getLogger(ToSalConversionsUtils.class);
131 private ToSalConversionsUtils() {
135 public static Flow toFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source, Node node) {
136 final Flow target = new Flow();
137 genericFlowToAdFlow(source, target);
139 target.setMatch(toMatch(source.getMatch()));
141 List<Action> actions = getAction(source);
142 if (actions != null) {
143 target.setActions(actionFrom(actions, node));
150 * @param source notification, missing instructions
151 * @param node corresponding node where the flow change occured
152 * @return ad-sal node, build from given data
154 public static Flow toFlow(SwitchFlowRemoved source, Node node) {
155 final Flow target = new Flow();
156 genericFlowToAdFlow(source, target);
158 target.setMatch(toMatch(source.getMatch()));
167 private static void genericFlowToAdFlow(GenericFlowAttributes source,
169 Integer hardTimeout = source.getHardTimeout();
170 if (hardTimeout != null) {
171 target.setHardTimeout(hardTimeout.shortValue());
174 Integer idleTimeout = source.getIdleTimeout();
175 if (idleTimeout != null) {
176 target.setIdleTimeout(idleTimeout.shortValue());
179 Integer priority = source.getPriority();
180 if (priority != null) {
181 target.setPriority(priority.shortValue());
183 target.setId(source.getCookie().getValue().longValue());
186 public static List<Action> getAction(
187 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source) {
188 if (source.getInstructions() != null) {
189 for (Instruction instruction : source.getInstructions().getInstruction()) {
190 if (instruction.getInstruction() instanceof ApplyActionsCase) {
191 return (((ApplyActionsCase) instruction.getInstruction()).getApplyActions().getAction());
195 // TODO Auto-generated method stub
196 return Collections.emptyList();
199 public static List<org.opendaylight.controller.sal.action.Action> actionFrom(List<Action> actions, Node node) {
200 List<org.opendaylight.controller.sal.action.Action> targetAction = new ArrayList<>();
201 for (Action action : actions) {
202 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action sourceAction = action
205 if (sourceAction instanceof ControllerActionCase) {
206 targetAction.add(new Controller());
207 } else if (sourceAction instanceof OutputActionCase) {
209 Uri nodeConnector = ((OutputActionCase) sourceAction).getOutputAction().getOutputNodeConnector();
210 if (nodeConnector != null) {
211 //for (Uri uri : nodeConnectors) {
212 targetAction.add(new Output(fromNodeConnectorRef(nodeConnector, node)));
215 } else if (sourceAction instanceof PopMplsActionCase) {
216 // TODO: define maping
217 } else if (sourceAction instanceof PushMplsActionCase) {
218 // TODO: define maping
219 } else if (sourceAction instanceof PushPbbActionCase) {
220 // TODO: define maping
221 } else if (sourceAction instanceof SetMplsTtlActionCase) {
222 // TODO: define maping
223 // targetAction = //no action to map
224 } else if (sourceAction instanceof SetNwTtlActionCase) {
225 // TODO: define maping
226 } else if (sourceAction instanceof SetQueueActionCase) {
227 // TODO: define maping
228 // targetAction = //no action to map
229 } else if (sourceAction instanceof DropActionCase) {
230 targetAction.add(new Drop());
231 } else if (sourceAction instanceof FloodActionCase) {
232 targetAction.add(new Flood());
233 } else if (sourceAction instanceof FloodAllActionCase) {
234 targetAction.add(new FloodAll());
235 } else if (sourceAction instanceof HwPathActionCase) {
236 targetAction.add(new HwPath());
237 } else if (sourceAction instanceof LoopbackActionCase) {
238 targetAction.add(new Loopback());
239 } else if (sourceAction instanceof PopVlanActionCase) {
240 targetAction.add(new PopVlan());
241 } else if (sourceAction instanceof PushVlanActionCase) {
242 PushVlanActionCase pushVlanAction = (PushVlanActionCase) sourceAction;
243 PushVlan pushVlan = pushVlanFrom(pushVlanAction.getPushVlanAction());
244 if (pushVlan != null) {
245 targetAction.add(pushVlan);
247 } else if (sourceAction instanceof SetDlDstActionCase) {
248 MacAddress addressL2Dest = ((SetDlDstActionCase) sourceAction).getSetDlDstAction().getAddress();
249 if (addressL2Dest != null) {
250 targetAction.add(new SetDlDst(bytesFrom(addressL2Dest)));
252 } else if (sourceAction instanceof SetDlSrcActionCase) {
253 MacAddress addressL2Src = ((SetDlSrcActionCase) sourceAction).getSetDlSrcAction().getAddress();
254 if (addressL2Src != null) {
255 targetAction.add(new SetDlSrc(bytesFrom(addressL2Src)));
258 } else if (sourceAction instanceof SetDlTypeActionCase) {
259 EtherType dlType = ((SetDlTypeActionCase) sourceAction).getSetDlTypeAction().getDlType();
260 if (dlType != null) {
261 Long dlTypeValue = dlType.getValue();
262 if (dlTypeValue != null) {
263 targetAction.add(new SetDlType(dlTypeValue.intValue()));
266 } else if (sourceAction instanceof SetNextHopActionCase) {
267 Address addressL3 = ((SetNextHopActionCase) sourceAction).getSetNextHopAction().getAddress();
269 InetAddress inetAddress = inetAddressFrom(addressL3);
270 if (inetAddress != null) {
271 targetAction.add(new SetNextHop(inetAddress));
273 } else if (sourceAction instanceof SetNwDstActionCase) {
274 Address addressL3 = ((SetNwDstActionCase) sourceAction).getSetNwDstAction().getAddress();
276 InetAddress inetAddress = inetAddressFrom(addressL3);
277 if (inetAddress != null) {
278 targetAction.add(new SetNwDst(inetAddress));
280 } else if (sourceAction instanceof SetNwSrcActionCase) {
281 Address addressL3 = ((SetNwSrcActionCase) sourceAction).getSetNwSrcAction().getAddress();
283 InetAddress inetAddress = inetAddressFrom(addressL3);
284 if (inetAddress != null) {
285 targetAction.add(new SetNwSrc(inetAddress));
287 } else if (sourceAction instanceof SetNwTosActionCase) {
288 Integer tos = ((SetNwTosActionCase) sourceAction).getSetNwTosAction().getTos();
290 targetAction.add(new SetNwTos(tos));
292 } else if (sourceAction instanceof SetTpDstActionCase) {
293 PortNumber port = ((SetTpDstActionCase) sourceAction).getSetTpDstAction().getPort();
295 Integer portValue = port.getValue();
296 if (port.getValue() != null) {
297 targetAction.add(new SetTpDst(portValue));
300 } else if (sourceAction instanceof SetTpSrcActionCase) {
301 PortNumber port = ((SetTpSrcActionCase) sourceAction).getSetTpSrcAction().getPort();
303 Integer portValue = port.getValue();
304 if (port.getValue() != null) {
305 targetAction.add(new SetTpSrc(portValue));
308 } else if (sourceAction instanceof SetVlanCfiActionCase) {
309 VlanCfi vlanCfi = ((SetVlanCfiActionCase) sourceAction).getSetVlanCfiAction().getVlanCfi();
310 if (vlanCfi != null) {
311 Integer vlanCfiValue = vlanCfi.getValue();
312 if (vlanCfiValue != null) {
313 targetAction.add(new SetVlanCfi(vlanCfiValue));
316 } else if (sourceAction instanceof SetVlanIdActionCase) {
317 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdActionCase) sourceAction).getSetVlanIdAction()
319 if (vlanID != null) {
320 Integer vlanIdValue = vlanID.getValue();
321 if (vlanIdValue != null) {
322 targetAction.add(new SetVlanId(vlanIdValue));
325 } else if (sourceAction instanceof SetVlanPcpActionCase) {
326 VlanPcp vlanPcp = ((SetVlanPcpActionCase) sourceAction).getSetVlanPcpAction().getVlanPcp();
327 if (vlanPcp != null) {
328 Short vlanPcpValue = vlanPcp.getValue();
329 if (vlanPcpValue != null) {
330 targetAction.add(new SetVlanPcp(vlanPcpValue));
333 } else if (sourceAction instanceof SwPathActionCase) {
334 targetAction.add(new SwPath());
341 private static InetAddress inetAddressFrom(Address addressL3) {
342 if (addressL3 != null) {
343 if (addressL3 instanceof Ipv4) {
344 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
345 if (addressL3Ipv4 != null) {
346 return inetAddressFrom(addressL3Ipv4);
348 } else if (addressL3 instanceof Ipv6) {
349 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
350 if (addressL3Ipv6 != null) {
351 return inetAddressFrom(addressL3Ipv6);
358 private static PushVlan pushVlanFrom(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanAction pushVlanAction) {
364 if (pushVlanAction.getTag() != null) {
365 tag = pushVlanAction.getTag();
366 if (pushVlanAction.getPcp() != null) {
367 pcp = pushVlanAction.getPcp();
368 if (pushVlanAction.getCfi() != null && pushVlanAction.getCfi().getValue() != null) {
369 cfi = pushVlanAction.getCfi().getValue();
370 if (pushVlanAction.getVlanId() != null && pushVlanAction.getVlanId().getValue() != null) {
371 vlanId = pushVlanAction.getVlanId().getValue();
372 return new PushVlan(tag, pcp, cfi, vlanId);
381 * @param openflow nodeConnector uri
383 * @return assembled nodeConnector
385 public static NodeConnector fromNodeConnectorRef(Uri uri, Node node) {
386 NodeConnector nodeConnector = null;
388 nodeConnector = new NodeConnector(NodeConnectorIDType.OPENFLOW,
389 Short.valueOf(uri.getValue()), node);
390 } catch (ConstructionException e) {
393 return nodeConnector;
396 public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match source) {
397 Match target = new Match();
398 if (source != null) {
399 fillFrom(target, source.getVlanMatch());
400 fillFrom(target, source.getEthernetMatch());
401 fillFrom(target, source.getLayer3Match());
402 fillFrom(target, source.getLayer4Match());
403 fillFrom(target, source.getIpMatch());
404 fillFrom(target, source.getInPort());
414 private static void fillFrom(Match target, NodeConnectorId inPort) {
415 if (inPort != null) {
416 String inPortValue = inPort.getValue();
417 if (inPortValue != null) {
419 target.setField(MatchType.IN_PORT, NodeMapping.toADNodeConnector(inPort,
420 NodeMapping.toAdNodeId(inPort)));
421 } catch (ConstructionException e) {
422 LOG.warn("nodeConnector construction failed", e);
428 private static void fillFrom(Match target, VlanMatch vlanMatch) {
429 if (vlanMatch != null) {
430 VlanId vlanId = vlanMatch.getVlanId();
431 if (vlanId != null) {
432 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
434 if (vlanIdInner != null) {
435 Integer vlanValue = vlanIdInner.getValue();
436 if (vlanValue != null) {
437 target.setField(DL_VLAN, vlanValue.shortValue());
441 VlanPcp vlanPcp = vlanMatch.getVlanPcp();
442 if (vlanPcp != null) {
443 Short vlanPcpValue = vlanPcp.getValue();
444 if (vlanPcpValue != null) {
445 target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
451 private static void fillFrom(Match target, IpMatch ipMatch) {
452 if (ipMatch != null) {
453 Short ipProtocol = ipMatch.getIpProtocol();
455 if (ipProtocol != null && target.getField(NW_PROTO) == null) {
456 target.setField(NW_PROTO, ipProtocol.byteValue());
458 Dscp dscp = ipMatch.getIpDscp();
460 Short dscpValue = dscp.getValue();
461 if (dscpValue != null) {
462 target.setField(NW_TOS, dscpValue.byteValue());
468 private static void fillFrom(Match target, Layer4Match layer4Match) {
469 if (layer4Match == null) {
472 if (layer4Match instanceof SctpMatch) {
473 fillTransportLayer(target, (SctpMatch) layer4Match);
474 } else if (layer4Match instanceof TcpMatch) {
475 fillTransportLayer(target, (TcpMatch) layer4Match);
476 } else if (layer4Match instanceof UdpMatch) {
477 fillTransportLayer(target, (UdpMatch) layer4Match);
481 private static void fillTransportLayer(Match target, UdpMatch source) {
482 PortNumber udpSourcePort = source.getUdpSourcePort();
483 if (udpSourcePort != null) {
484 Integer udpSourcePortValue = udpSourcePort.getValue();
485 if (udpSourcePortValue != null) {
486 target.setField(TP_SRC, udpSourcePortValue.shortValue());
490 PortNumber udpDestPort = source.getUdpDestinationPort();
491 if (udpDestPort != null) {
492 Integer udpDestPortValue = udpDestPort.getValue();
493 if (udpDestPortValue != null) {
494 target.setField(TP_DST, udpDestPortValue.shortValue());
498 target.setField(NW_PROTO, UDP);
501 private static void fillTransportLayer(Match target, TcpMatch source) {
502 PortNumber tcpSourcePort = source.getTcpSourcePort();
503 if (tcpSourcePort != null) {
504 Integer tcpSourcePortValue = tcpSourcePort.getValue();
505 if (tcpSourcePortValue != null) {
506 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
510 PortNumber tcpDestPort = source.getTcpDestinationPort();
511 if (tcpDestPort != null) {
512 Integer tcpDestPortValue = tcpDestPort.getValue();
513 if (tcpDestPortValue != null) {
514 target.setField(TP_DST, tcpDestPortValue.shortValue());
518 target.setField(NW_PROTO, TCP);
521 private static void fillTransportLayer(Match target, SctpMatch source) {
522 PortNumber sctpSourcePort = source.getSctpSourcePort();
523 if (sctpSourcePort != null) {
524 Integer sctpSourcePortValue = sctpSourcePort.getValue();
525 if (sctpSourcePortValue != null) {
526 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
529 PortNumber sctpDestPort = source.getSctpDestinationPort();
530 if (sctpDestPort != null) {
531 Integer sctpDestPortValue = sctpDestPort.getValue();
532 if (sctpDestPortValue != null) {
533 target.setField(TP_DST, sctpDestPortValue.shortValue());
537 target.setField(NW_PROTO, CRUDP);
541 private static void fillFrom(Match target, Layer3Match source) {
544 if (source instanceof Ipv4Match) {
545 fillFromIpv4(target, (Ipv4Match) source);
546 } else if (source instanceof Ipv6Match) {
547 fillFromIpv6(target, (Ipv6Match) source);
548 } else if (source instanceof ArpMatch) {
549 fillFromArp(target, (ArpMatch) source);
553 private static void fillFromArp(Match target, ArpMatch source) {
554 Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
555 if (sourceAddress != null) {
556 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
558 Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
559 if (destAddress != null) {
560 target.setField(NW_DST, inetAddressFrom(destAddress), null);
562 ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
563 if (sourceHwAddress != null) {
564 target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
566 ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
567 if (targetHwAddress != null) {
568 target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
571 target.setField(DL_TYPE, new Short(ETHERNET_ARP));
575 private static void fillFromIpv6(Match target, Ipv6Match source) {
576 Ipv6Prefix sourceAddress = source.getIpv6Source();
577 if (sourceAddress != null) {
578 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
580 Ipv6Prefix destAddress = source.getIpv6Destination();
581 if (destAddress != null) {
582 target.setField(NW_DST, inetAddressFrom(destAddress), null);
586 private static void fillFromIpv4(Match target, Ipv4Match source) {
587 Ipv4Prefix sourceAddress = source.getIpv4Source();
588 if (sourceAddress != null) {
589 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
591 Ipv4Prefix destAddress = source.getIpv4Destination();
592 if (destAddress != null) {
593 target.setField(NW_DST, inetAddressFrom(destAddress), null);
597 private static InetAddress inetAddressFrom(Ipv4Prefix source) {
598 if (source != null) {
599 String[] parts = source.getValue().split("/");
600 return InetAddresses.forString(parts[0]);
605 private static InetAddress inetAddressFrom(Ipv6Prefix source) {
606 if (source != null) {
607 String[] parts = source.getValue().split("/");
608 return InetAddresses.forString(parts[0]);
613 private static void fillFrom(Match target, EthernetMatch source) {
616 EthernetType ethType = source.getEthernetType();
617 if (ethType != null) {
618 EtherType ethInnerType = ethType.getType();
619 if (ethInnerType != null && target.getField(DL_TYPE) == null) {
620 Long value = ethInnerType.getValue();
621 target.setField(DL_TYPE, value.shortValue());
625 MacAddressFilter ethSource = source.getEthernetSource();
626 if (ethSource != null) {
627 target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
630 MacAddressFilter ethDest = source.getEthernetDestination();
631 if (ethDest != null) {
632 target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
636 public static byte[] bytesFrom(MacAddress address) {
637 String[] mac = address.getValue().split(":");
638 byte[] macAddress = new byte[6]; // mac.length == 6 bytes
639 for (int i = 0; i < mac.length; i++) {
640 macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
645 public static byte[] bytesFromDpid(long dpid) {
646 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
648 for (short i = 0; i < 6; i++) {
649 mac[5 - i] = (byte) dpid;