Merge "Add message to update the schema context of the InMemoryDOMDataStore"
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / ToSalConversionsUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.compatibility;
9
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;
25
26 import java.net.InetAddress;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30
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;
123
124 import com.google.common.net.InetAddresses;
125
126 public class ToSalConversionsUtils {
127
128     private static final Logger LOG = LoggerFactory.getLogger(ToSalConversionsUtils.class);
129
130     private ToSalConversionsUtils() {
131
132     }
133
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);
137
138         target.setMatch(toMatch(source.getMatch()));
139
140         List<Action> actions = getAction(source);
141         if (actions != null) {
142             target.setActions(actionFrom(actions, node));
143         }
144
145         return target;
146     }
147
148     /**
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
152      */
153     public static Flow toFlow(SwitchFlowRemoved source, Node node) {
154         final Flow target = new Flow();
155         genericFlowToAdFlow(source, target);
156
157         target.setMatch(toMatch(source.getMatch()));
158
159         return target;
160     }
161
162     /**
163      * @param source
164      * @param target
165      */
166     private static void genericFlowToAdFlow(GenericFlowAttributes source,
167             final Flow target) {
168         Integer hardTimeout = source.getHardTimeout();
169         if (hardTimeout != null) {
170             target.setHardTimeout(hardTimeout.shortValue());
171         }
172
173         Integer idleTimeout = source.getIdleTimeout();
174         if (idleTimeout != null) {
175             target.setIdleTimeout(idleTimeout.shortValue());
176         }
177
178         Integer priority = source.getPriority();
179         if (priority != null) {
180             target.setPriority(priority.shortValue());
181         }
182         target.setId(source.getCookie().getValue().longValue());
183     }
184
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());
191                 }
192             }
193         }
194         // TODO Auto-generated method stub
195         return Collections.emptyList();
196     }
197
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
202                     .getAction();
203
204             if (sourceAction instanceof ControllerActionCase) {
205                 targetAction.add(new Controller());
206             } else if (sourceAction instanceof OutputActionCase) {
207
208                 Uri nodeConnector = ((OutputActionCase) sourceAction).getOutputAction().getOutputNodeConnector();
209                 if (nodeConnector != null) {
210                     //for (Uri uri : nodeConnectors) {
211                         targetAction.add(new Output(fromNodeConnectorRef(nodeConnector, node)));
212                     //}
213                 }
214             } else if (sourceAction instanceof PopMplsActionCase) {
215                 // TODO: define maping
216             } else if (sourceAction instanceof PushMplsActionCase) {
217                 // TODO: define maping
218             } else if (sourceAction instanceof PushPbbActionCase) {
219                 // TODO: define maping
220             } else if (sourceAction instanceof SetMplsTtlActionCase) {
221                 // TODO: define maping
222                 // targetAction = //no action to map
223             } else if (sourceAction instanceof SetNwTtlActionCase) {
224                 // TODO: define maping
225             } else if (sourceAction instanceof SetQueueActionCase) {
226                 // TODO: define maping
227                 // targetAction = //no action to map
228             } else if (sourceAction instanceof DropActionCase) {
229                 targetAction.add(new Drop());
230             } else if (sourceAction instanceof FloodActionCase) {
231                 targetAction.add(new Flood());
232             } else if (sourceAction instanceof FloodAllActionCase) {
233                 targetAction.add(new FloodAll());
234             } else if (sourceAction instanceof HwPathActionCase) {
235                 targetAction.add(new HwPath());
236             } else if (sourceAction instanceof LoopbackActionCase) {
237                 targetAction.add(new Loopback());
238             } else if (sourceAction instanceof PopVlanActionCase) {
239                 targetAction.add(new PopVlan());
240             } else if (sourceAction instanceof PushVlanActionCase) {
241                 PushVlanActionCase pushVlanAction = (PushVlanActionCase) sourceAction;
242                 PushVlan pushVlan = pushVlanFrom(pushVlanAction.getPushVlanAction());
243                 if (pushVlan != null) {
244                     targetAction.add(pushVlan);
245                 }
246             } else if (sourceAction instanceof SetDlDstActionCase) {
247                 MacAddress addressL2Dest = ((SetDlDstActionCase) sourceAction).getSetDlDstAction().getAddress();
248                 if (addressL2Dest != null) {
249                     targetAction.add(new SetDlDst(bytesFrom(addressL2Dest)));
250                 }
251             } else if (sourceAction instanceof SetDlSrcActionCase) {
252                 MacAddress addressL2Src = ((SetDlSrcActionCase) sourceAction).getSetDlSrcAction().getAddress();
253                 if (addressL2Src != null) {
254                     targetAction.add(new SetDlSrc(bytesFrom(addressL2Src)));
255
256                 }
257             } else if (sourceAction instanceof SetDlTypeActionCase) {
258                 EtherType dlType = ((SetDlTypeActionCase) sourceAction).getSetDlTypeAction().getDlType();
259                 if (dlType != null) {
260                     Long dlTypeValue = dlType.getValue();
261                     if (dlTypeValue != null) {
262                         targetAction.add(new SetDlType(dlTypeValue.intValue()));
263                     }
264                 }
265             } else if (sourceAction instanceof SetNextHopActionCase) {
266                 Address addressL3 = ((SetNextHopActionCase) sourceAction).getSetNextHopAction().getAddress();
267
268                 InetAddress inetAddress = inetAddressFrom(addressL3);
269                 if (inetAddress != null) {
270                     targetAction.add(new SetNextHop(inetAddress));
271                 }
272             } else if (sourceAction instanceof SetNwDstActionCase) {
273                 Address addressL3 = ((SetNwDstActionCase) sourceAction).getSetNwDstAction().getAddress();
274
275                 InetAddress inetAddress = inetAddressFrom(addressL3);
276                 if (inetAddress != null) {
277                     targetAction.add(new SetNwDst(inetAddress));
278                 }
279             } else if (sourceAction instanceof SetNwSrcActionCase) {
280                 Address addressL3 = ((SetNwSrcActionCase) sourceAction).getSetNwSrcAction().getAddress();
281
282                 InetAddress inetAddress = inetAddressFrom(addressL3);
283                 if (inetAddress != null) {
284                     targetAction.add(new SetNwSrc(inetAddress));
285                 }
286             } else if (sourceAction instanceof SetNwTosActionCase) {
287                 Integer tos = ((SetNwTosActionCase) sourceAction).getSetNwTosAction().getTos();
288                 if (tos != null) {
289                     targetAction.add(new SetNwTos(tos));
290                 }
291             } else if (sourceAction instanceof SetTpDstActionCase) {
292                 PortNumber port = ((SetTpDstActionCase) sourceAction).getSetTpDstAction().getPort();
293                 if (port != null) {
294                     Integer portValue = port.getValue();
295                     if (port.getValue() != null) {
296                         targetAction.add(new SetTpDst(portValue));
297                     }
298                 }
299             } else if (sourceAction instanceof SetTpSrcActionCase) {
300                 PortNumber port = ((SetTpSrcActionCase) sourceAction).getSetTpSrcAction().getPort();
301                 if (port != null) {
302                     Integer portValue = port.getValue();
303                     if (port.getValue() != null) {
304                         targetAction.add(new SetTpSrc(portValue));
305                     }
306                 }
307             } else if (sourceAction instanceof SetVlanCfiActionCase) {
308                 VlanCfi vlanCfi = ((SetVlanCfiActionCase) sourceAction).getSetVlanCfiAction().getVlanCfi();
309                 if (vlanCfi != null) {
310                     Integer vlanCfiValue = vlanCfi.getValue();
311                     if (vlanCfiValue != null) {
312                         targetAction.add(new SetVlanCfi(vlanCfiValue));
313                     }
314                 }
315             } else if (sourceAction instanceof SetVlanIdActionCase) {
316                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdActionCase) sourceAction).getSetVlanIdAction()
317                         .getVlanId();
318                 if (vlanID != null) {
319                     Integer vlanIdValue = vlanID.getValue();
320                     if (vlanIdValue != null) {
321                         targetAction.add(new SetVlanId(vlanIdValue));
322                     }
323                 }
324             } else if (sourceAction instanceof SetVlanPcpActionCase) {
325                 VlanPcp vlanPcp = ((SetVlanPcpActionCase) sourceAction).getSetVlanPcpAction().getVlanPcp();
326                 if (vlanPcp != null) {
327                     Short vlanPcpValue = vlanPcp.getValue();
328                     if (vlanPcpValue != null) {
329                         targetAction.add(new SetVlanPcp(vlanPcpValue));
330                     }
331                 }
332             } else if (sourceAction instanceof SwPathActionCase) {
333                 targetAction.add(new SwPath());
334             }
335         }
336
337         return targetAction;
338     }
339
340     private static InetAddress inetAddressFrom(Address addressL3) {
341         if (addressL3 != null) {
342             if (addressL3 instanceof Ipv4) {
343                 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
344                 if (addressL3Ipv4 != null) {
345                     return inetAddressFrom(addressL3Ipv4);
346                 }
347             } else if (addressL3 instanceof Ipv6) {
348                 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
349                 if (addressL3Ipv6 != null) {
350                     return inetAddressFrom(addressL3Ipv6);
351                 }
352             }
353         }
354         return null;
355     }
356
357     private static PushVlan pushVlanFrom(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanAction pushVlanAction) {
358         final int tag;
359         final int pcp;
360         final int cfi;
361         final int vlanId;
362
363         if (pushVlanAction.getTag() != null) {
364             tag = pushVlanAction.getTag();
365             if (pushVlanAction.getPcp() != null) {
366                 pcp = pushVlanAction.getPcp();
367                 if (pushVlanAction.getCfi() != null && pushVlanAction.getCfi().getValue() != null) {
368                     cfi = pushVlanAction.getCfi().getValue();
369                     if (pushVlanAction.getVlanId() != null && pushVlanAction.getVlanId().getValue() != null) {
370                         vlanId = pushVlanAction.getVlanId().getValue();
371                         return new PushVlan(tag, pcp, cfi, vlanId);
372                     }
373                 }
374             }
375         }
376         return null;
377     }
378
379     private static NodeConnector fromNodeConnectorRef(Uri uri, Node node) {
380         NodeConnector nodeConnector = null;
381         try {
382             nodeConnector = new NodeConnector(NodeMapping.MD_SAL_TYPE,node.getNodeIDString()+":"+uri.getValue(),node);
383         } catch (ConstructionException e) {
384             e.printStackTrace();
385         }
386         return nodeConnector;
387     }
388
389     public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match source) {
390         Match target = new Match();
391         if (source != null) {
392             fillFrom(target, source.getVlanMatch());
393             fillFrom(target, source.getEthernetMatch());
394             fillFrom(target, source.getLayer3Match());
395             fillFrom(target, source.getLayer4Match());
396             fillFrom(target, source.getIpMatch());
397             fillFrom(target, source.getInPort());
398         }
399
400         return target;
401     }
402
403     /**
404      * @param target
405      * @param inPort
406      */
407     private static void fillFrom(Match target, NodeConnectorId inPort) {
408         if (inPort != null) {
409             String inPortValue = inPort.getValue();
410             if (inPortValue != null) {
411                 try {
412                     target.setField(MatchType.IN_PORT, NodeMapping.toADNodeConnector(inPort,
413                             NodeMapping.toAdNodeId(inPort)));
414                 } catch (ConstructionException e) {
415                     LOG.warn("nodeConnector construction failed", e);
416                 }
417             }
418         }
419     }
420
421     private static void fillFrom(Match target, VlanMatch vlanMatch) {
422         if (vlanMatch != null) {
423             VlanId vlanId = vlanMatch.getVlanId();
424             if (vlanId != null) {
425                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
426                         .getVlanId();
427                 if (vlanIdInner != null) {
428                     Integer vlanValue = vlanIdInner.getValue();
429                     if (vlanValue != null) {
430                         target.setField(DL_VLAN, vlanValue.shortValue());
431                     }
432                 }
433             }
434             VlanPcp vlanPcp = vlanMatch.getVlanPcp();
435             if (vlanPcp != null) {
436                 Short vlanPcpValue = vlanPcp.getValue();
437                 if (vlanPcpValue != null) {
438                     target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
439                 }
440             }
441         }
442     }
443
444     private static void fillFrom(Match target, IpMatch ipMatch) {
445         if (ipMatch != null) {
446             Short ipProtocol = ipMatch.getIpProtocol();
447
448             if (ipProtocol != null && target.getField(NW_PROTO) == null) {
449                 target.setField(NW_PROTO, ipProtocol.byteValue());
450             }
451             Dscp dscp = ipMatch.getIpDscp();
452             if (dscp != null) {
453                 Short dscpValue = dscp.getValue();
454                 if (dscpValue != null) {
455                     target.setField(NW_TOS, dscpValue.byteValue());
456                 }
457             }
458         }
459     }
460
461     private static void fillFrom(Match target, Layer4Match layer4Match) {
462         if (layer4Match == null) {
463             return;
464         }
465         if (layer4Match instanceof SctpMatch) {
466             fillTransportLayer(target, (SctpMatch) layer4Match);
467         } else if (layer4Match instanceof TcpMatch) {
468             fillTransportLayer(target, (TcpMatch) layer4Match);
469         } else if (layer4Match instanceof UdpMatch) {
470             fillTransportLayer(target, (UdpMatch) layer4Match);
471         }
472     }
473
474     private static void fillTransportLayer(Match target, UdpMatch source) {
475         PortNumber udpSourcePort = source.getUdpSourcePort();
476         if (udpSourcePort != null) {
477             Integer udpSourcePortValue = udpSourcePort.getValue();
478             if (udpSourcePortValue != null) {
479                 target.setField(TP_SRC, udpSourcePortValue.shortValue());
480             }
481         }
482
483         PortNumber udpDestPort = source.getUdpDestinationPort();
484         if (udpDestPort != null) {
485             Integer udpDestPortValue = udpDestPort.getValue();
486             if (udpDestPortValue != null) {
487                 target.setField(TP_DST, udpDestPortValue.shortValue());
488             }
489         }
490
491         target.setField(NW_PROTO, UDP);
492     }
493
494     private static void fillTransportLayer(Match target, TcpMatch source) {
495         PortNumber tcpSourcePort = source.getTcpSourcePort();
496         if (tcpSourcePort != null) {
497             Integer tcpSourcePortValue = tcpSourcePort.getValue();
498             if (tcpSourcePortValue != null) {
499                 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
500             }
501         }
502
503         PortNumber tcpDestPort = source.getTcpDestinationPort();
504         if (tcpDestPort != null) {
505             Integer tcpDestPortValue = tcpDestPort.getValue();
506             if (tcpDestPortValue != null) {
507                 target.setField(TP_DST, tcpDestPortValue.shortValue());
508             }
509         }
510
511         target.setField(NW_PROTO, TCP);
512     }
513
514     private static void fillTransportLayer(Match target, SctpMatch source) {
515         PortNumber sctpSourcePort = source.getSctpSourcePort();
516         if (sctpSourcePort != null) {
517             Integer sctpSourcePortValue = sctpSourcePort.getValue();
518             if (sctpSourcePortValue != null) {
519                 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
520             }
521         }
522         PortNumber sctpDestPort = source.getSctpDestinationPort();
523         if (sctpDestPort != null) {
524             Integer sctpDestPortValue = sctpDestPort.getValue();
525             if (sctpDestPortValue != null) {
526                 target.setField(TP_DST, sctpDestPortValue.shortValue());
527             }
528         }
529
530         target.setField(NW_PROTO, CRUDP);
531
532     }
533
534     private static void fillFrom(Match target, Layer3Match source) {
535         if (source == null)
536             return;
537         if (source instanceof Ipv4Match) {
538             fillFromIpv4(target, (Ipv4Match) source);
539         } else if (source instanceof Ipv6Match) {
540             fillFromIpv6(target, (Ipv6Match) source);
541         } else if (source instanceof ArpMatch) {
542             fillFromArp(target, (ArpMatch) source);
543         }
544     }
545
546     private static void fillFromArp(Match target, ArpMatch source) {
547         Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
548         if (sourceAddress != null) {
549             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
550         }
551         Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
552         if (destAddress != null) {
553             target.setField(NW_DST, inetAddressFrom(destAddress), null);
554         }
555         ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
556         if (sourceHwAddress != null) {
557             target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
558         }
559         ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
560         if (targetHwAddress != null) {
561             target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
562         }
563
564         target.setField(DL_TYPE, new Short(ETHERNET_ARP));
565
566     }
567
568     private static void fillFromIpv6(Match target, Ipv6Match source) {
569         Ipv6Prefix sourceAddress = source.getIpv6Source();
570         if (sourceAddress != null) {
571             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
572         }
573         Ipv6Prefix destAddress = source.getIpv6Destination();
574         if (destAddress != null) {
575             target.setField(NW_DST, inetAddressFrom(destAddress), null);
576         }
577     }
578
579     private static void fillFromIpv4(Match target, Ipv4Match source) {
580         Ipv4Prefix sourceAddress = source.getIpv4Source();
581         if (sourceAddress != null) {
582             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
583         }
584         Ipv4Prefix destAddress = source.getIpv4Destination();
585         if (destAddress != null) {
586             target.setField(NW_DST, inetAddressFrom(destAddress), null);
587         }
588     }
589
590     private static InetAddress inetAddressFrom(Ipv4Prefix source) {
591         if (source != null) {
592             String[] parts = source.getValue().split("/");
593             return InetAddresses.forString(parts[0]);
594         }
595         return null;
596     }
597
598     private static InetAddress inetAddressFrom(Ipv6Prefix source) {
599         if (source != null) {
600             String[] parts = source.getValue().split("/");
601             return InetAddresses.forString(parts[0]);
602         }
603         return null;
604     }
605
606     private static void fillFrom(Match target, EthernetMatch source) {
607         if (source == null)
608             return;
609         EthernetType ethType = source.getEthernetType();
610         if (ethType != null) {
611             EtherType ethInnerType = ethType.getType();
612             if (ethInnerType != null && target.getField(DL_TYPE) == null) {
613                 Long value = ethInnerType.getValue();
614                 target.setField(DL_TYPE, value.shortValue());
615             }
616         }
617
618         MacAddressFilter ethSource = source.getEthernetSource();
619         if (ethSource != null) {
620             target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
621         }
622
623         MacAddressFilter ethDest = source.getEthernetDestination();
624         if (ethDest != null) {
625             target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
626         }
627     }
628
629     public static byte[] bytesFrom(MacAddress address) {
630         String[] mac = address.getValue().split(":");
631         byte[] macAddress = new byte[6]; // mac.length == 6 bytes
632         for (int i = 0; i < mac.length; i++) {
633             macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
634         }
635         return macAddress;
636     }
637
638     public static byte[] bytesFromDpid(long dpid) {
639         byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
640
641         for (short i = 0; i < 6; i++) {
642             mac[5 - i] = (byte) dpid;
643             dpid >>= 8;
644         }
645
646         return mac;
647     }
648 }