Remove use of StringBuffer
[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.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78
79 @Singleton
80 public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, ElanNodeListener> {
81
82     private static final Logger LOG = LoggerFactory.getLogger(ElanNodeListener.class);
83     private static final int LEARN_MATCH_REG4_VALUE = 1;
84     private static final int ARP_LEARN_FLOW_PRIORITY = 10;
85     private static final int ARP_LEARN_MATCH_VALUE = 0x1;
86     private static final int GARP_LEARN_MATCH_VALUE = 0x101;
87     private static final long ARP_LEARN_MATCH_MASK = 0xFFFFL;
88
89     private final DataBroker broker;
90     private final ManagedNewTransactionRunner txRunner;
91     private final IMdsalApiManager mdsalManager;
92     private final IdManagerService idManagerService;
93     private final int tempSmacLearnTimeout;
94     private final int arpPuntTimeout;
95     private final boolean puntLldpToController;
96     private final JobCoordinator jobCoordinator;
97     private final DataTreeEventCallbackRegistrar eventCallbacks;
98
99     @Inject
100     public ElanNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager, ElanConfig elanConfig,
101             IdManagerService idManagerService, JobCoordinator jobCoordinator,
102             DataTreeEventCallbackRegistrar eventCallbacks) {
103         this.broker = dataBroker;
104         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
105         this.mdsalManager = mdsalManager;
106         this.tempSmacLearnTimeout = elanConfig.getTempSmacLearnTimeout();
107         this.arpPuntTimeout = elanConfig.getArpPuntTimeout().intValue();
108         this.puntLldpToController = elanConfig.isPuntLldpToController();
109         this.idManagerService = idManagerService;
110         this.jobCoordinator = jobCoordinator;
111         this.eventCallbacks = eventCallbacks;
112     }
113
114     @Override
115     @PostConstruct
116     public void init() {
117         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
118     }
119
120     @Override
121     protected InstanceIdentifier<Node> getWildCardPath() {
122         return InstanceIdentifier.create(Nodes.class).child(Node.class);
123     }
124
125     @Override
126     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
127     }
128
129     @Override
130     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
131     }
132
133     @Override
134     protected void add(InstanceIdentifier<Node> identifier, Node add) {
135         NodeId nodeId = add.getId();
136         String[] node = nodeId.getValue().split(":");
137         if (node.length < 2) {
138             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
139             return;
140         }
141         addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION, tx -> {
142             BigInteger dpId = new BigInteger(node[1]);
143             createTableMissEntry(tx, dpId);
144             createMulticastFlows(tx, dpId);
145             createArpDefaultFlowsForArpCheckTable(dpId);
146         }), LOG, "Error handling ELAN node addition for {}", add);
147     }
148
149     private void createArpDefaultFlowsForArpCheckTable(BigInteger dpId) {
150
151         jobCoordinator.enqueueJob("ARP_CHECK_TABLE-" + dpId.toString(),
152             () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
153                 try {
154                     LOG.debug("Received notification to install Arp Check Default entries for dpn {} ", dpId);
155                     createArpRequestMatchFlows(dpId, tx);
156                     createArpResponseMatchFlows(dpId, tx);
157                     createArpPuntAndLearnFlow(dpId, tx);
158                     addGarpLearnMatchFlow(dpId, tx);
159                     addArpLearnMatchFlow(dpId, tx);
160                 } catch (InterruptedException | ExecutionException e) {
161                     LOG.error("Error programming ARP rules for dpn {}", dpId, e);
162                 }
163             })));
164     }
165
166     public void createTableMissEntry(TypedWriteTransaction<Configuration> tx, BigInteger dpnId) {
167         setupTableMissSmacFlow(tx, dpnId);
168         setupTableMissDmacFlow(tx, dpnId);
169         setupTableMissArpCheckFlow(tx, dpnId);
170         setupTableMissApResponderFlow(tx, dpnId);
171         setupExternalL2vniTableMissFlow(tx, dpnId);
172     }
173
174     private void createMulticastFlows(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
175         createL2ControlProtocolDropFlows(tx, dpId);
176         createMulticastPuntFlows(tx, dpId);
177     }
178
179     private void createMulticastPuntFlows(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
180         if (puntLldpToController) {
181             createLldpFlows(tx, dpId);
182         }
183     }
184
185     private void createLldpFlows(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
186         createLldpFlow(tx, dpId, ElanConstants.LLDP_DST_1, "LLDP dMac Table Flow 1");
187         createLldpFlow(tx, dpId, ElanConstants.LLDP_DST_2, "LLDP dMac Table Flow 2");
188         createLldpFlow(tx, dpId, ElanConstants.LLDP_DST_3, "LLDP dMac Table Flow 3");
189     }
190
191     private void createLldpFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId, String dstMac,
192             String flowName) {
193         List<MatchInfo> mkMatches = new ArrayList<>();
194         mkMatches.add(new MatchEthernetType(ElanConstants.LLDP_ETH_TYPE));
195         mkMatches.add(new MatchEthernetDestination(new MacAddress(dstMac)));
196
197         List<ActionInfo> listActionInfo = new ArrayList<>();
198         listActionInfo.add(new ActionPuntToController());
199
200         List<InstructionInfo> mkInstructions = new ArrayList<>();
201         mkInstructions.add(new InstructionApplyActions(listActionInfo));
202
203         String flowId = dpId.toString() + NwConstants.ELAN_DMAC_TABLE + "lldp" + ElanConstants.LLDP_ETH_TYPE + dstMac;
204         FlowEntity lldpFlow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 16, flowName, 0, 0,
205                 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
206
207         mdsalManager.addFlow(tx, lldpFlow);
208     }
209
210     private void setupTableMissSmacFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
211         List<ActionInfo> actionsInfos = new ArrayList<>();
212         actionsInfos.add(new ActionPuntToController());
213         actionsInfos.add(new ActionLearn(0, tempSmacLearnTimeout, 0, ElanConstants.COOKIE_ELAN_LEARNED_SMAC, 0,
214                 NwConstants.ELAN_SMAC_LEARNED_TABLE, 0, 0,
215                 Arrays.asList(
216                         new ActionLearn.MatchFromField(NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getType(),
217                                 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getType(),
218                                 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getFlowModHeaderLenInt()),
219                         new ActionLearn.MatchFromField(NwConstants.NxmOfFieldType.NXM_NX_REG1.getType(),
220                                 NwConstants.NxmOfFieldType.NXM_NX_REG1.getType(), ElanConstants.INTERFACE_TAG_LENGTH),
221                         new ActionLearn.CopyFromValue(LEARN_MATCH_REG4_VALUE,
222                                 NwConstants.NxmOfFieldType.NXM_NX_REG4.getType(), 8))));
223
224         List<InstructionInfo> mkInstructions = new ArrayList<>();
225         mkInstructions.add(new InstructionApplyActions(actionsInfos));
226         mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
227
228         List<MatchInfo> mkMatches = new ArrayList<>();
229         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
230                 getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE), 0, "ELAN sMac Table Miss Flow", 0, 0,
231                 ElanConstants.COOKIE_ELAN_KNOWN_SMAC, mkMatches, mkInstructions);
232         mdsalManager.addFlow(tx, flowEntity);
233
234         addSmacBaseTableFlow(tx, dpId);
235         addSmacLearnedTableFlow(tx, dpId);
236     }
237
238     private void addSmacBaseTableFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
239         // T48 - resubmit to T49 & T50
240         List<ActionInfo> actionsInfo = new ArrayList<>();
241         actionsInfo.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_LEARNED_TABLE));
242         actionsInfo.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_TABLE));
243         List<InstructionInfo> mkInstruct = new ArrayList<>();
244         mkInstruct.add(new InstructionApplyActions(actionsInfo));
245         List<MatchInfo> mkMatch = new ArrayList<>();
246         FlowEntity doubleResubmitTable = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_BASE_TABLE,
247                 getTableMissFlowRef(NwConstants.ELAN_BASE_TABLE), 0, "Elan sMac resubmit table", 0, 0,
248                 ElanConstants.COOKIE_ELAN_BASE_SMAC, mkMatch, mkInstruct);
249         mdsalManager.addFlow(tx, doubleResubmitTable);
250     }
251
252     private void addSmacLearnedTableFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
253         // T50 - match on Reg4 and goto T51
254         List<MatchInfoBase> mkMatches = new ArrayList<>();
255         mkMatches.add(new NxMatchRegister(NxmNxReg4.class, LEARN_MATCH_REG4_VALUE));
256         List<InstructionInfo> mkInstructions = new ArrayList<>();
257         mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
258         String flowRef = new StringBuilder().append(NwConstants.ELAN_SMAC_TABLE).append(NwConstants.FLOWID_SEPARATOR)
259                 .append(LEARN_MATCH_REG4_VALUE).toString();
260         FlowEntity flowEntity =
261                 MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE, flowRef, 10, "ELAN sMac Table Reg4 Flow",
262                         0, 0, ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(BigInteger.valueOf(LEARN_MATCH_REG4_VALUE)),
263                         mkMatches, mkInstructions);
264         mdsalManager.addFlow(tx, flowEntity);
265     }
266
267     private void setupTableMissDmacFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
268         List<MatchInfo> mkMatches = new ArrayList<>();
269
270         List<InstructionInfo> mkInstructions = new ArrayList<>();
271         mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_UNKNOWN_DMAC_TABLE));
272
273         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE,
274                 getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE), 0, "ELAN dMac Table Miss Flow", 0, 0,
275                 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
276
277         mdsalManager.addFlow(tx, flowEntity);
278     }
279
280     private void setupExternalL2vniTableMissFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpnId) {
281         List<MatchInfo> matches = new ArrayList<>();
282         List<ActionInfo> actionsInfos = Collections.singletonList(new ActionNxResubmit(NwConstants
283                         .LPORT_DISPATCHER_TABLE));
284         List<InstructionInfo> instructions = Collections.singletonList(new InstructionApplyActions(actionsInfos));
285         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L2VNI_EXTERNAL_TUNNEL_DEMUX_TABLE,
286                         getTableMissFlowRef(NwConstants.L2VNI_EXTERNAL_TUNNEL_DEMUX_TABLE), 0,
287                         "External L2VNI Table Miss Flow", 0, 0,
288                          ElanConstants.COOKIE_L2VNI_DEMUX, matches, instructions);
289         mdsalManager.addFlow(tx, flowEntity);
290     }
291
292
293     private void createL2ControlProtocolDropFlows(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
294         List<MatchInfo> mkMatches = new ArrayList<>();
295         MatchEthernetDestination matchEthDst =
296                 new MatchEthernetDestination(new MacAddress(ElanConstants.L2_CONTROL_PACKETS_DMAC),
297                         new MacAddress(ElanConstants.L2_CONTROL_PACKETS_DMAC_MASK));
298
299         mkMatches.add(matchEthDst);
300
301         List<ActionInfo> listActionInfo = new ArrayList<>();
302         listActionInfo.add(new ActionDrop());
303
304         List<InstructionInfo> mkInstructions = new ArrayList<>();
305         mkInstructions.add(new InstructionApplyActions(listActionInfo));
306
307         String flowId = dpId.toString() + NwConstants.ELAN_DMAC_TABLE + "l2control"
308                 + ElanConstants.L2_CONTROL_PACKETS_DMAC + ElanConstants.L2_CONTROL_PACKETS_DMAC_MASK;
309         FlowEntity flow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 15,
310                 "L2 control packets dMac Table Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches,
311                 mkInstructions);
312
313         mdsalManager.addFlow(tx, flow);
314     }
315
316     private static String getTableMissFlowRef(long tableId) {
317         return String.valueOf(tableId);
318     }
319
320     @Override
321     protected ElanNodeListener getDataTreeChangeListener() {
322         return ElanNodeListener.this;
323     }
324
325     private void setupTableMissApResponderFlow(TypedWriteTransaction<Configuration> tx, final BigInteger dpnId) {
326         mdsalManager.addFlow(tx, ArpResponderUtil.getArpResponderTableMissFlow(dpnId));
327     }
328
329     private void setupTableMissArpCheckFlow(TypedWriteTransaction<Configuration> tx, BigInteger 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(BigInteger 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(BigInteger 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(BigInteger 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(BigInteger 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         BigInteger cookie = new BigInteger("88880000", 16);
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(BigInteger 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(BigInteger 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 }