Bump odlparent->6.0.0,mdsal->5.0.3
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanNodeListener.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.netvirt.elan.internal;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.infrautils.utils.concurrent.LoggingFutures.addErrorLogging;
12
13 import java.math.BigInteger;
14 import java.time.Duration;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import javax.annotation.PostConstruct;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
26 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
27 import org.opendaylight.genius.infra.Datastore;
28 import org.opendaylight.genius.infra.Datastore.Configuration;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
30 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
31 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
32 import org.opendaylight.genius.infra.TypedWriteTransaction;
33 import org.opendaylight.genius.mdsalutil.ActionInfo;
34 import org.opendaylight.genius.mdsalutil.BucketInfo;
35 import org.opendaylight.genius.mdsalutil.FlowEntity;
36 import org.opendaylight.genius.mdsalutil.GroupEntity;
37 import org.opendaylight.genius.mdsalutil.InstructionInfo;
38 import org.opendaylight.genius.mdsalutil.MDSALUtil;
39 import org.opendaylight.genius.mdsalutil.MatchInfo;
40 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
41 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
42 import org.opendaylight.genius.mdsalutil.NwConstants;
43 import org.opendaylight.genius.mdsalutil.NwConstants.NxmOfFieldType;
44 import org.opendaylight.genius.mdsalutil.actions.ActionDrop;
45 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
46 import org.opendaylight.genius.mdsalutil.actions.ActionLearn;
47 import org.opendaylight.genius.mdsalutil.actions.ActionLearn.CopyFromValue;
48 import org.opendaylight.genius.mdsalutil.actions.ActionLearn.MatchFromField;
49 import org.opendaylight.genius.mdsalutil.actions.ActionLearn.MatchFromValue;
50 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
51 import org.opendaylight.genius.mdsalutil.actions.ActionPuntToController;
52 import org.opendaylight.genius.mdsalutil.actions.ActionRegLoad;
53 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
54 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
55 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
56 import org.opendaylight.genius.mdsalutil.matches.MatchArpOp;
57 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
58 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
59 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchRegister;
60 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
61 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
62 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderConstant;
63 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
64 import org.opendaylight.netvirt.elan.utils.ElanConstants;
65 import org.opendaylight.netvirt.elan.utils.ElanUtils;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.opendaylight.yangtools.yang.common.Uint64;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
79
80 @Singleton
81 public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, ElanNodeListener> {
82
83     private static final Logger LOG = LoggerFactory.getLogger(ElanNodeListener.class);
84     private static final int LEARN_MATCH_REG4_VALUE = 1;
85     private static final int ARP_LEARN_FLOW_PRIORITY = 10;
86     private static final int ARP_LEARN_MATCH_VALUE = 0x1;
87     private static final int GARP_LEARN_MATCH_VALUE = 0x101;
88     private static final long ARP_LEARN_MATCH_MASK = 0xFFFFL;
89
90     private final DataBroker broker;
91     private final ManagedNewTransactionRunner txRunner;
92     private final IMdsalApiManager mdsalManager;
93     private final IdManagerService idManagerService;
94     private final int tempSmacLearnTimeout;
95     private final int arpPuntTimeout;
96     private final boolean puntLldpToController;
97     private final JobCoordinator jobCoordinator;
98     private final DataTreeEventCallbackRegistrar eventCallbacks;
99
100     @Inject
101     public ElanNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager, ElanConfig elanConfig,
102             IdManagerService idManagerService, JobCoordinator jobCoordinator,
103             DataTreeEventCallbackRegistrar eventCallbacks) {
104         this.broker = dataBroker;
105         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
106         this.mdsalManager = mdsalManager;
107         this.tempSmacLearnTimeout = elanConfig.getTempSmacLearnTimeout().toJava();
108         this.arpPuntTimeout = elanConfig.getArpPuntTimeout().intValue();
109         this.puntLldpToController = elanConfig.isPuntLldpToController();
110         this.idManagerService = idManagerService;
111         this.jobCoordinator = jobCoordinator;
112         this.eventCallbacks = eventCallbacks;
113     }
114
115     @Override
116     @PostConstruct
117     public void init() {
118         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
119     }
120
121     @Override
122     protected InstanceIdentifier<Node> getWildCardPath() {
123         return InstanceIdentifier.create(Nodes.class).child(Node.class);
124     }
125
126     @Override
127     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
128     }
129
130     @Override
131     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
132     }
133
134     @Override
135     protected void add(InstanceIdentifier<Node> identifier, Node add) {
136         NodeId nodeId = add.getId();
137         String[] node = nodeId.getValue().split(":");
138         if (node.length < 2) {
139             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
140             return;
141         }
142         addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION, tx -> {
143             Uint64 dpId = Uint64.valueOf(node[1]);
144             createTableMissEntry(tx, dpId);
145             createMulticastFlows(tx, dpId);
146             createArpDefaultFlowsForArpCheckTable(dpId);
147         }), LOG, "Error handling ELAN node addition for {}", add);
148     }
149
150     private void createArpDefaultFlowsForArpCheckTable(Uint64 dpId) {
151
152         jobCoordinator.enqueueJob("ARP_CHECK_TABLE-" + dpId.toString(),
153             () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
154                 try {
155                     LOG.debug("Received notification to install Arp Check Default entries for dpn {} ", dpId);
156                     createArpRequestMatchFlows(dpId, tx);
157                     createArpResponseMatchFlows(dpId, tx);
158                     createArpPuntAndLearnFlow(dpId, tx);
159                     addGarpLearnMatchFlow(dpId, tx);
160                     addArpLearnMatchFlow(dpId, tx);
161                 } catch (InterruptedException | ExecutionException e) {
162                     LOG.error("Error programming ARP rules for dpn {}", dpId, e);
163                 }
164             })));
165     }
166
167     public void createTableMissEntry(TypedWriteTransaction<Configuration> tx, Uint64 dpnId) {
168         setupTableMissSmacFlow(tx, dpnId);
169         setupTableMissDmacFlow(tx, dpnId);
170         setupTableMissArpCheckFlow(tx, dpnId);
171         setupTableMissApResponderFlow(tx, dpnId);
172         setupExternalL2vniTableMissFlow(tx, dpnId);
173     }
174
175     private void createMulticastFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
176         createL2ControlProtocolDropFlows(tx, dpId);
177         createMulticastPuntFlows(tx, dpId);
178     }
179
180     private void createMulticastPuntFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
181         if (puntLldpToController) {
182             createLldpFlows(tx, dpId);
183         }
184     }
185
186     private void createLldpFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
187         createLldpFlow(tx, dpId, ElanConstants.LLDP_DST_1, "LLDP dMac Table Flow 1");
188         createLldpFlow(tx, dpId, ElanConstants.LLDP_DST_2, "LLDP dMac Table Flow 2");
189         createLldpFlow(tx, dpId, ElanConstants.LLDP_DST_3, "LLDP dMac Table Flow 3");
190     }
191
192     private void createLldpFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId, String dstMac,
193             String flowName) {
194         List<MatchInfo> mkMatches = new ArrayList<>();
195         mkMatches.add(new MatchEthernetType(ElanConstants.LLDP_ETH_TYPE));
196         mkMatches.add(new MatchEthernetDestination(new MacAddress(dstMac)));
197
198         List<ActionInfo> listActionInfo = new ArrayList<>();
199         listActionInfo.add(new ActionPuntToController());
200
201         List<InstructionInfo> mkInstructions = new ArrayList<>();
202         mkInstructions.add(new InstructionApplyActions(listActionInfo));
203
204         String flowId = dpId.toString() + NwConstants.ELAN_DMAC_TABLE + "lldp" + ElanConstants.LLDP_ETH_TYPE + dstMac;
205         FlowEntity lldpFlow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 16, flowName, 0, 0,
206                 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
207
208         mdsalManager.addFlow(tx, lldpFlow);
209     }
210
211     private void setupTableMissSmacFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
212         List<ActionInfo> actionsInfos = new ArrayList<>();
213         actionsInfos.add(new ActionPuntToController());
214         actionsInfos.add(new ActionLearn(0, tempSmacLearnTimeout, 0, ElanConstants.COOKIE_ELAN_LEARNED_SMAC, 0,
215                 NwConstants.ELAN_SMAC_LEARNED_TABLE, 0, 0,
216                 Arrays.asList(
217                         new ActionLearn.MatchFromField(NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getType(),
218                                 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getType(),
219                                 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getFlowModHeaderLenInt()),
220                         new ActionLearn.MatchFromField(NwConstants.NxmOfFieldType.NXM_NX_REG1.getType(),
221                                 NwConstants.NxmOfFieldType.NXM_NX_REG1.getType(), ElanConstants.INTERFACE_TAG_LENGTH),
222                         new ActionLearn.CopyFromValue(LEARN_MATCH_REG4_VALUE,
223                                 NwConstants.NxmOfFieldType.NXM_NX_REG4.getType(), 8))));
224
225         List<InstructionInfo> mkInstructions = new ArrayList<>();
226         mkInstructions.add(new InstructionApplyActions(actionsInfos));
227         mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
228
229         List<MatchInfo> mkMatches = new ArrayList<>();
230         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
231                 getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE), 0, "ELAN sMac Table Miss Flow", 0, 0,
232                 ElanConstants.COOKIE_ELAN_KNOWN_SMAC, mkMatches, mkInstructions);
233         mdsalManager.addFlow(tx, flowEntity);
234
235         addSmacBaseTableFlow(tx, dpId);
236         addSmacLearnedTableFlow(tx, dpId);
237     }
238
239     private void addSmacBaseTableFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
240         // T48 - resubmit to T49 & T50
241         List<ActionInfo> actionsInfo = new ArrayList<>();
242         actionsInfo.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_LEARNED_TABLE));
243         actionsInfo.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_TABLE));
244         List<InstructionInfo> mkInstruct = new ArrayList<>();
245         mkInstruct.add(new InstructionApplyActions(actionsInfo));
246         List<MatchInfo> mkMatch = new ArrayList<>();
247         FlowEntity doubleResubmitTable = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_BASE_TABLE,
248                 getTableMissFlowRef(NwConstants.ELAN_BASE_TABLE), 0, "Elan sMac resubmit table", 0, 0,
249                 ElanConstants.COOKIE_ELAN_BASE_SMAC, mkMatch, mkInstruct);
250         mdsalManager.addFlow(tx, doubleResubmitTable);
251     }
252
253     private void addSmacLearnedTableFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
254         // T50 - match on Reg4 and goto T51
255         List<MatchInfoBase> mkMatches = new ArrayList<>();
256         mkMatches.add(new NxMatchRegister(NxmNxReg4.class, LEARN_MATCH_REG4_VALUE));
257         List<InstructionInfo> mkInstructions = new ArrayList<>();
258         mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
259         String flowRef = new StringBuilder().append(NwConstants.ELAN_SMAC_TABLE).append(NwConstants.FLOWID_SEPARATOR)
260                 .append(LEARN_MATCH_REG4_VALUE).toString();
261         FlowEntity flowEntity =
262                 MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE, flowRef, 10, "ELAN sMac Table Reg4 Flow",
263                         0, 0, Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_SMAC.toJava()
264                             .add(BigInteger.valueOf(LEARN_MATCH_REG4_VALUE))),
265                         mkMatches, mkInstructions);
266         mdsalManager.addFlow(tx, flowEntity);
267     }
268
269     private void setupTableMissDmacFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
270         List<MatchInfo> mkMatches = new ArrayList<>();
271
272         List<InstructionInfo> mkInstructions = new ArrayList<>();
273         mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_UNKNOWN_DMAC_TABLE));
274
275         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE,
276                 getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE), 0, "ELAN dMac Table Miss Flow", 0, 0,
277                 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
278
279         mdsalManager.addFlow(tx, flowEntity);
280     }
281
282     private void setupExternalL2vniTableMissFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpnId) {
283         List<MatchInfo> matches = new ArrayList<>();
284         List<ActionInfo> actionsInfos = Collections.singletonList(new ActionNxResubmit(NwConstants
285                         .LPORT_DISPATCHER_TABLE));
286         List<InstructionInfo> instructions = Collections.singletonList(new InstructionApplyActions(actionsInfos));
287         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L2VNI_EXTERNAL_TUNNEL_DEMUX_TABLE,
288                         getTableMissFlowRef(NwConstants.L2VNI_EXTERNAL_TUNNEL_DEMUX_TABLE), 0,
289                         "External L2VNI Table Miss Flow", 0, 0,
290                          ElanConstants.COOKIE_L2VNI_DEMUX, matches, instructions);
291         mdsalManager.addFlow(tx, flowEntity);
292     }
293
294
295     private void createL2ControlProtocolDropFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
296         List<MatchInfo> mkMatches = new ArrayList<>();
297         MatchEthernetDestination matchEthDst =
298                 new MatchEthernetDestination(new MacAddress(ElanConstants.L2_CONTROL_PACKETS_DMAC),
299                         new MacAddress(ElanConstants.L2_CONTROL_PACKETS_DMAC_MASK));
300
301         mkMatches.add(matchEthDst);
302
303         List<ActionInfo> listActionInfo = new ArrayList<>();
304         listActionInfo.add(new ActionDrop());
305
306         List<InstructionInfo> mkInstructions = new ArrayList<>();
307         mkInstructions.add(new InstructionApplyActions(listActionInfo));
308
309         String flowId = dpId.toString() + NwConstants.ELAN_DMAC_TABLE + "l2control"
310                 + ElanConstants.L2_CONTROL_PACKETS_DMAC + ElanConstants.L2_CONTROL_PACKETS_DMAC_MASK;
311         FlowEntity flow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 15,
312                 "L2 control packets dMac Table Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches,
313                 mkInstructions);
314
315         mdsalManager.addFlow(tx, flow);
316     }
317
318     private static String getTableMissFlowRef(long tableId) {
319         return String.valueOf(tableId);
320     }
321
322     @Override
323     protected ElanNodeListener getDataTreeChangeListener() {
324         return ElanNodeListener.this;
325     }
326
327     private void setupTableMissApResponderFlow(TypedWriteTransaction<Configuration> tx, final Uint64 dpnId) {
328         mdsalManager.addFlow(tx, ArpResponderUtil.getArpResponderTableMissFlow(dpnId));
329     }
330
331     private void setupTableMissArpCheckFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpnId) {
332         mdsalManager.addFlow(tx,
333                 MDSALUtil.buildFlowEntity(dpnId, NwConstants.ARP_CHECK_TABLE,
334                         String.valueOf("L2.ELAN." + NwConstants.ARP_CHECK_TABLE), NwConstants.TABLE_MISS_PRIORITY,
335                         ArpResponderConstant.DROP_FLOW_NAME.value(), 0, 0, NwConstants.COOKIE_ARP_RESPONDER,
336                         new ArrayList<MatchInfo>(),
337                         Collections.singletonList(new InstructionGotoTable(NwConstants.ELAN_BASE_TABLE))));
338     }
339
340     private void createArpRequestMatchFlows(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx)
341             throws ExecutionException, InterruptedException {
342         long arpRequestGroupId = ArpResponderUtil.retrieveStandardArpResponderGroupId(idManagerService);
343         List<BucketInfo> buckets = ArpResponderUtil.getDefaultBucketInfos(NwConstants.ARP_RESPONDER_TABLE);
344         LOG.trace("Installing group flow on dpn {}", dpId);
345         GroupEntity groupEntity =
346             MDSALUtil.buildGroupEntity(dpId, arpRequestGroupId, ArpResponderConstant.GROUP_FLOW_NAME.value(),
347                 GroupTypes.GroupAll, buckets);
348         mdsalManager.addGroup(tx, groupEntity);
349         InstanceIdentifier<Group> groupIid = ElanUtils.getGroupInstanceid(dpId, arpRequestGroupId);
350         if (tx.read(groupIid).get().isPresent()) {
351             LOG.info("group {} is present in the config hence adding the flow", arpRequestGroupId);
352             createArpRequestMatchFlowsForGroup(dpId, arpRequestGroupId,tx);
353             return;
354         }
355         eventCallbacks.onAddOrUpdate(LogicalDatastoreType.CONFIGURATION,
356                 ElanUtils.getGroupInstanceid(dpId, arpRequestGroupId), (unused, newGroupId) -> {
357                 LOG.info("group {} added in the config", arpRequestGroupId);
358                 LoggingFutures.addErrorLogging(
359                         txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
360                             innerConfTx -> createArpRequestMatchFlowsForGroup(dpId, arpRequestGroupId,
361                                     innerConfTx)),
362                         LOG, "Error adding flow for the group {}",arpRequestGroupId);
363                 return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
364             }, Duration.ofSeconds(5), iid -> LOG.error("arpRequestGroupId {} not found in Config datastore",
365                         arpRequestGroupId));
366
367     }
368
369     private void createArpRequestMatchFlowsForGroup(Uint64 dpId, long arpRequestGroupId,
370             TypedReadWriteTransaction<Configuration> tx) {
371         FlowEntity arpReqArpCheckTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.ARP_CHECK_TABLE,
372                 NwConstants.ARP_REQUEST, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REQUEST), () ->
373                         Arrays.asList(new ActionGroup(arpRequestGroupId),
374                                 new ActionNxResubmit(NwConstants.ARP_LEARN_TABLE_1),
375                                 new ActionNxResubmit(NwConstants.ARP_LEARN_TABLE_2),
376                                 new ActionNxResubmit(NwConstants.ELAN_BASE_TABLE)));
377         LOG.trace("Invoking MDSAL to install Arp Rquest Match Flow for table {}", NwConstants.ARP_CHECK_TABLE);
378         mdsalManager.addFlow(tx, arpReqArpCheckTbl);
379     }
380
381     private void createArpResponseMatchFlows(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx) {
382         FlowEntity arpRepArpCheckTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.ARP_CHECK_TABLE,
383                 NwConstants.ARP_REPLY, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REPLY), () ->
384                         Arrays.asList(new ActionNxResubmit(NwConstants.ARP_LEARN_TABLE_1),
385                                 new ActionNxResubmit(NwConstants.ARP_LEARN_TABLE_2),
386                                 new ActionNxResubmit(NwConstants.ELAN_BASE_TABLE)));
387         LOG.trace("Invoking MDSAL to install  Arp Reply Match Flow for Table {} ", NwConstants.ARP_CHECK_TABLE);
388         mdsalManager.addFlow(tx, arpRepArpCheckTbl);
389     }
390
391     private void createArpPuntAndLearnFlow(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx) {
392         LOG.debug("adding arp punt and learn entry in table {}", NwConstants.ARP_LEARN_TABLE_1);
393
394         List<MatchInfo> matches = new ArrayList<>();
395         List<ActionInfo> actions = new ArrayList<>();
396         Uint64 cookie = Uint64.valueOf("88880000", 16).intern();
397
398         matches.add(MatchEthernetType.ARP);
399         actions.add(new ActionPuntToController());
400         if (arpPuntTimeout != 0) {
401             actions.add(new ActionLearn(0, arpPuntTimeout, ARP_LEARN_FLOW_PRIORITY, cookie, 0,
402                     NwConstants.ARP_LEARN_TABLE_1, 0, 0,
403                     Arrays.asList(
404                             new MatchFromValue(NwConstants.ETHTYPE_ARP, NxmOfFieldType.NXM_OF_ETH_TYPE.getType(),
405                                     NxmOfFieldType.NXM_OF_ETH_TYPE.getFlowModHeaderLenInt()),
406                             new MatchFromField(NxmOfFieldType.NXM_OF_ARP_OP.getType(),
407                                     NxmOfFieldType.NXM_OF_ARP_OP.getType(),
408                                     NxmOfFieldType.NXM_OF_ARP_OP.getFlowModHeaderLenInt()),
409                             new MatchFromField(NxmOfFieldType.NXM_OF_ARP_TPA.getType(),
410                                     NxmOfFieldType.NXM_OF_ARP_TPA.getType(),
411                                     NxmOfFieldType.NXM_OF_ARP_TPA.getFlowModHeaderLenInt()),
412                             new ActionLearn.MatchFromField(NxmOfFieldType.OXM_OF_METADATA.getType(),
413                                     MetaDataUtil.METADATA_ELAN_TAG_OFFSET,
414                                     NxmOfFieldType.OXM_OF_METADATA.getType(), MetaDataUtil.METADATA_ELAN_TAG_OFFSET,
415                                     ElanConstants.ELAN_TAG_LENGTH),
416                             new CopyFromValue(1, NxmOfFieldType.NXM_NX_REG4.getType(), 8))));
417
418             actions.add(new ActionLearn(0, arpPuntTimeout, ARP_LEARN_FLOW_PRIORITY, cookie, 0,
419                     NwConstants.ARP_LEARN_TABLE_2, 0, 0,
420                     Arrays.asList(
421                             new MatchFromValue(NwConstants.ETHTYPE_ARP, NxmOfFieldType.NXM_OF_ETH_TYPE.getType(),
422                                     NxmOfFieldType.NXM_OF_ETH_TYPE.getFlowModHeaderLenInt()),
423                             new MatchFromField(NxmOfFieldType.NXM_OF_ARP_OP.getType(),
424                                     NxmOfFieldType.NXM_OF_ARP_OP.getType(),
425                                     NxmOfFieldType.NXM_OF_ARP_OP.getFlowModHeaderLenInt()),
426                             new MatchFromField(NxmOfFieldType.NXM_OF_ARP_TPA.getType(),
427                                     NxmOfFieldType.NXM_OF_ARP_SPA.getType(),
428                                     NxmOfFieldType.NXM_OF_ARP_TPA.getFlowModHeaderLenInt()),
429                             new ActionLearn.MatchFromField(NxmOfFieldType.OXM_OF_METADATA.getType(),
430                                     MetaDataUtil.METADATA_ELAN_TAG_OFFSET, NxmOfFieldType.OXM_OF_METADATA.getType(),
431                                     MetaDataUtil.METADATA_ELAN_TAG_OFFSET, MetaDataUtil.METADATA_ELAN_TAG_BITLEN),
432                             new CopyFromValue(1, NxmOfFieldType.NXM_NX_REG4.getType(), 8, 8))));
433         }
434
435         List<InstructionInfo> instructions = new ArrayList<>();
436         instructions.add(new InstructionApplyActions(actions));
437         String flowid = String.valueOf(NwConstants.ARP_LEARN_TABLE_1) + NwConstants.FLOWID_SEPARATOR + "arp.punt";
438         FlowEntity flow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ARP_LEARN_TABLE_1, flowid,
439                 NwConstants.TABLE_MISS_PRIORITY, "arp punt/learn flow", 0,
440                 0, cookie, matches, instructions);
441         mdsalManager.addFlow(tx, flow);
442     }
443
444     private void addGarpLearnMatchFlow(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx) {
445         List<ActionInfo> actions = new ArrayList<>();
446         List<MatchInfoBase> matches = new ArrayList<>();
447
448         matches.add(MatchEthernetType.ARP);
449         matches.add(new NxMatchRegister(NxmNxReg4.class, GARP_LEARN_MATCH_VALUE, ARP_LEARN_MATCH_MASK));
450
451         actions.add(new ActionRegLoad(NxmNxReg4.class, 0, 31, 0));
452         actions.add(new ActionPuntToController());
453         actions.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_LEARNED_TABLE));
454         actions.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_TABLE));
455
456         List<InstructionInfo> instructions = new ArrayList<>();
457         instructions.add(new InstructionApplyActions(actions));
458         String flowid = String.valueOf(NwConstants.ELAN_BASE_TABLE) + NwConstants.FLOWID_SEPARATOR + "garp.match";
459         FlowEntity garpFlow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_BASE_TABLE, flowid,
460                 NwConstants.DEFAULT_ARP_FLOW_PRIORITY, "GARP learn match flow", 0, 0,
461                 ElanConstants.COOKIE_ELAN_BASE_SMAC, matches, instructions);
462         mdsalManager.addFlow(tx, garpFlow);
463     }
464
465     private void addArpLearnMatchFlow(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx) {
466         List<ActionInfo> actions = new ArrayList<>();
467         List<MatchInfoBase> matches = new ArrayList<>();
468
469         matches.add(MatchEthernetType.ARP);
470         matches.add(new NxMatchRegister(NxmNxReg4.class, ARP_LEARN_MATCH_VALUE, ARP_LEARN_MATCH_MASK));
471
472         actions.add(new ActionRegLoad(NxmNxReg4.class, 0, 31, 0));
473         actions.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_LEARNED_TABLE));
474         actions.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_TABLE));
475
476         List<InstructionInfo> instructions = new ArrayList<>();
477         instructions.add(new InstructionApplyActions(actions));
478         String flowid = String.valueOf(NwConstants.ELAN_BASE_TABLE) + NwConstants.FLOWID_SEPARATOR + "arp.match";
479         FlowEntity arpFlow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_BASE_TABLE, flowid,
480                 NwConstants.DEFAULT_ARP_FLOW_PRIORITY, "ARP learn match flow", 0, 0,
481                 ElanConstants.COOKIE_ELAN_BASE_SMAC, matches, instructions);
482         mdsalManager.addFlow(tx, arpFlow);
483     }
484
485 }