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