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.controller.sal.match.MatchType;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.ControllerActionCase;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodActionCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAllActionCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.HwPathActionCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.LoopbackActionCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCase;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCase;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsActionCase;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbActionCase;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCase;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCase;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCase;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlTypeActionCase;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetMplsTtlActionCase;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNextHopActionCase;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCase;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCase;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTosActionCase;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTtlActionCase;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetQueueActionCase;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCase;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpSrcActionCase;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanCfiActionCase;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCase;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanPcpActionCase;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SwPathActionCase;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.GenericFlowAttributes;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.MacAddressFilter;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddress;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
121 import org.slf4j.Logger;
122 import org.slf4j.LoggerFactory;
124 import com.google.common.net.InetAddresses;
126 public class ToSalConversionsUtils {
128 private static final Logger LOG = LoggerFactory.getLogger(ToSalConversionsUtils.class);
130 private ToSalConversionsUtils() {
131 throw new IllegalAccessError("forcing no instance for factory");
134 public static Flow toFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source, Node node) {
135 final Flow target = new Flow();
136 genericFlowToAdFlow(source, target);
138 target.setMatch(toMatch(source.getMatch()));
140 List<Action> actions = getAction(source);
141 if (actions != null) {
142 target.setActions(actionFrom(actions, node));
149 * @param source notification, missing instructions
150 * @param node corresponding node where the flow change occured
151 * @return ad-sal node, build from given data
153 public static Flow toFlow(SwitchFlowRemoved source, Node node) {
154 final Flow target = new Flow();
155 genericFlowToAdFlow(source, target);
157 target.setMatch(toMatch(source.getMatch()));
166 private static void genericFlowToAdFlow(GenericFlowAttributes source,
168 Integer hardTimeout = source.getHardTimeout();
169 if (hardTimeout != null) {
170 target.setHardTimeout(hardTimeout.shortValue());
173 Integer idleTimeout = source.getIdleTimeout();
174 if (idleTimeout != null) {
175 target.setIdleTimeout(idleTimeout.shortValue());
178 Integer priority = source.getPriority();
179 if (priority != null) {
180 target.setPriority(priority.shortValue());
182 target.setId(source.getCookie().getValue().longValue());
185 public static List<Action> getAction(
186 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source) {
187 if (source.getInstructions() != null) {
188 for (Instruction instruction : source.getInstructions().getInstruction()) {
189 if (instruction.getInstruction() instanceof ApplyActionsCase) {
190 return (((ApplyActionsCase) instruction.getInstruction()).getApplyActions().getAction());
194 // TODO Auto-generated method stub
195 return Collections.emptyList();
198 public static List<org.opendaylight.controller.sal.action.Action> actionFrom(List<Action> actions, Node node) {
199 List<org.opendaylight.controller.sal.action.Action> targetAction = new ArrayList<>();
200 for (Action action : actions) {
201 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action sourceAction = action
204 if (sourceAction instanceof ControllerActionCase) {
205 targetAction.add(new Controller());
206 } else if (sourceAction instanceof OutputActionCase) {
208 Uri nodeConnector = ((OutputActionCase) sourceAction).getOutputAction().getOutputNodeConnector();
209 if (nodeConnector != null) {
210 //for (Uri uri : nodeConnectors) {
211 Uri fullNodeConnector = new Uri(node.getType()+":"+node.getID()+":"+nodeConnector.getValue());
212 targetAction.add(new Output(fromNodeConnectorRef(fullNodeConnector, 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(ToSalConversionsUtils.tosToNwDscp(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) {
359 Integer tag = pushVlanAction.getTag();
361 return new PushVlan(tag.intValue());
367 * @param openflow nodeConnector uri
369 * @return assembled nodeConnector
371 public static NodeConnector fromNodeConnectorRef(Uri uri, Node node) {
372 NodeConnector nodeConnector = null;
374 NodeConnectorId nodeConnectorId = new NodeConnectorId(uri.getValue());
375 nodeConnector = NodeMapping.toADNodeConnector(nodeConnectorId, node);
376 } catch (ConstructionException e) {
377 LOG.warn("nodeConnector creation failed at node: {} with nodeConnectorUri: {}",
378 node, uri.getValue());
380 return nodeConnector;
383 public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match source) {
384 Match target = new Match();
385 if (source != null) {
386 fillFrom(target, source.getVlanMatch());
387 fillFrom(target, source.getEthernetMatch());
388 fillFrom(target, source.getLayer3Match());
389 fillFrom(target, source.getLayer4Match());
390 fillFrom(target, source.getIpMatch());
391 fillFrom(target, source.getInPort());
401 private static void fillFrom(Match target, NodeConnectorId inPort) {
402 if (inPort != null) {
403 String inPortValue = inPort.getValue();
404 if (inPortValue != null) {
406 target.setField(MatchType.IN_PORT, NodeMapping.toADNodeConnector(inPort,
407 NodeMapping.toAdNodeId(inPort)));
408 } catch (ConstructionException e) {
409 LOG.warn("nodeConnector construction failed", e);
415 private static void fillFrom(Match target, VlanMatch vlanMatch) {
416 if (vlanMatch != null) {
417 VlanId vlanId = vlanMatch.getVlanId();
418 if (vlanId != null) {
419 if (Boolean.TRUE.equals(vlanId.isVlanIdPresent())) {
420 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
422 if (vlanIdInner != null) {
423 Integer vlanValue = vlanIdInner.getValue();
424 if (vlanValue != null) {
425 target.setField(DL_VLAN, vlanValue.shortValue());
429 target.setField(DL_VLAN, MatchType.DL_VLAN_NONE);
432 VlanPcp vlanPcp = vlanMatch.getVlanPcp();
433 if (vlanPcp != null) {
434 Short vlanPcpValue = vlanPcp.getValue();
435 if (vlanPcpValue != null) {
436 target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
442 private static void fillFrom(Match target, IpMatch ipMatch) {
443 if (ipMatch != null) {
444 Short ipProtocol = ipMatch.getIpProtocol();
446 if (ipProtocol != null && target.getField(NW_PROTO) == null) {
447 target.setField(NW_PROTO, ipProtocol.byteValue());
449 Dscp dscp = ipMatch.getIpDscp();
451 Short dscpValue = dscp.getValue();
452 if (dscpValue != null) {
453 target.setField(NW_TOS, dscpValue.byteValue());
459 private static void fillFrom(Match target, Layer4Match layer4Match) {
460 if (layer4Match == null) {
463 if (layer4Match instanceof SctpMatch) {
464 fillTransportLayer(target, (SctpMatch) layer4Match);
465 } else if (layer4Match instanceof TcpMatch) {
466 fillTransportLayer(target, (TcpMatch) layer4Match);
467 } else if (layer4Match instanceof UdpMatch) {
468 fillTransportLayer(target, (UdpMatch) layer4Match);
472 private static void fillTransportLayer(Match target, UdpMatch source) {
473 PortNumber udpSourcePort = source.getUdpSourcePort();
474 if (udpSourcePort != null) {
475 Integer udpSourcePortValue = udpSourcePort.getValue();
476 if (udpSourcePortValue != null) {
477 target.setField(TP_SRC, udpSourcePortValue.shortValue());
481 PortNumber udpDestPort = source.getUdpDestinationPort();
482 if (udpDestPort != null) {
483 Integer udpDestPortValue = udpDestPort.getValue();
484 if (udpDestPortValue != null) {
485 target.setField(TP_DST, udpDestPortValue.shortValue());
489 target.setField(NW_PROTO, UDP);
492 private static void fillTransportLayer(Match target, TcpMatch source) {
493 PortNumber tcpSourcePort = source.getTcpSourcePort();
494 if (tcpSourcePort != null) {
495 Integer tcpSourcePortValue = tcpSourcePort.getValue();
496 if (tcpSourcePortValue != null) {
497 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
501 PortNumber tcpDestPort = source.getTcpDestinationPort();
502 if (tcpDestPort != null) {
503 Integer tcpDestPortValue = tcpDestPort.getValue();
504 if (tcpDestPortValue != null) {
505 target.setField(TP_DST, tcpDestPortValue.shortValue());
509 target.setField(NW_PROTO, TCP);
512 private static void fillTransportLayer(Match target, SctpMatch source) {
513 PortNumber sctpSourcePort = source.getSctpSourcePort();
514 if (sctpSourcePort != null) {
515 Integer sctpSourcePortValue = sctpSourcePort.getValue();
516 if (sctpSourcePortValue != null) {
517 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
520 PortNumber sctpDestPort = source.getSctpDestinationPort();
521 if (sctpDestPort != null) {
522 Integer sctpDestPortValue = sctpDestPort.getValue();
523 if (sctpDestPortValue != null) {
524 target.setField(TP_DST, sctpDestPortValue.shortValue());
528 target.setField(NW_PROTO, CRUDP);
532 private static void fillFrom(Match target, Layer3Match source) {
535 if (source instanceof Ipv4Match) {
536 fillFromIpv4(target, (Ipv4Match) source);
537 } else if (source instanceof Ipv6Match) {
538 fillFromIpv6(target, (Ipv6Match) source);
539 } else if (source instanceof ArpMatch) {
540 fillFromArp(target, (ArpMatch) source);
544 private static void fillFromArp(Match target, ArpMatch source) {
545 Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
546 if (sourceAddress != null) {
547 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
549 Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
550 if (destAddress != null) {
551 target.setField(NW_DST, inetAddressFrom(destAddress), null);
553 ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
554 if (sourceHwAddress != null) {
555 target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
557 ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
558 if (targetHwAddress != null) {
559 target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
562 target.setField(DL_TYPE, new Short(ETHERNET_ARP));
566 private static void fillFromIpv6(Match target, Ipv6Match source) {
567 Ipv6Prefix sourceAddress = source.getIpv6Source();
568 if (sourceAddress != null) {
569 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
571 Ipv6Prefix destAddress = source.getIpv6Destination();
572 if (destAddress != null) {
573 target.setField(NW_DST, inetAddressFrom(destAddress), null);
577 private static void fillFromIpv4(Match target, Ipv4Match source) {
578 Ipv4Prefix sourceAddress = source.getIpv4Source();
579 if (sourceAddress != null) {
580 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
582 Ipv4Prefix destAddress = source.getIpv4Destination();
583 if (destAddress != null) {
584 target.setField(NW_DST, inetAddressFrom(destAddress), null);
588 private static InetAddress inetAddressFrom(Ipv4Prefix source) {
589 if (source != null) {
590 String[] parts = source.getValue().split("/");
591 return InetAddresses.forString(parts[0]);
596 private static InetAddress inetAddressFrom(Ipv6Prefix source) {
597 if (source != null) {
598 String[] parts = source.getValue().split("/");
599 return InetAddresses.forString(parts[0]);
604 private static void fillFrom(Match target, EthernetMatch source) {
607 EthernetType ethType = source.getEthernetType();
608 if (ethType != null) {
609 EtherType ethInnerType = ethType.getType();
610 if (ethInnerType != null && target.getField(DL_TYPE) == null) {
611 Long value = ethInnerType.getValue();
612 target.setField(DL_TYPE, value.shortValue());
616 MacAddressFilter ethSource = source.getEthernetSource();
617 if (ethSource != null) {
618 target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
621 MacAddressFilter ethDest = source.getEthernetDestination();
622 if (ethDest != null) {
623 target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
627 public static byte[] bytesFrom(MacAddress address) {
628 String[] mac = address.getValue().split(":");
629 byte[] macAddress = new byte[6]; // mac.length == 6 bytes
630 for (int i = 0; i < mac.length; i++) {
631 macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
636 public static byte[] bytesFromDpid(long dpid) {
637 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
639 for (short i = 0; i < 6; i++) {
640 mac[5 - i] = (byte) dpid;
648 * @param nwTos NW-TOS
649 * @return shifted to NW-DSCP
651 public static int tosToNwDscp(int nwTos) {
652 return (short) (nwTos >>> FromSalConversionsUtils.ENC_FIELD_BIT_SIZE);