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.flowprogrammer.Flow;
57 import org.opendaylight.controller.sal.match.Match;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.ControllerActionCase;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCase;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodActionCase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAllActionCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.HwPathActionCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.LoopbackActionCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCase;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCase;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsActionCase;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbActionCase;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCase;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCase;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCase;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlTypeActionCase;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetMplsTtlActionCase;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNextHopActionCase;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCase;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCase;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTosActionCase;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTtlActionCase;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetQueueActionCase;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCase;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpSrcActionCase;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanCfiActionCase;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCase;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanPcpActionCase;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SwPathActionCase;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.MacAddressFilter;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddress;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
118 import com.google.common.net.InetAddresses;
120 public class ToSalConversionsUtils {
122 private ToSalConversionsUtils() {
126 public static Flow toFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source, Node node) {
127 final Flow target = new Flow();
129 Integer hardTimeout = source.getHardTimeout();
130 if (hardTimeout != null) {
131 target.setHardTimeout(hardTimeout.shortValue());
134 Integer idleTimeout = source.getIdleTimeout();
135 if (idleTimeout != null) {
136 target.setIdleTimeout(idleTimeout.shortValue());
139 Integer priority = source.getPriority();
140 if (priority != null) {
141 target.setPriority(priority.shortValue());
144 target.setMatch(toMatch(source.getMatch()));
146 List<Action> actions = getAction(source);
147 if (actions != null) {
148 target.setActions(actionFrom(actions, node));
151 target.setId(source.getCookie().getValue().longValue());
155 public static List<Action> getAction(
156 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source) {
157 if (source.getInstructions() != null) {
158 for (Instruction instruction : source.getInstructions().getInstruction()) {
159 if (instruction.getInstruction() instanceof ApplyActionsCase) {
160 return (((ApplyActionsCase) instruction.getInstruction()).getApplyActions().getAction());
164 // TODO Auto-generated method stub
165 return Collections.emptyList();
168 public static List<org.opendaylight.controller.sal.action.Action> actionFrom(List<Action> actions, Node node) {
169 List<org.opendaylight.controller.sal.action.Action> targetAction = new ArrayList<>();
170 for (Action action : actions) {
171 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action sourceAction = action
174 if (sourceAction instanceof ControllerActionCase) {
175 targetAction.add(new Controller());
176 } else if (sourceAction instanceof OutputActionCase) {
178 Uri nodeConnector = ((OutputActionCase) sourceAction).getOutputAction().getOutputNodeConnector();
179 if (nodeConnector != null) {
180 //for (Uri uri : nodeConnectors) {
181 targetAction.add(new Output(fromNodeConnectorRef(nodeConnector, node)));
184 } else if (sourceAction instanceof PopMplsActionCase) {
185 // TODO: define maping
186 } else if (sourceAction instanceof PushMplsActionCase) {
187 // TODO: define maping
188 } else if (sourceAction instanceof PushPbbActionCase) {
189 // TODO: define maping
190 } else if (sourceAction instanceof SetMplsTtlActionCase) {
191 // TODO: define maping
192 // targetAction = //no action to map
193 } else if (sourceAction instanceof SetNwTtlActionCase) {
194 // TODO: define maping
195 } else if (sourceAction instanceof SetQueueActionCase) {
196 // TODO: define maping
197 // targetAction = //no action to map
198 } else if (sourceAction instanceof DropActionCase) {
199 targetAction.add(new Drop());
200 } else if (sourceAction instanceof FloodActionCase) {
201 targetAction.add(new Flood());
202 } else if (sourceAction instanceof FloodAllActionCase) {
203 targetAction.add(new FloodAll());
204 } else if (sourceAction instanceof HwPathActionCase) {
205 targetAction.add(new HwPath());
206 } else if (sourceAction instanceof LoopbackActionCase) {
207 targetAction.add(new Loopback());
208 } else if (sourceAction instanceof PopVlanActionCase) {
209 targetAction.add(new PopVlan());
210 } else if (sourceAction instanceof PushVlanActionCase) {
211 PushVlanActionCase pushVlanAction = (PushVlanActionCase) sourceAction;
212 PushVlan pushVlan = pushVlanFrom(pushVlanAction.getPushVlanAction());
213 if (pushVlan != null) {
214 targetAction.add(pushVlan);
216 } else if (sourceAction instanceof SetDlDstActionCase) {
217 MacAddress addressL2Dest = ((SetDlDstActionCase) sourceAction).getSetDlDstAction().getAddress();
218 if (addressL2Dest != null) {
219 targetAction.add(new SetDlDst(bytesFrom(addressL2Dest)));
221 } else if (sourceAction instanceof SetDlSrcActionCase) {
222 MacAddress addressL2Src = ((SetDlSrcActionCase) sourceAction).getSetDlSrcAction().getAddress();
223 if (addressL2Src != null) {
224 targetAction.add(new SetDlSrc(bytesFrom(addressL2Src)));
227 } else if (sourceAction instanceof SetDlTypeActionCase) {
228 EtherType dlType = ((SetDlTypeActionCase) sourceAction).getSetDlTypeAction().getDlType();
229 if (dlType != null) {
230 Long dlTypeValue = dlType.getValue();
231 if (dlTypeValue != null) {
232 targetAction.add(new SetDlType(dlTypeValue.intValue()));
235 } else if (sourceAction instanceof SetNextHopActionCase) {
236 Address addressL3 = ((SetNextHopActionCase) sourceAction).getSetNextHopAction().getAddress();
238 InetAddress inetAddress = inetAddressFrom(addressL3);
239 if (inetAddress != null) {
240 targetAction.add(new SetNextHop(inetAddress));
242 } else if (sourceAction instanceof SetNwDstActionCase) {
243 Address addressL3 = ((SetNwDstActionCase) sourceAction).getSetNwDstAction().getAddress();
245 InetAddress inetAddress = inetAddressFrom(addressL3);
246 if (inetAddress != null) {
247 targetAction.add(new SetNwDst(inetAddress));
249 } else if (sourceAction instanceof SetNwSrcActionCase) {
250 Address addressL3 = ((SetNwSrcActionCase) sourceAction).getSetNwSrcAction().getAddress();
252 InetAddress inetAddress = inetAddressFrom(addressL3);
253 if (inetAddress != null) {
254 targetAction.add(new SetNwSrc(inetAddress));
256 } else if (sourceAction instanceof SetNwTosActionCase) {
257 Integer tos = ((SetNwTosActionCase) sourceAction).getSetNwTosAction().getTos();
259 targetAction.add(new SetNwTos(tos));
261 } else if (sourceAction instanceof SetTpDstActionCase) {
262 PortNumber port = ((SetTpDstActionCase) sourceAction).getSetTpDstAction().getPort();
264 Integer portValue = port.getValue();
265 if (port.getValue() != null) {
266 targetAction.add(new SetTpDst(portValue));
269 } else if (sourceAction instanceof SetTpSrcActionCase) {
270 PortNumber port = ((SetTpSrcActionCase) sourceAction).getSetTpSrcAction().getPort();
272 Integer portValue = port.getValue();
273 if (port.getValue() != null) {
274 targetAction.add(new SetTpSrc(portValue));
277 } else if (sourceAction instanceof SetVlanCfiActionCase) {
278 VlanCfi vlanCfi = ((SetVlanCfiActionCase) sourceAction).getSetVlanCfiAction().getVlanCfi();
279 if (vlanCfi != null) {
280 Integer vlanCfiValue = vlanCfi.getValue();
281 if (vlanCfiValue != null) {
282 targetAction.add(new SetVlanCfi(vlanCfiValue));
285 } else if (sourceAction instanceof SetVlanIdActionCase) {
286 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdActionCase) sourceAction).getSetVlanIdAction()
288 if (vlanID != null) {
289 Integer vlanIdValue = vlanID.getValue();
290 if (vlanIdValue != null) {
291 targetAction.add(new SetVlanId(vlanIdValue));
294 } else if (sourceAction instanceof SetVlanPcpActionCase) {
295 VlanPcp vlanPcp = ((SetVlanPcpActionCase) sourceAction).getSetVlanPcpAction().getVlanPcp();
296 if (vlanPcp != null) {
297 Short vlanPcpValue = vlanPcp.getValue();
298 if (vlanPcpValue != null) {
299 targetAction.add(new SetVlanPcp(vlanPcpValue));
302 } else if (sourceAction instanceof SwPathActionCase) {
303 targetAction.add(new SwPath());
310 private static InetAddress inetAddressFrom(Address addressL3) {
311 if (addressL3 != null) {
312 if (addressL3 instanceof Ipv4) {
313 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
314 if (addressL3Ipv4 != null) {
315 return inetAddressFrom(addressL3Ipv4);
317 } else if (addressL3 instanceof Ipv6) {
318 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
319 if (addressL3Ipv6 != null) {
320 return inetAddressFrom(addressL3Ipv6);
327 private static PushVlan pushVlanFrom(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanAction pushVlanAction) {
333 if (pushVlanAction.getTag() != null) {
334 tag = pushVlanAction.getTag();
335 if (pushVlanAction.getPcp() != null) {
336 pcp = pushVlanAction.getPcp();
337 if (pushVlanAction.getCfi() != null && pushVlanAction.getCfi().getValue() != null) {
338 cfi = pushVlanAction.getCfi().getValue();
339 if (pushVlanAction.getVlanId() != null && pushVlanAction.getVlanId().getValue() != null) {
340 vlanId = pushVlanAction.getVlanId().getValue();
341 return new PushVlan(tag, pcp, cfi, vlanId);
349 private static NodeConnector fromNodeConnectorRef(Uri uri, Node node) {
350 NodeConnector nodeConnector = null;
352 nodeConnector = new NodeConnector(NodeMapping.MD_SAL_TYPE,node.getNodeIDString()+":"+uri.getValue(),node);
353 } catch (ConstructionException e) {
356 return nodeConnector;
359 public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match source) {
360 Match target = new Match();
361 if (source != null) {
362 fillFrom(target, source.getVlanMatch());
363 fillFrom(target, source.getEthernetMatch());
364 fillFrom(target, source.getLayer3Match());
365 fillFrom(target, source.getLayer4Match());
366 fillFrom(target, source.getIpMatch());
372 private static void fillFrom(Match target, VlanMatch vlanMatch) {
373 if (vlanMatch != null) {
374 VlanId vlanId = vlanMatch.getVlanId();
375 if (vlanId != null) {
376 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
378 if (vlanIdInner != null) {
379 Integer vlanValue = vlanIdInner.getValue();
380 if (vlanValue != null) {
381 target.setField(DL_VLAN, vlanValue.shortValue());
385 VlanPcp vlanPcp = vlanMatch.getVlanPcp();
386 if (vlanPcp != null) {
387 Short vlanPcpValue = vlanPcp.getValue();
388 if (vlanPcpValue != null) {
389 target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
395 private static void fillFrom(Match target, IpMatch ipMatch) {
396 if (ipMatch != null) {
397 Short ipProtocol = ipMatch.getIpProtocol();
399 if (ipProtocol != null && target.getField(NW_PROTO) == null) {
400 target.setField(NW_PROTO, ipProtocol.byteValue());
402 Dscp dscp = ipMatch.getIpDscp();
404 Short dscpValue = dscp.getValue();
405 if (dscpValue != null) {
406 target.setField(NW_TOS, dscpValue.byteValue());
412 private static void fillFrom(Match target, Layer4Match layer4Match) {
413 if (layer4Match == null) {
416 if (layer4Match instanceof SctpMatch) {
417 fillTransportLayer(target, (SctpMatch) layer4Match);
418 } else if (layer4Match instanceof TcpMatch) {
419 fillTransportLayer(target, (TcpMatch) layer4Match);
420 } else if (layer4Match instanceof UdpMatch) {
421 fillTransportLayer(target, (UdpMatch) layer4Match);
425 private static void fillTransportLayer(Match target, UdpMatch source) {
426 PortNumber udpSourcePort = source.getUdpSourcePort();
427 if (udpSourcePort != null) {
428 Integer udpSourcePortValue = udpSourcePort.getValue();
429 if (udpSourcePortValue != null) {
430 target.setField(TP_SRC, udpSourcePortValue.shortValue());
434 PortNumber udpDestPort = source.getUdpDestinationPort();
435 if (udpDestPort != null) {
436 Integer udpDestPortValue = udpDestPort.getValue();
437 if (udpDestPortValue != null) {
438 target.setField(TP_DST, udpDestPortValue.shortValue());
442 target.setField(NW_PROTO, UDP);
445 private static void fillTransportLayer(Match target, TcpMatch source) {
446 PortNumber tcpSourcePort = source.getTcpSourcePort();
447 if (tcpSourcePort != null) {
448 Integer tcpSourcePortValue = tcpSourcePort.getValue();
449 if (tcpSourcePortValue != null) {
450 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
454 PortNumber tcpDestPort = source.getTcpDestinationPort();
455 if (tcpDestPort != null) {
456 Integer tcpDestPortValue = tcpDestPort.getValue();
457 if (tcpDestPortValue != null) {
458 target.setField(TP_DST, tcpDestPortValue.shortValue());
462 target.setField(NW_PROTO, TCP);
465 private static void fillTransportLayer(Match target, SctpMatch source) {
466 PortNumber sctpSourcePort = source.getSctpSourcePort();
467 if (sctpSourcePort != null) {
468 Integer sctpSourcePortValue = sctpSourcePort.getValue();
469 if (sctpSourcePortValue != null) {
470 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
473 PortNumber sctpDestPort = source.getSctpDestinationPort();
474 if (sctpDestPort != null) {
475 Integer sctpDestPortValue = sctpDestPort.getValue();
476 if (sctpDestPortValue != null) {
477 target.setField(TP_DST, sctpDestPortValue.shortValue());
481 target.setField(NW_PROTO, CRUDP);
485 private static void fillFrom(Match target, Layer3Match source) {
488 if (source instanceof Ipv4Match) {
489 fillFromIpv4(target, (Ipv4Match) source);
490 } else if (source instanceof Ipv6Match) {
491 fillFromIpv6(target, (Ipv6Match) source);
492 } else if (source instanceof ArpMatch) {
493 fillFromArp(target, (ArpMatch) source);
497 private static void fillFromArp(Match target, ArpMatch source) {
498 Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
499 if (sourceAddress != null) {
500 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
502 Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
503 if (destAddress != null) {
504 target.setField(NW_DST, inetAddressFrom(destAddress), null);
506 ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
507 if (sourceHwAddress != null) {
508 target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
510 ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
511 if (targetHwAddress != null) {
512 target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
515 target.setField(DL_TYPE, new Short(ETHERNET_ARP));
519 private static void fillFromIpv6(Match target, Ipv6Match source) {
520 Ipv6Prefix sourceAddress = source.getIpv6Source();
521 if (sourceAddress != null) {
522 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
524 Ipv6Prefix destAddress = source.getIpv6Destination();
525 if (destAddress != null) {
526 target.setField(NW_DST, inetAddressFrom(destAddress), null);
530 private static void fillFromIpv4(Match target, Ipv4Match source) {
531 Ipv4Prefix sourceAddress = source.getIpv4Source();
532 if (sourceAddress != null) {
533 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
535 Ipv4Prefix destAddress = source.getIpv4Destination();
536 if (destAddress != null) {
537 target.setField(NW_DST, inetAddressFrom(destAddress), null);
541 private static InetAddress inetAddressFrom(Ipv4Prefix source) {
542 if (source != null) {
543 String[] parts = source.getValue().split("/");
544 return InetAddresses.forString(parts[0]);
549 private static InetAddress inetAddressFrom(Ipv6Prefix source) {
550 if (source != null) {
551 String[] parts = source.getValue().split("/");
552 return InetAddresses.forString(parts[0]);
557 private static void fillFrom(Match target, EthernetMatch source) {
560 EthernetType ethType = source.getEthernetType();
561 if (ethType != null) {
562 EtherType ethInnerType = ethType.getType();
563 if (ethInnerType != null && target.getField(DL_TYPE) == null) {
564 Long value = ethInnerType.getValue();
565 target.setField(DL_TYPE, value.shortValue());
569 MacAddressFilter ethSource = source.getEthernetSource();
570 if (ethSource != null) {
571 target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
574 MacAddressFilter ethDest = source.getEthernetDestination();
575 if (ethDest != null) {
576 target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
580 public static byte[] bytesFrom(MacAddress address) {
581 String[] mac = address.getValue().split(":");
582 byte[] macAddress = new byte[6]; // mac.length == 6 bytes
583 for (int i = 0; i < mac.length; i++) {
584 macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
589 public static byte[] bytesFromDpid(long dpid) {
590 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
592 for (short i = 0; i < 6; i++) {
593 mac[5 - i] = (byte) dpid;