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