1 package org.opendaylight.controller.sal.compatibility;
3 import com.google.common.net.InetAddresses;
5 import org.opendaylight.controller.sal.action.Controller;
6 import org.opendaylight.controller.sal.action.Drop;
7 import org.opendaylight.controller.sal.action.Flood;
8 import org.opendaylight.controller.sal.action.FloodAll;
9 import org.opendaylight.controller.sal.action.HwPath;
10 import org.opendaylight.controller.sal.action.Loopback;
11 import org.opendaylight.controller.sal.action.Output;
12 import org.opendaylight.controller.sal.action.PopVlan;
13 import org.opendaylight.controller.sal.action.PushVlan;
14 import org.opendaylight.controller.sal.action.SetDlDst;
15 import org.opendaylight.controller.sal.action.SetDlSrc;
16 import org.opendaylight.controller.sal.action.SetDlType;
17 import org.opendaylight.controller.sal.action.SetNextHop;
18 import org.opendaylight.controller.sal.action.SetNwDst;
19 import org.opendaylight.controller.sal.action.SetNwSrc;
20 import org.opendaylight.controller.sal.action.SetNwTos;
21 import org.opendaylight.controller.sal.action.SetTpDst;
22 import org.opendaylight.controller.sal.action.SetTpSrc;
23 import org.opendaylight.controller.sal.action.SetVlanCfi;
24 import org.opendaylight.controller.sal.action.SetVlanId;
25 import org.opendaylight.controller.sal.action.SetVlanPcp;
26 import org.opendaylight.controller.sal.action.SwPath;
27 import org.opendaylight.controller.sal.core.NodeConnector;
28 import org.opendaylight.controller.sal.flowprogrammer.Flow;
29 import org.opendaylight.controller.sal.match.Match;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.ActionList;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.ControllerAction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropAction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAllAction;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.HwPathAction;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.LoopbackAction;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputAction;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsAction;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanAction;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsAction;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbAction;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanAction;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstAction;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcAction;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlTypeAction;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetMplsTtlAction;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNextHopAction;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstAction;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcAction;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTosAction;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTtlAction;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetQueueAction;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstAction;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpSrcAction;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanCfiAction;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdAction;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanPcpAction;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SwPathAction;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.MacAddressFilter;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddress;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
90 import java.net.InetAddress;
91 import java.util.ArrayList;
92 import java.util.Collections;
93 import java.util.List;
95 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.ETHERNET_ARP;
96 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.CRUDP;
97 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.TCP;
98 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.UDP;
99 import static org.opendaylight.controller.sal.match.MatchType.DL_DST;
100 import static org.opendaylight.controller.sal.match.MatchType.DL_SRC;
101 import static org.opendaylight.controller.sal.match.MatchType.DL_TYPE;
102 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN;
103 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN_PR;
104 import static org.opendaylight.controller.sal.match.MatchType.NW_DST;
105 import static org.opendaylight.controller.sal.match.MatchType.NW_PROTO;
106 import static org.opendaylight.controller.sal.match.MatchType.NW_SRC;
107 import static org.opendaylight.controller.sal.match.MatchType.NW_TOS;
108 import static org.opendaylight.controller.sal.match.MatchType.TP_DST;
109 import static org.opendaylight.controller.sal.match.MatchType.TP_SRC;
111 public class ToSalConversionsUtils {
113 private ToSalConversionsUtils() {
117 public static Flow toFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source) {
118 final Flow target = new Flow();
120 Integer hardTimeout = source.getHardTimeout();
121 if (hardTimeout != null) {
122 target.setHardTimeout(hardTimeout.shortValue());
125 Integer idleTimeout = source.getIdleTimeout();
126 if (idleTimeout != null) {
127 target.setIdleTimeout(idleTimeout.shortValue());
130 Integer priority = source.getPriority();
131 if (priority != null) {
132 target.setPriority(priority.shortValue());
135 target.setMatch(toMatch(source.getMatch()));
137 List<Action> actions = getAction(source);
138 if (actions != null) {
139 target.setActions(actionFrom(actions));
142 target.setId(source.getCookie().longValue());
146 public static List<Action> getAction(
147 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source) {
148 if (source.getInstructions() != null) {
149 for (Instruction instruction : source.getInstructions().getInstruction()) {
150 if (instruction.getInstruction() instanceof ActionList) {
151 return (((ActionList) instruction.getInstruction()).getAction());
155 // TODO Auto-generated method stub
156 return Collections.emptyList();
159 public static List<org.opendaylight.controller.sal.action.Action> actionFrom(List<Action> actions) {
160 List<org.opendaylight.controller.sal.action.Action> targetAction = new ArrayList<>();
161 for (Action action : actions) {
162 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action sourceAction = action
165 if (sourceAction instanceof ControllerAction) {
166 targetAction.add(new Controller());
167 } else if (sourceAction instanceof OutputAction) {
169 Uri nodeConnector = ((OutputAction) sourceAction).getOutputNodeConnector();
170 if (nodeConnector != null) {
171 //for (Uri uri : nodeConnectors) {
172 targetAction.add(new Output(fromNodeConnectorRef(nodeConnector)));
175 } else if (sourceAction instanceof PopMplsAction) {
176 // TODO: define maping
177 } else if (sourceAction instanceof PushMplsAction) {
178 // TODO: define maping
179 } else if (sourceAction instanceof PushPbbAction) {
180 // TODO: define maping
181 } else if (sourceAction instanceof SetMplsTtlAction) {
182 // TODO: define maping
183 // targetAction = //no action to map
184 } else if (sourceAction instanceof SetNwTtlAction) {
185 // TODO: define maping
186 } else if (sourceAction instanceof SetQueueAction) {
187 // TODO: define maping
188 // targetAction = //no action to map
189 } else if (sourceAction instanceof DropAction) {
190 targetAction.add(new Drop());
191 } else if (sourceAction instanceof FloodAction) {
192 targetAction.add(new Flood());
193 } else if (sourceAction instanceof FloodAllAction) {
194 targetAction.add(new FloodAll());
195 } else if (sourceAction instanceof HwPathAction) {
196 targetAction.add(new HwPath());
197 } else if (sourceAction instanceof LoopbackAction) {
198 targetAction.add(new Loopback());
199 } else if (sourceAction instanceof PopVlanAction) {
200 targetAction.add(new PopVlan());
201 } else if (sourceAction instanceof PushVlanAction) {
202 PushVlanAction pushVlanAction = (PushVlanAction) sourceAction;
203 PushVlan pushVlan = pushVlanFrom(pushVlanAction);
204 if (pushVlan != null) {
205 targetAction.add(pushVlan);
207 } else if (sourceAction instanceof SetDlDstAction) {
208 MacAddress addressL2Dest = ((SetDlDstAction) sourceAction).getAddress();
209 if (addressL2Dest != null) {
210 targetAction.add(new SetDlDst(bytesFrom(addressL2Dest)));
212 } else if (sourceAction instanceof SetDlSrcAction) {
213 MacAddress addressL2Src = ((SetDlSrcAction) sourceAction).getAddress();
214 if (addressL2Src != null) {
215 targetAction.add(new SetDlSrc(bytesFrom(addressL2Src)));
218 } else if (sourceAction instanceof SetDlTypeAction) {
219 EtherType dlType = ((SetDlTypeAction) sourceAction).getDlType();
220 if (dlType != null) {
221 Long dlTypeValue = dlType.getValue();
222 if (dlTypeValue != null) {
223 targetAction.add(new SetDlType(dlTypeValue.intValue()));
226 } else if (sourceAction instanceof SetNextHopAction) {
227 Address addressL3 = ((SetNextHopAction) sourceAction).getAddress();
229 InetAddress inetAddress = inetAddressFrom(addressL3);
230 if (inetAddress != null) {
231 targetAction.add(new SetNextHop(inetAddress));
233 } else if (sourceAction instanceof SetNwDstAction) {
234 Address addressL3 = ((SetNwDstAction) sourceAction).getAddress();
236 InetAddress inetAddress = inetAddressFrom(addressL3);
237 if (inetAddress != null) {
238 targetAction.add(new SetNwDst(inetAddress));
240 } else if (sourceAction instanceof SetNwSrcAction) {
241 Address addressL3 = ((SetNwSrcAction) sourceAction).getAddress();
243 InetAddress inetAddress = inetAddressFrom(addressL3);
244 if (inetAddress != null) {
245 targetAction.add(new SetNwSrc(inetAddress));
247 } else if (sourceAction instanceof SetNwTosAction) {
248 Integer tos = ((SetNwTosAction) sourceAction).getTos();
250 targetAction.add(new SetNwTos(tos));
252 } else if (sourceAction instanceof SetTpDstAction) {
253 PortNumber port = ((SetTpDstAction) sourceAction).getPort();
255 Integer portValue = port.getValue();
256 if (port.getValue() != null) {
257 targetAction.add(new SetTpDst(portValue));
260 } else if (sourceAction instanceof SetTpSrcAction) {
261 PortNumber port = ((SetTpSrcAction) sourceAction).getPort();
263 Integer portValue = port.getValue();
264 if (port.getValue() != null) {
265 targetAction.add(new SetTpSrc(portValue));
268 } else if (sourceAction instanceof SetVlanCfiAction) {
269 VlanCfi vlanCfi = ((SetVlanCfiAction) sourceAction).getVlanCfi();
270 if (vlanCfi != null) {
271 Integer vlanCfiValue = vlanCfi.getValue();
272 if (vlanCfiValue != null) {
273 targetAction.add(new SetVlanCfi(vlanCfiValue));
276 } else if (sourceAction instanceof SetVlanIdAction) {
277 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdAction) sourceAction)
279 if (vlanID != null) {
280 Integer vlanIdValue = vlanID.getValue();
281 if (vlanIdValue != null) {
282 targetAction.add(new SetVlanId(vlanIdValue));
285 } else if (sourceAction instanceof SetVlanPcpAction) {
286 VlanPcp vlanPcp = ((SetVlanPcpAction) sourceAction).getVlanPcp();
287 if (vlanPcp != null) {
288 Short vlanPcpValue = vlanPcp.getValue();
289 if (vlanPcpValue != null) {
290 targetAction.add(new SetVlanPcp(vlanPcpValue));
293 } else if (sourceAction instanceof SwPathAction) {
294 targetAction.add(new SwPath());
301 private static InetAddress inetAddressFrom(Address addressL3) {
302 if (addressL3 != null) {
303 if (addressL3 instanceof Ipv4) {
304 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
305 if (addressL3Ipv4 != null) {
306 return inetAddressFrom(addressL3Ipv4);
308 } else if (addressL3 instanceof Ipv6) {
309 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
310 if (addressL3Ipv6 != null) {
311 return inetAddressFrom(addressL3Ipv6);
318 private static PushVlan pushVlanFrom(PushVlanAction pushVlanAction) {
324 if (pushVlanAction.getTag() != null) {
325 tag = pushVlanAction.getTag();
326 if (pushVlanAction.getPcp() != null) {
327 pcp = pushVlanAction.getPcp();
328 if (pushVlanAction.getCfi() != null && pushVlanAction.getCfi().getValue() != null) {
329 cfi = pushVlanAction.getCfi().getValue();
330 if (pushVlanAction.getVlanId() != null && pushVlanAction.getVlanId().getValue() != null) {
331 vlanId = pushVlanAction.getVlanId().getValue();
332 return new PushVlan(tag, pcp, cfi, vlanId);
340 private static NodeConnector fromNodeConnectorRef(Uri uri) {
341 // TODO: Define mapping
345 public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match source) {
346 Match target = new Match();
347 if (source != null) {
348 fillFrom(target, source.getVlanMatch());
349 fillFrom(target, source.getEthernetMatch());
350 fillFrom(target, source.getLayer3Match());
351 fillFrom(target, source.getLayer4Match());
352 fillFrom(target, source.getIpMatch());
358 private static void fillFrom(Match target, VlanMatch vlanMatch) {
359 if (vlanMatch != null) {
360 VlanId vlanId = vlanMatch.getVlanId();
361 if (vlanId != null) {
362 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
364 if (vlanIdInner != null) {
365 Integer vlanValue = vlanIdInner.getValue();
366 if (vlanValue != null) {
367 target.setField(DL_VLAN, vlanValue.shortValue());
371 VlanPcp vlanPcp = vlanMatch.getVlanPcp();
372 if (vlanPcp != null) {
373 Short vlanPcpValue = vlanPcp.getValue();
374 if (vlanPcpValue != null) {
375 target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
381 private static void fillFrom(Match target, IpMatch ipMatch) {
382 if (ipMatch != null) {
383 Short ipProtocol = ipMatch.getIpProtocol();
385 if (ipProtocol != null && target.getField(NW_PROTO) == null) {
386 target.setField(NW_PROTO, ipProtocol.byteValue());
388 Dscp dscp = ipMatch.getIpDscp();
390 Short dscpValue = dscp.getValue();
391 if (dscpValue != null) {
392 target.setField(NW_TOS, dscpValue.byteValue());
398 private static void fillFrom(Match target, Layer4Match layer4Match) {
399 if (layer4Match == null) {
402 if (layer4Match instanceof SctpMatch) {
403 fillTransportLayer(target, (SctpMatch) layer4Match);
404 } else if (layer4Match instanceof TcpMatch) {
405 fillTransportLayer(target, (TcpMatch) layer4Match);
406 } else if (layer4Match instanceof UdpMatch) {
407 fillTransportLayer(target, (UdpMatch) layer4Match);
411 private static void fillTransportLayer(Match target, UdpMatch source) {
412 PortNumber udpSourcePort = source.getUdpSourcePort();
413 if (udpSourcePort != null) {
414 Integer udpSourcePortValue = udpSourcePort.getValue();
415 if (udpSourcePortValue != null) {
416 target.setField(TP_SRC, udpSourcePortValue.shortValue());
420 PortNumber udpDestPort = source.getUdpDestinationPort();
421 if (udpDestPort != null) {
422 Integer udpDestPortValue = udpDestPort.getValue();
423 if (udpDestPortValue != null) {
424 target.setField(TP_DST, udpDestPortValue.shortValue());
428 target.setField(NW_PROTO, UDP);
431 private static void fillTransportLayer(Match target, TcpMatch source) {
432 PortNumber tcpSourcePort = source.getTcpSourcePort();
433 if (tcpSourcePort != null) {
434 Integer tcpSourcePortValue = tcpSourcePort.getValue();
435 if (tcpSourcePortValue != null) {
436 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
440 PortNumber tcpDestPort = source.getTcpDestinationPort();
441 if (tcpDestPort != null) {
442 Integer tcpDestPortValue = tcpDestPort.getValue();
443 if (tcpDestPortValue != null) {
444 target.setField(TP_DST, tcpDestPortValue.shortValue());
448 target.setField(NW_PROTO, TCP);
451 private static void fillTransportLayer(Match target, SctpMatch source) {
452 PortNumber sctpSourcePort = source.getSctpSourcePort();
453 if (sctpSourcePort != null) {
454 Integer sctpSourcePortValue = sctpSourcePort.getValue();
455 if (sctpSourcePortValue != null) {
456 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
459 PortNumber sctpDestPort = source.getSctpDestinationPort();
460 if (sctpDestPort != null) {
461 Integer sctpDestPortValue = sctpDestPort.getValue();
462 if (sctpDestPortValue != null) {
463 target.setField(TP_DST, sctpDestPortValue.shortValue());
467 target.setField(NW_PROTO, CRUDP);
471 private static void fillFrom(Match target, Layer3Match source) {
474 if (source instanceof Ipv4Match) {
475 fillFromIpv4(target, (Ipv4Match) source);
476 } else if (source instanceof Ipv6Match) {
477 fillFromIpv6(target, (Ipv6Match) source);
478 } else if (source instanceof ArpMatch) {
479 fillFromArp(target, (ArpMatch) source);
483 private static void fillFromArp(Match target, ArpMatch source) {
484 Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
485 if (sourceAddress != null) {
486 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
488 Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
489 if (destAddress != null) {
490 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
492 ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
493 if (sourceHwAddress != null) {
494 target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
496 ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
497 if (targetHwAddress != null) {
498 target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
501 target.setField(DL_TYPE, new Short(ETHERNET_ARP));
505 private static void fillFromIpv6(Match target, Ipv6Match source) {
506 Ipv6Prefix sourceAddress = source.getIpv6Source();
507 if (sourceAddress != null) {
508 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
510 Ipv6Prefix destAddress = source.getIpv6Destination();
511 if (destAddress != null) {
512 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
516 private static void fillFromIpv4(Match target, Ipv4Match source) {
517 Ipv4Prefix sourceAddress = source.getIpv4Source();
518 if (sourceAddress != null) {
519 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
521 Ipv4Prefix destAddress = source.getIpv4Destination();
522 if (destAddress != null) {
523 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
527 private static InetAddress inetAddressFrom(Ipv4Prefix source) {
528 if (source != null) {
529 String[] parts = source.getValue().split("/");
530 return InetAddresses.forString(parts[0]);
535 private static InetAddress inetAddressFrom(Ipv6Prefix source) {
536 if (source != null) {
537 String[] parts = source.getValue().split("/");
538 return InetAddresses.forString(parts[0]);
543 private static void fillFrom(Match target, EthernetMatch source) {
546 EthernetType ethType = source.getEthernetType();
547 if (ethType != null) {
548 EtherType ethInnerType = ethType.getType();
549 if (ethInnerType != null && target.getField(DL_TYPE) == null) {
550 Long value = ethInnerType.getValue();
551 target.setField(DL_TYPE, value.shortValue());
555 MacAddressFilter ethSource = source.getEthernetSource();
556 if (ethSource != null) {
557 target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
560 MacAddressFilter ethDest = source.getEthernetDestination();
561 if (ethDest != null) {
562 target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
566 private static byte[] bytesFrom(MacAddress address) {
567 String[] mac = address.getValue().split(":");
568 byte[] macAddress = new byte[6]; // mac.length == 6 bytes
569 for (int i = 0; i < mac.length; i++) {
570 macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();