Merge "Added Update and Remove commands code"
[netvirt.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / workaround / services / SfcClassifierService.java
1 /*
2  * Copyright © 2015 Red Hat, 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
9 package org.opendaylight.ovsdb.openstack.netvirt.sfc.workaround.services;
10
11 import com.google.common.collect.Lists;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
16 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
17 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
18 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
19 import org.opendaylight.ovsdb.openstack.netvirt.sfc.ISfcClassifierService;
20 import org.opendaylight.ovsdb.openstack.netvirt.sfc.NshUtils;
21 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
22 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
23 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
24 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.Matches;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.matches.ace.type.AceEth;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
49 import org.osgi.framework.BundleContext;
50 import org.osgi.framework.ServiceReference;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class SfcClassifierService extends AbstractServiceInstance implements ConfigInterface, ISfcClassifierService {
55     private static final Logger LOG = LoggerFactory.getLogger(SfcClassifierService.class);
56     private static final short TABLE_0 = 0;
57     private static final short UDP_SHORT = 17;
58     static int cookieIndex = 0;
59
60     private enum FlowID {
61         FLOW_INGRESSCLASS(1), FLOW_SFINGRESS(2), FLOW_SFEGRESS(3), FLOW_SFARP(4),
62         FLOW_EGRESSCLASSUNUSED(5), FLOW_EGRESSCLASS(6), FLOW_EGRESSCLASSBYPASS(7), FLOW_SFCTABLE(8);
63
64         private int value;
65         FlowID(int value) {
66             this.value = value;
67         }
68
69     }
70
71     private BigInteger getCookie(FlowID flowID) {
72         String cookieString = String.format("1110%02d%010d", flowID.value, cookieIndex++);
73         return new BigInteger(cookieString, 16);
74     }
75
76     public SfcClassifierService(Service service) {
77         super(service);
78     }
79
80     public SfcClassifierService() {
81         super(Service.SFC_CLASSIFIER);
82     }
83
84     @Override
85     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
86         super.setDependencies(bundleContext.getServiceReference(ISfcClassifierService.class.getName()), this);
87     }
88
89     @Override
90     public void setDependencies(Object impl) {}
91
92     @Override
93     public void programIngressClassifier(long dataPathId, String ruleName, Matches matches,
94                                          NshUtils nshHeader, long vxGpeOfPort, boolean write) {
95         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dataPathId);
96         FlowBuilder flowBuilder = new FlowBuilder();
97
98         MatchBuilder matchBuilder = buildMatch(matches);
99         MatchUtils.addNxRegMatch(matchBuilder,
100                 MatchUtils.RegMatch.of(FlowUtils.REG_FIELD, FlowUtils.REG_VALUE_FROM_LOCAL));
101         MatchUtils.addNxRegMatch(matchBuilder,
102                 MatchUtils.RegMatch.of(FlowUtils.REG_FIELD, FlowUtils.REG_VALUE_FROM_LOCAL));
103         flowBuilder.setMatch(matchBuilder.build());
104
105         String flowId = "sfcIngressClass_" + ruleName;// + "_" + nshHeader.getNshNsp();
106         flowBuilder.setId(new FlowId(flowId));
107         FlowKey key = new FlowKey(new FlowId(flowId));
108         flowBuilder.setBarrier(true);
109         flowBuilder.setTableId(getTable());
110         flowBuilder.setKey(key);
111         flowBuilder.setFlowName(flowId);
112         flowBuilder.setHardTimeout(0);
113         flowBuilder.setIdleTimeout(0);
114         flowBuilder.setCookie(new FlowCookie(getCookie(FlowID.FLOW_INGRESSCLASS)));
115
116         if (write) {
117             ActionBuilder ab = new ActionBuilder();
118             List<Action> actionList = new ArrayList<>();
119
120             ab.setAction(ActionUtils.nxMoveTunIdtoNshc2());
121             ab.setOrder(actionList.size());
122             ab.setKey(new ActionKey(actionList.size()));
123             actionList.add(ab.build());
124
125             getNshAction(nshHeader, actionList);
126
127             ab.setAction(ActionUtils.outputAction(FlowUtils.getNodeConnectorId(dataPathId, vxGpeOfPort)));
128             ab.setOrder(actionList.size());
129             ab.setKey(new ActionKey(actionList.size()));
130             actionList.add(ab.build());
131
132             ApplyActionsBuilder aab = new ApplyActionsBuilder();
133             aab.setAction(actionList);
134
135             InstructionBuilder ib = new InstructionBuilder();
136             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
137             ib.setOrder(0);
138             ib.setKey(new InstructionKey(0));
139             List<Instruction> instructions = Lists.newArrayList();
140             instructions.add(ib.build());
141
142             InstructionsBuilder isb = new InstructionsBuilder();
143             isb.setInstruction(instructions);
144             flowBuilder.setInstructions(isb.build());
145             writeFlow(flowBuilder, nodeBuilder);
146         } else {
147             removeFlow(flowBuilder, nodeBuilder);
148         }
149     }
150
151     @Override
152     public void programSfcTable(long dataPathId, long vxGpeOfPort, short goToTableId, boolean write) {
153         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dataPathId);
154         FlowBuilder flowBuilder = new FlowBuilder();
155
156         MatchBuilder matchBuilder = new MatchBuilder();
157         MatchUtils.createInPortMatch(matchBuilder, dataPathId, vxGpeOfPort);
158         flowBuilder.setMatch(matchBuilder.build());
159
160         String flowId = "sfcTable_" + vxGpeOfPort;
161         flowBuilder.setId(new FlowId(flowId));
162         FlowKey key = new FlowKey(new FlowId(flowId));
163         flowBuilder.setBarrier(true);
164         flowBuilder.setTableId(TABLE_0);
165         flowBuilder.setKey(key);
166         flowBuilder.setFlowName(flowId);
167         flowBuilder.setHardTimeout(0);
168         flowBuilder.setIdleTimeout(0);
169         flowBuilder.setPriority(1000);
170         flowBuilder.setCookie(new FlowCookie(getCookie(FlowID.FLOW_SFCTABLE)));
171         flowBuilder.setCookieMask(new FlowCookie(getCookie(FlowID.FLOW_SFCTABLE)));
172
173         if (write) {
174             InstructionsBuilder isb = new InstructionsBuilder();
175             List<Instruction> instructions = Lists.newArrayList();
176             InstructionBuilder ib =
177                     InstructionUtils.createGotoTableInstructions(new InstructionBuilder(), goToTableId);
178             ib.setOrder(0);
179             ib.setKey(new InstructionKey(0));
180             instructions.add(ib.build());
181
182             isb.setInstruction(instructions);
183             flowBuilder.setInstructions(isb.build());
184             writeFlow(flowBuilder, nodeBuilder);
185         } else {
186             removeFlow(flowBuilder, nodeBuilder);
187         }
188     }
189
190     @Override
191     public void programEgressClassifier1(long dataPathId, long vxGpeOfPort, long nsp, short nsi,
192                                          int tunnelOfPort, int tunnelId, short gotoTableId, boolean write) {
193         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dataPathId);
194         FlowBuilder flowBuilder = new FlowBuilder();
195
196         MatchBuilder matchBuilder = new MatchBuilder();
197         MatchUtils.createInPortMatch(matchBuilder, dataPathId, vxGpeOfPort);
198         MatchUtils.addNxNspMatch(matchBuilder, nsp);
199         MatchUtils.addNxNsiMatch(matchBuilder, nsi);
200         flowBuilder.setMatch(matchBuilder.build());
201
202         String flowId = "sfcEgressClass1_" + vxGpeOfPort;
203         flowBuilder.setId(new FlowId(flowId));
204         FlowKey key = new FlowKey(new FlowId(flowId));
205         flowBuilder.setBarrier(true);
206         flowBuilder.setTableId(TABLE_0);
207         flowBuilder.setKey(key);
208         flowBuilder.setFlowName(flowId);
209         flowBuilder.setHardTimeout(0);
210         flowBuilder.setIdleTimeout(0);
211         flowBuilder.setCookie(new FlowCookie(getCookie(FlowID.FLOW_EGRESSCLASSUNUSED)));
212         flowBuilder.setCookieMask(new FlowCookie(getCookie(FlowID.FLOW_EGRESSCLASSUNUSED)));
213
214         if (write) {
215             InstructionsBuilder isb = new InstructionsBuilder();
216             List<Instruction> instructions = Lists.newArrayList();
217             InstructionBuilder ib = new InstructionBuilder();
218
219             /*List<Action> actionList = Lists.newArrayList();
220
221             ActionBuilder ab = new ActionBuilder();
222             ab.setAction(ActionUtils.nxMoveNshc2ToTunId());
223             ab.setOrder(0);
224             ab.setKey(new ActionKey(0));
225             actionList.add(ab.build());
226
227             ApplyActionsBuilder aab = new ApplyActionsBuilder();
228             aab.setAction(actionList);
229             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
230
231             ib.setOrder(instructions.size());
232             ib.setKey(new InstructionKey(instructions.size()));
233             instructions.add(ib.build());*/
234
235             ib = InstructionUtils.createGotoTableInstructions(new InstructionBuilder(), getTable());
236             ib.setOrder(instructions.size());
237             ib.setKey(new InstructionKey(instructions.size()));
238             instructions.add(ib.build());
239
240             isb.setInstruction(instructions);
241             flowBuilder.setInstructions(isb.build());
242             writeFlow(flowBuilder, nodeBuilder);
243         } else {
244             removeFlow(flowBuilder, nodeBuilder);
245         }
246     }
247
248     @Override
249     public void programEgressClassifier(long dataPathId, long vxGpeOfPort, long nsp, short nsi,
250                                         long sfOfPort, int tunnelId, boolean write) {
251         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dataPathId);
252         FlowBuilder flowBuilder = new FlowBuilder();
253
254         MatchBuilder matchBuilder = new MatchBuilder();
255         MatchUtils.createInPortMatch(matchBuilder, dataPathId, vxGpeOfPort);
256         MatchUtils.addNxNspMatch(matchBuilder, nsp);
257         MatchUtils.addNxNsiMatch(matchBuilder, nsi);
258         flowBuilder.setMatch(matchBuilder.build());
259
260         String flowId = "sfcEgressClass_" + nsp + "_" + + nsi + "_"  + vxGpeOfPort;
261         flowBuilder.setId(new FlowId(flowId));
262         FlowKey key = new FlowKey(new FlowId(flowId));
263         flowBuilder.setBarrier(true);
264         flowBuilder.setTableId(TABLE_0);
265         flowBuilder.setKey(key);
266         flowBuilder.setFlowName(flowId);
267         flowBuilder.setHardTimeout(0);
268         flowBuilder.setIdleTimeout(0);
269         flowBuilder.setCookie(new FlowCookie(getCookie(FlowID.FLOW_EGRESSCLASS)));
270         flowBuilder.setCookieMask(new FlowCookie(getCookie(FlowID.FLOW_EGRESSCLASS)));
271
272         if (write) {
273             InstructionsBuilder isb = new InstructionsBuilder();
274             List<Instruction> instructions = Lists.newArrayList();
275             List<Action> actionList = Lists.newArrayList();
276
277             ActionBuilder ab = new ActionBuilder();
278
279             ab.setAction(
280                     ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(FlowUtils.REG_FIELD).build(),
281                     BigInteger.valueOf(FlowUtils.REG_VALUE_FROM_LOCAL)));
282             ab.setOrder(actionList.size());
283             ab.setKey(new ActionKey(actionList.size()));
284             actionList.add(ab.build());
285
286             ab.setAction(ActionUtils.nxMoveNshc2ToTunId());
287             ab.setOrder(actionList.size());
288             ab.setKey(new ActionKey(actionList.size()));
289             actionList.add(ab.build());
290
291             ab.setAction(ActionUtils.nxResubmitAction((int)sfOfPort, TABLE_0));
292             ab.setOrder(actionList.size());
293             ab.setKey(new ActionKey(actionList.size()));
294             actionList.add(ab.build());
295
296             ApplyActionsBuilder aab = new ApplyActionsBuilder();
297             aab.setAction(actionList);
298             InstructionBuilder ib = new InstructionBuilder();
299             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
300
301             ib.setOrder(0);
302             ib.setKey(new InstructionKey(0));
303             instructions.add(ib.build());
304
305             isb.setInstruction(instructions);
306             flowBuilder.setInstructions(isb.build());
307             writeFlow(flowBuilder, nodeBuilder);
308         } else {
309             removeFlow(flowBuilder, nodeBuilder);
310         }
311     }
312
313     @Override
314     public void programEgressClassifierBypass(long dataPathId, long vxGpeOfPort, long nsp, short nsi,
315                                               long sfOfPort, int tunnelId, boolean write) {
316         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dataPathId);
317         FlowBuilder flowBuilder = new FlowBuilder();
318
319         MatchBuilder matchBuilder = new MatchBuilder();
320         MatchUtils.createInPortMatch(matchBuilder, dataPathId, sfOfPort);
321         MatchUtils.addNxRegMatch(matchBuilder,
322                 MatchUtils.RegMatch.of(FlowUtils.REG_FIELD, FlowUtils.REG_VALUE_FROM_LOCAL));
323         MatchUtils.addNxNspMatch(matchBuilder, nsp);
324         MatchUtils.addNxNsiMatch(matchBuilder, nsi);
325         flowBuilder.setMatch(matchBuilder.build());
326
327         String flowId = "sfcEgressClassBypass_" + nsp + "_" + + nsi + "_"  + sfOfPort;
328         flowBuilder.setId(new FlowId(flowId));
329         FlowKey key = new FlowKey(new FlowId(flowId));
330         flowBuilder.setBarrier(true);
331         flowBuilder.setTableId(TABLE_0);
332         flowBuilder.setKey(key);
333         flowBuilder.setFlowName(flowId);
334         flowBuilder.setHardTimeout(0);
335         flowBuilder.setIdleTimeout(0);
336         flowBuilder.setCookie(new FlowCookie(getCookie(FlowID.FLOW_EGRESSCLASSBYPASS)));
337         flowBuilder.setCookieMask(new FlowCookie(getCookie(FlowID.FLOW_EGRESSCLASSBYPASS)));
338         flowBuilder.setPriority(40000); //Needs to be above default priority of 32768
339
340         if (write) {
341             InstructionsBuilder isb = new InstructionsBuilder();
342             List<Instruction> instructions = Lists.newArrayList();
343
344             InstructionBuilder ib;
345             ib = this.getMutablePipelineInstructionBuilder();
346             ib.setOrder(0);
347             ib.setKey(new InstructionKey(0));
348             instructions.add(ib.build());
349
350             isb.setInstruction(instructions);
351             flowBuilder.setInstructions(isb.build());
352             writeFlow(flowBuilder, nodeBuilder);
353         } else {
354             removeFlow(flowBuilder, nodeBuilder);
355         }
356     }
357
358     // packet from sf to sff that need to go out local
359     @Override
360     public void program_sfEgress(long dataPathId, int dstPort, boolean write) {
361         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dataPathId);
362         FlowBuilder flowBuilder = new FlowBuilder();
363
364         MatchBuilder matchBuilder = new MatchBuilder();
365         MatchUtils.createIpProtocolMatch(matchBuilder, UDP_SHORT);
366         MatchUtils.addLayer4Match(matchBuilder, UDP_SHORT, 0, dstPort);
367         MatchUtils.addNxRegMatch(matchBuilder,
368                 MatchUtils.RegMatch.of(FlowUtils.REG_FIELD, FlowUtils.REG_VALUE_FROM_LOCAL));
369         flowBuilder.setMatch(matchBuilder.build());
370
371         String flowId = "sfEgress_" + dstPort;
372         flowBuilder.setId(new FlowId(flowId));
373         FlowKey key = new FlowKey(new FlowId(flowId));
374         flowBuilder.setBarrier(true);
375         flowBuilder.setTableId(getTable());
376         flowBuilder.setKey(key);
377         flowBuilder.setFlowName(flowId);
378         flowBuilder.setHardTimeout(0);
379         flowBuilder.setIdleTimeout(0);
380         flowBuilder.setCookie(new FlowCookie(getCookie(FlowID.FLOW_SFEGRESS)));
381         flowBuilder.setCookieMask(new FlowCookie(getCookie(FlowID.FLOW_SFEGRESS)));
382
383         if (write) {
384             InstructionBuilder ib = new InstructionBuilder();
385             InstructionsBuilder isb = new InstructionsBuilder();
386             List<Instruction> instructions = Lists.newArrayList();
387             InstructionUtils.createLocalInstructions(ib, dataPathId);
388             ib.setOrder(0);
389             ib.setKey(new InstructionKey(0));
390             instructions.add(ib.build());
391
392             isb.setInstruction(instructions);
393             flowBuilder.setInstructions(isb.build());
394             writeFlow(flowBuilder, nodeBuilder);
395         } else {
396             removeFlow(flowBuilder, nodeBuilder);
397         }
398     }
399
400     // looped back sff to sf packets
401     @Override
402     public void program_sfIngress(long dataPathId, int dstPort, long sfOfPort,
403                                   String ipAddress, String sfDplName, boolean write) {
404         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dataPathId);
405         FlowBuilder flowBuilder = new FlowBuilder();
406
407         MatchBuilder matchBuilder = new MatchBuilder();
408         MatchUtils.createIpProtocolMatch(matchBuilder, UDP_SHORT);
409         Ipv4Prefix ipCidr = MatchUtils.iPv4PrefixFromIPv4Address(ipAddress);
410         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(ipCidr));
411         MatchUtils.addLayer4Match(matchBuilder, UDP_SHORT, 0, dstPort);
412         flowBuilder.setMatch(matchBuilder.build());
413
414         String flowId = "sfIngress_" + dstPort + "_" + ipAddress;
415         flowBuilder.setId(new FlowId(flowId));
416         FlowKey key = new FlowKey(new FlowId(flowId));
417         flowBuilder.setBarrier(true);
418         flowBuilder.setTableId(TABLE_0);
419         flowBuilder.setKey(key);
420         flowBuilder.setFlowName(flowId);
421         flowBuilder.setHardTimeout(0);
422         flowBuilder.setIdleTimeout(0);
423         flowBuilder.setCookie(new FlowCookie(getCookie(FlowID.FLOW_SFINGRESS)));
424         flowBuilder.setCookieMask(new FlowCookie(getCookie(FlowID.FLOW_SFINGRESS)));
425
426         if (write) {
427             InstructionBuilder ib = new InstructionBuilder();
428             InstructionsBuilder isb = new InstructionsBuilder();
429             List<Instruction> instructions = Lists.newArrayList();
430             InstructionUtils.createOutputPortInstructions(ib, dataPathId, sfOfPort);
431
432             ib.setOrder(0);
433             ib.setKey(new InstructionKey(0));
434             instructions.add(ib.build());
435
436             isb.setInstruction(instructions);
437             flowBuilder.setInstructions(isb.build());
438             writeFlow(flowBuilder, nodeBuilder);
439         } else {
440             removeFlow(flowBuilder, nodeBuilder);
441         }
442     }
443
444     @Override
445     public void programStaticArpEntry(long dataPathId, long ofPort, String macAddressStr,
446                                       String ipAddress, boolean write) {
447         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dataPathId);
448         FlowBuilder flowBuilder = new FlowBuilder();
449
450         MacAddress macAddress = new MacAddress(macAddressStr);
451
452         MatchBuilder matchBuilder = new MatchBuilder();
453         MatchUtils.createInPortReservedMatch(matchBuilder, dataPathId, OutputPortValues.LOCAL.toString());
454         MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
455         MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress));
456         flowBuilder.setMatch(matchBuilder.build());
457
458         String flowId = "ArpResponder_" + ipAddress;
459         flowBuilder.setId(new FlowId(flowId));
460         FlowKey key = new FlowKey(new FlowId(flowId));
461         flowBuilder.setBarrier(true);
462         flowBuilder.setTableId(TABLE_0);
463         flowBuilder.setKey(key);
464         flowBuilder.setPriority(1024);
465         flowBuilder.setFlowName(flowId);
466         flowBuilder.setHardTimeout(0);
467         flowBuilder.setIdleTimeout(0);
468         flowBuilder.setCookie(new FlowCookie(getCookie(FlowID.FLOW_SFARP)));
469         flowBuilder.setCookieMask(new FlowCookie(getCookie(FlowID.FLOW_SFARP)));
470
471         if (write == true) {
472             InstructionBuilder ib = new InstructionBuilder();
473             InstructionsBuilder isb = new InstructionsBuilder();
474             List<Instruction> instructions = Lists.newArrayList();
475             ApplyActionsBuilder aab = new ApplyActionsBuilder();
476             ActionBuilder ab = new ActionBuilder();
477             List<Action> actionList = Lists.newArrayList();
478
479             // Move Eth Src to Eth Dst
480             ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
481             ab.setOrder(0);
482             ab.setKey(new ActionKey(0));
483             actionList.add(ab.build());
484
485             // Set Eth Src
486             ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(macAddress)));
487             ab.setOrder(1);
488             ab.setKey(new ActionKey(1));
489             actionList.add(ab.build());
490
491             // Set ARP OP
492             ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(FlowUtils.ARP_OP_REPLY)));
493             ab.setOrder(2);
494             ab.setKey(new ActionKey(2));
495             actionList.add(ab.build());
496
497             // Move ARP SHA to ARP THA
498             ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
499             ab.setOrder(3);
500             ab.setKey(new ActionKey(3));
501             actionList.add(ab.build());
502
503             // Move ARP SPA to ARP TPA
504             ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
505             ab.setOrder(4);
506             ab.setKey(new ActionKey(4));
507             actionList.add(ab.build());
508
509             // Load Mac to ARP SHA
510             ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
511             ab.setOrder(5);
512             ab.setKey(new ActionKey(5));
513             actionList.add(ab.build());
514
515             // Load IP to ARP SPA
516             ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress));
517             ab.setOrder(6);
518             ab.setKey(new ActionKey(6));
519             actionList.add(ab.build());
520
521             // Output of InPort
522             ab.setAction(ActionUtils.outputAction(
523                     FlowUtils.getSpecialNodeConnectorId(dataPathId, OutputPortValues.INPORT.toString())));
524             ab.setOrder(7);
525             ab.setKey(new ActionKey(7));
526             actionList.add(ab.build());
527
528             // Create Apply Actions Instruction
529             aab.setAction(actionList);
530             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
531             ib.setOrder(0);
532             ib.setKey(new InstructionKey(0));
533             instructions.add(ib.build());
534
535             isb.setInstruction(instructions);
536             flowBuilder.setInstructions(isb.build());
537             writeFlow(flowBuilder, nodeBuilder);
538         } else {
539             removeFlow(flowBuilder, nodeBuilder);
540         }
541     }
542
543     private List<Action> getNshAction(NshUtils header, List<Action> actionList) {
544         // Build the Actions to Add the NSH Header
545         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nshC1Load =
546                 ActionUtils.nxLoadNshc1RegAction(header.getNshMetaC1());
547         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nshC2Load =
548                 ActionUtils.nxLoadNshc2RegAction(header.getNshMetaC2());
549         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nspLoad =
550                 ActionUtils.nxSetNspAction(header.getNshNsp());
551         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nsiLoad =
552                 ActionUtils.nxSetNsiAction(header.getNshNsi());
553         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action loadChainTunVnid =
554                 ActionUtils.nxLoadTunIdAction(BigInteger.valueOf(header.getNshNsp()), false);
555         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action loadChainTunDest =
556                 ActionUtils.nxLoadTunIPv4Action(header.getNshTunIpDst().getValue(), false);
557
558         int count = actionList.size();
559         actionList.add(new ActionBuilder()
560                 .setKey(new ActionKey(count)).setOrder(count++).setAction(nshC1Load).build());
561         //actionList.add(new ActionBuilder()
562         // .setKey(new ActionKey(count)).setOrder(count++).setAction(nshC2Load).build());
563         actionList.add(new ActionBuilder()
564                 .setKey(new ActionKey(count)).setOrder(count++).setAction(nspLoad).build());
565         actionList.add(new ActionBuilder()
566                 .setKey(new ActionKey(count)).setOrder(count++).setAction(nsiLoad).build());
567         actionList.add(new ActionBuilder()
568                 .setKey(new ActionKey(count)).setOrder(count++).setAction(loadChainTunDest).build());
569         actionList.add(new ActionBuilder()
570                 .setKey(new ActionKey(count)).setOrder(count).setAction(loadChainTunVnid).build());
571         return actionList;
572     }
573
574     public MatchBuilder buildMatch(Matches matches) {
575         MatchBuilder matchBuilder = new MatchBuilder();
576
577         if (matches.getAceType() instanceof AceIp) {
578             AceIp aceIp = (AceIp)matches.getAceType();
579             if (aceIp.getAceIpVersion() instanceof AceIpv4) {
580                 //AceIpv4 aceIpv4 = (AceIpv4) aceIp.getAceIpVersion();
581                 //MatchUtils.createSrcL3IPv4Match(matchBuilder, aceIpv4.getSourceIpv4Network());
582                 //MatchUtils.createDstL3IPv4Match(matchBuilder, aceIpv4.getDestinationIpv4Network());
583                 MatchUtils.createIpProtocolMatch(matchBuilder, aceIp.getProtocol());
584                 MatchUtils.addLayer4Match(matchBuilder, aceIp.getProtocol().intValue(), 0,
585                         aceIp.getDestinationPortRange().getLowerPort().getValue().intValue());
586             } else {
587                 MatchUtils.createIpProtocolMatch(matchBuilder, aceIp.getProtocol());
588                 MatchUtils.addLayer4Match(matchBuilder, aceIp.getProtocol().intValue(), 0,
589                         aceIp.getDestinationPortRange().getLowerPort().getValue().intValue());
590             }
591         } else if (matches.getAceType() instanceof AceEth) {
592             AceEth aceEth = (AceEth) matches.getAceType();
593             MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(aceEth.getSourceMacAddress().getValue()));
594             MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(aceEth.getDestinationMacAddress().getValue()),
595                     new MacAddress(aceEth.getDestinationMacAddressMask().getValue()));
596         }
597
598         LOG.info("buildMatch: {}", matchBuilder.build());
599         return matchBuilder;
600     }
601 }