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.Node.NodeIDType;
56 import org.opendaylight.controller.sal.core.NodeConnector;
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() {
132 throw new IllegalAccessError("forcing no instance for factory");
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 // TODO: We should really have a bi-directional map from AD-SAL node types to
212 // MD-SAL node types, but lets fix that later.
213 String type = node.getType();
214 if( type.equals(NodeIDType.OPENFLOW) ){
215 type = NodeMapping.OPENFLOW_ID_PREFIX;
219 Uri fullNodeConnector = new Uri(type+node.getID()+":"+nodeConnector.getValue());
220 targetAction.add(new Output(fromNodeConnectorRef(fullNodeConnector, node)));
222 } else if (sourceAction instanceof PopMplsActionCase) {
223 // TODO: define maping
224 } else if (sourceAction instanceof PushMplsActionCase) {
225 // TODO: define maping
226 } else if (sourceAction instanceof PushPbbActionCase) {
227 // TODO: define maping
228 } else if (sourceAction instanceof SetMplsTtlActionCase) {
229 // TODO: define maping
230 // targetAction = //no action to map
231 } else if (sourceAction instanceof SetNwTtlActionCase) {
232 // TODO: define maping
233 } else if (sourceAction instanceof SetQueueActionCase) {
234 // TODO: define maping
235 // targetAction = //no action to map
236 } else if (sourceAction instanceof DropActionCase) {
237 targetAction.add(new Drop());
238 } else if (sourceAction instanceof FloodActionCase) {
239 targetAction.add(new Flood());
240 } else if (sourceAction instanceof FloodAllActionCase) {
241 targetAction.add(new FloodAll());
242 } else if (sourceAction instanceof HwPathActionCase) {
243 targetAction.add(new HwPath());
244 } else if (sourceAction instanceof LoopbackActionCase) {
245 targetAction.add(new Loopback());
246 } else if (sourceAction instanceof PopVlanActionCase) {
247 targetAction.add(new PopVlan());
248 } else if (sourceAction instanceof PushVlanActionCase) {
249 PushVlanActionCase pushVlanAction = (PushVlanActionCase) sourceAction;
250 PushVlan pushVlan = pushVlanFrom(pushVlanAction.getPushVlanAction());
251 if (pushVlan != null) {
252 targetAction.add(pushVlan);
254 } else if (sourceAction instanceof SetDlDstActionCase) {
255 MacAddress addressL2Dest = ((SetDlDstActionCase) sourceAction).getSetDlDstAction().getAddress();
256 if (addressL2Dest != null) {
257 targetAction.add(new SetDlDst(bytesFrom(addressL2Dest)));
259 } else if (sourceAction instanceof SetDlSrcActionCase) {
260 MacAddress addressL2Src = ((SetDlSrcActionCase) sourceAction).getSetDlSrcAction().getAddress();
261 if (addressL2Src != null) {
262 targetAction.add(new SetDlSrc(bytesFrom(addressL2Src)));
265 } else if (sourceAction instanceof SetDlTypeActionCase) {
266 EtherType dlType = ((SetDlTypeActionCase) sourceAction).getSetDlTypeAction().getDlType();
267 if (dlType != null) {
268 Long dlTypeValue = dlType.getValue();
269 if (dlTypeValue != null) {
270 targetAction.add(new SetDlType(dlTypeValue.intValue()));
273 } else if (sourceAction instanceof SetNextHopActionCase) {
274 Address addressL3 = ((SetNextHopActionCase) sourceAction).getSetNextHopAction().getAddress();
276 InetAddress inetAddress = inetAddressFrom(addressL3);
277 if (inetAddress != null) {
278 targetAction.add(new SetNextHop(inetAddress));
280 } else if (sourceAction instanceof SetNwDstActionCase) {
281 Address addressL3 = ((SetNwDstActionCase) sourceAction).getSetNwDstAction().getAddress();
283 InetAddress inetAddress = inetAddressFrom(addressL3);
284 if (inetAddress != null) {
285 targetAction.add(new SetNwDst(inetAddress));
287 } else if (sourceAction instanceof SetNwSrcActionCase) {
288 Address addressL3 = ((SetNwSrcActionCase) sourceAction).getSetNwSrcAction().getAddress();
290 InetAddress inetAddress = inetAddressFrom(addressL3);
291 if (inetAddress != null) {
292 targetAction.add(new SetNwSrc(inetAddress));
294 } else if (sourceAction instanceof SetNwTosActionCase) {
295 Integer tos = ((SetNwTosActionCase) sourceAction).getSetNwTosAction().getTos();
297 targetAction.add(new SetNwTos(ToSalConversionsUtils.tosToNwDscp(tos)));
299 } else if (sourceAction instanceof SetTpDstActionCase) {
300 PortNumber port = ((SetTpDstActionCase) sourceAction).getSetTpDstAction().getPort();
302 Integer portValue = port.getValue();
303 if (port.getValue() != null) {
304 targetAction.add(new SetTpDst(portValue));
307 } else if (sourceAction instanceof SetTpSrcActionCase) {
308 PortNumber port = ((SetTpSrcActionCase) sourceAction).getSetTpSrcAction().getPort();
310 Integer portValue = port.getValue();
311 if (port.getValue() != null) {
312 targetAction.add(new SetTpSrc(portValue));
315 } else if (sourceAction instanceof SetVlanCfiActionCase) {
316 VlanCfi vlanCfi = ((SetVlanCfiActionCase) sourceAction).getSetVlanCfiAction().getVlanCfi();
317 if (vlanCfi != null) {
318 Integer vlanCfiValue = vlanCfi.getValue();
319 if (vlanCfiValue != null) {
320 targetAction.add(new SetVlanCfi(vlanCfiValue));
323 } else if (sourceAction instanceof SetVlanIdActionCase) {
324 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdActionCase) sourceAction).getSetVlanIdAction()
326 if (vlanID != null) {
327 Integer vlanIdValue = vlanID.getValue();
328 if (vlanIdValue != null) {
329 targetAction.add(new SetVlanId(vlanIdValue));
332 } else if (sourceAction instanceof SetVlanPcpActionCase) {
333 VlanPcp vlanPcp = ((SetVlanPcpActionCase) sourceAction).getSetVlanPcpAction().getVlanPcp();
334 if (vlanPcp != null) {
335 Short vlanPcpValue = vlanPcp.getValue();
336 if (vlanPcpValue != null) {
337 targetAction.add(new SetVlanPcp(vlanPcpValue));
340 } else if (sourceAction instanceof SwPathActionCase) {
341 targetAction.add(new SwPath());
348 private static InetAddress inetAddressFrom(Address addressL3) {
349 if (addressL3 != null) {
350 if (addressL3 instanceof Ipv4) {
351 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
352 if (addressL3Ipv4 != null) {
353 return inetAddressFrom(addressL3Ipv4);
355 } else if (addressL3 instanceof Ipv6) {
356 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
357 if (addressL3Ipv6 != null) {
358 return inetAddressFrom(addressL3Ipv6);
365 private static PushVlan pushVlanFrom(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanAction pushVlanAction) {
366 Integer tag = pushVlanAction.getTag();
368 return new PushVlan(tag.intValue());
374 * @param openflow nodeConnector uri
376 * @return assembled nodeConnector
378 public static NodeConnector fromNodeConnectorRef(Uri uri, Node node) {
379 NodeConnector nodeConnector = null;
381 NodeConnectorId nodeConnectorId = new NodeConnectorId(uri.getValue());
382 nodeConnector = NodeMapping.toADNodeConnector(nodeConnectorId, node);
383 } catch (ConstructionException e) {
384 LOG.warn("nodeConnector creation failed at node: {} with nodeConnectorUri: {}",
385 node, uri.getValue());
387 return nodeConnector;
390 public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match source) {
391 Match target = new Match();
392 if (source != null) {
393 fillFrom(target, source.getVlanMatch());
394 fillFrom(target, source.getEthernetMatch());
395 fillFrom(target, source.getLayer3Match());
396 fillFrom(target, source.getLayer4Match());
397 fillFrom(target, source.getIpMatch());
398 fillFrom(target, source.getInPort());
408 private static void fillFrom(Match target, NodeConnectorId inPort) {
409 if (inPort != null) {
410 String inPortValue = inPort.getValue();
411 if (inPortValue != null) {
413 target.setField(MatchType.IN_PORT, NodeMapping.toADNodeConnector(inPort,
414 NodeMapping.toAdNodeId(inPort)));
415 } catch (ConstructionException e) {
416 LOG.warn("nodeConnector construction failed", e);
422 private static void fillFrom(Match target, VlanMatch vlanMatch) {
423 if (vlanMatch != null) {
424 VlanId vlanId = vlanMatch.getVlanId();
425 if (vlanId != null) {
426 if (Boolean.TRUE.equals(vlanId.isVlanIdPresent())) {
427 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
429 if (vlanIdInner != null) {
430 Integer vlanValue = vlanIdInner.getValue();
431 if (vlanValue != null) {
432 target.setField(DL_VLAN, vlanValue.shortValue());
436 target.setField(DL_VLAN, MatchType.DL_VLAN_NONE);
439 VlanPcp vlanPcp = vlanMatch.getVlanPcp();
440 if (vlanPcp != null) {
441 Short vlanPcpValue = vlanPcp.getValue();
442 if (vlanPcpValue != null) {
443 target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
449 private static void fillFrom(Match target, IpMatch ipMatch) {
450 if (ipMatch != null) {
451 Short ipProtocol = ipMatch.getIpProtocol();
453 if (ipProtocol != null && target.getField(NW_PROTO) == null) {
454 target.setField(NW_PROTO, ipProtocol.byteValue());
456 Dscp dscp = ipMatch.getIpDscp();
458 Short dscpValue = dscp.getValue();
459 if (dscpValue != null) {
460 target.setField(NW_TOS, dscpValue.byteValue());
466 private static void fillFrom(Match target, Layer4Match layer4Match) {
467 if (layer4Match == null) {
470 if (layer4Match instanceof SctpMatch) {
471 fillTransportLayer(target, (SctpMatch) layer4Match);
472 } else if (layer4Match instanceof TcpMatch) {
473 fillTransportLayer(target, (TcpMatch) layer4Match);
474 } else if (layer4Match instanceof UdpMatch) {
475 fillTransportLayer(target, (UdpMatch) layer4Match);
479 private static void fillTransportLayer(Match target, UdpMatch source) {
480 PortNumber udpSourcePort = source.getUdpSourcePort();
481 if (udpSourcePort != null) {
482 Integer udpSourcePortValue = udpSourcePort.getValue();
483 if (udpSourcePortValue != null) {
484 target.setField(TP_SRC, udpSourcePortValue.shortValue());
488 PortNumber udpDestPort = source.getUdpDestinationPort();
489 if (udpDestPort != null) {
490 Integer udpDestPortValue = udpDestPort.getValue();
491 if (udpDestPortValue != null) {
492 target.setField(TP_DST, udpDestPortValue.shortValue());
496 target.setField(NW_PROTO, UDP);
499 private static void fillTransportLayer(Match target, TcpMatch source) {
500 PortNumber tcpSourcePort = source.getTcpSourcePort();
501 if (tcpSourcePort != null) {
502 Integer tcpSourcePortValue = tcpSourcePort.getValue();
503 if (tcpSourcePortValue != null) {
504 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
508 PortNumber tcpDestPort = source.getTcpDestinationPort();
509 if (tcpDestPort != null) {
510 Integer tcpDestPortValue = tcpDestPort.getValue();
511 if (tcpDestPortValue != null) {
512 target.setField(TP_DST, tcpDestPortValue.shortValue());
516 target.setField(NW_PROTO, TCP);
519 private static void fillTransportLayer(Match target, SctpMatch source) {
520 PortNumber sctpSourcePort = source.getSctpSourcePort();
521 if (sctpSourcePort != null) {
522 Integer sctpSourcePortValue = sctpSourcePort.getValue();
523 if (sctpSourcePortValue != null) {
524 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
527 PortNumber sctpDestPort = source.getSctpDestinationPort();
528 if (sctpDestPort != null) {
529 Integer sctpDestPortValue = sctpDestPort.getValue();
530 if (sctpDestPortValue != null) {
531 target.setField(TP_DST, sctpDestPortValue.shortValue());
535 target.setField(NW_PROTO, CRUDP);
539 private static void fillFrom(Match target, Layer3Match source) {
542 if (source instanceof Ipv4Match) {
543 fillFromIpv4(target, (Ipv4Match) source);
544 } else if (source instanceof Ipv6Match) {
545 fillFromIpv6(target, (Ipv6Match) source);
546 } else if (source instanceof ArpMatch) {
547 fillFromArp(target, (ArpMatch) source);
551 private static void fillFromArp(Match target, ArpMatch source) {
552 Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
553 if (sourceAddress != null) {
554 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
556 Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
557 if (destAddress != null) {
558 target.setField(NW_DST, inetAddressFrom(destAddress), null);
560 ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
561 if (sourceHwAddress != null) {
562 target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
564 ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
565 if (targetHwAddress != null) {
566 target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
569 target.setField(DL_TYPE, new Short(ETHERNET_ARP));
573 private static void fillFromIpv6(Match target, Ipv6Match source) {
574 Ipv6Prefix sourceAddress = source.getIpv6Source();
575 if (sourceAddress != null) {
576 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
578 Ipv6Prefix destAddress = source.getIpv6Destination();
579 if (destAddress != null) {
580 target.setField(NW_DST, inetAddressFrom(destAddress), null);
584 private static void fillFromIpv4(Match target, Ipv4Match source) {
585 Ipv4Prefix sourceAddress = source.getIpv4Source();
586 if (sourceAddress != null) {
587 target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
589 Ipv4Prefix destAddress = source.getIpv4Destination();
590 if (destAddress != null) {
591 target.setField(NW_DST, inetAddressFrom(destAddress), null);
595 private static InetAddress inetAddressFrom(Ipv4Prefix source) {
596 if (source != null) {
597 String[] parts = source.getValue().split("/");
598 return InetAddresses.forString(parts[0]);
603 private static InetAddress inetAddressFrom(Ipv6Prefix source) {
604 if (source != null) {
605 String[] parts = source.getValue().split("/");
606 return InetAddresses.forString(parts[0]);
611 private static void fillFrom(Match target, EthernetMatch source) {
614 EthernetType ethType = source.getEthernetType();
615 if (ethType != null) {
616 EtherType ethInnerType = ethType.getType();
617 if (ethInnerType != null && target.getField(DL_TYPE) == null) {
618 Long value = ethInnerType.getValue();
619 target.setField(DL_TYPE, value.shortValue());
623 MacAddressFilter ethSource = source.getEthernetSource();
624 if (ethSource != null) {
625 target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
628 MacAddressFilter ethDest = source.getEthernetDestination();
629 if (ethDest != null) {
630 target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
634 public static byte[] bytesFrom(MacAddress address) {
635 String[] mac = address.getValue().split(":");
636 byte[] macAddress = new byte[6]; // mac.length == 6 bytes
637 for (int i = 0; i < mac.length; i++) {
638 macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
643 public static byte[] bytesFromDpid(long dpid) {
644 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
646 for (short i = 0; i < 6; i++) {
647 mac[5 - i] = (byte) dpid;
655 * @param nwTos NW-TOS
656 * @return shifted to NW-DSCP
658 public static int tosToNwDscp(int nwTos) {
659 return (short) (nwTos >>> FromSalConversionsUtils.ENC_FIELD_BIT_SIZE);