NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanNodeListener.java
index cd80a65db4d275d3017731750af32090459f6416..de14506d7bef476f820273866054aa7e70e04f75 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.netvirt.elan.internal;
 
 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+import static org.opendaylight.infrautils.utils.concurrent.LoggingFutures.addErrorLogging;
 
 import java.math.BigInteger;
 import java.time.Duration;
@@ -16,14 +17,9 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
-
-import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
 import org.opendaylight.genius.infra.Datastore;
 import org.opendaylight.genius.infra.Datastore.Configuration;
@@ -59,11 +55,15 @@ import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchRegister;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
-import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderConstant;
 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
 import org.opendaylight.netvirt.elan.utils.ElanConstants;
 import org.opendaylight.netvirt.elan.utils.ElanUtils;
+import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
@@ -74,11 +74,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, ElanNodeListener> {
+public class ElanNodeListener extends AbstractAsyncDataTreeChangeListener<Node> {
 
     private static final Logger LOG = LoggerFactory.getLogger(ElanNodeListener.class);
     private static final int LEARN_MATCH_REG4_VALUE = 1;
@@ -101,10 +102,12 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
     public ElanNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager, ElanConfig elanConfig,
             IdManagerService idManagerService, JobCoordinator jobCoordinator,
             DataTreeEventCallbackRegistrar eventCallbacks) {
+        super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class).child(Node.class),
+                Executors.newListeningSingleThreadExecutor("ElanNodeListener", LOG));
         this.broker = dataBroker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.mdsalManager = mdsalManager;
-        this.tempSmacLearnTimeout = elanConfig.getTempSmacLearnTimeout();
+        this.tempSmacLearnTimeout = elanConfig.getTempSmacLearnTimeout().toJava();
         this.arpPuntTimeout = elanConfig.getArpPuntTimeout().intValue();
         this.puntLldpToController = elanConfig.isPuntLldpToController();
         this.idManagerService = idManagerService;
@@ -112,43 +115,42 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         this.eventCallbacks = eventCallbacks;
     }
 
-    @Override
-    @PostConstruct
     public void init() {
-        registerListener(LogicalDatastoreType.OPERATIONAL, broker);
+        LOG.info("{} registered", getClass().getSimpleName());
     }
 
     @Override
-    protected InstanceIdentifier<Node> getWildCardPath() {
-        return InstanceIdentifier.create(Nodes.class).child(Node.class);
+    @PreDestroy
+    public void close() {
+        super.close();
+        Executors.shutdownAndAwaitTermination(getExecutorService());
     }
 
     @Override
-    protected void remove(InstanceIdentifier<Node> identifier, Node del) {
+    public void remove(InstanceIdentifier<Node> identifier, Node del) {
     }
 
     @Override
-    protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
+    public void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
     }
 
     @Override
-    protected void add(InstanceIdentifier<Node> identifier, Node add) {
+    public void add(InstanceIdentifier<Node> identifier, Node add) {
         NodeId nodeId = add.getId();
         String[] node = nodeId.getValue().split(":");
         if (node.length < 2) {
             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
             return;
         }
-        ListenableFutures.addErrorLogging(
-            txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION, tx -> {
-                BigInteger dpId = new BigInteger(node[1]);
-                createTableMissEntry(tx, dpId);
-                createMulticastFlows(tx, dpId);
-                createArpDefaultFlowsForArpCheckTable(dpId);
-            }), LOG, "Error handling ELAN node addition for {}", add);
+        addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION, tx -> {
+            Uint64 dpId = Uint64.valueOf(node[1]);
+            createTableMissEntry(tx, dpId);
+            createMulticastFlows(tx, dpId);
+            createArpDefaultFlowsForArpCheckTable(dpId);
+        }), LOG, "Error handling ELAN node addition for {}", add);
     }
 
-    private void createArpDefaultFlowsForArpCheckTable(BigInteger dpId) {
+    private void createArpDefaultFlowsForArpCheckTable(Uint64 dpId) {
 
         jobCoordinator.enqueueJob("ARP_CHECK_TABLE-" + dpId.toString(),
             () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
@@ -165,7 +167,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
             })));
     }
 
-    public void createTableMissEntry(TypedWriteTransaction<Configuration> tx, BigInteger dpnId) {
+    public void createTableMissEntry(TypedWriteTransaction<Configuration> tx, Uint64 dpnId) {
         setupTableMissSmacFlow(tx, dpnId);
         setupTableMissDmacFlow(tx, dpnId);
         setupTableMissArpCheckFlow(tx, dpnId);
@@ -173,24 +175,24 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         setupExternalL2vniTableMissFlow(tx, dpnId);
     }
 
-    private void createMulticastFlows(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
+    private void createMulticastFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         createL2ControlProtocolDropFlows(tx, dpId);
         createMulticastPuntFlows(tx, dpId);
     }
 
-    private void createMulticastPuntFlows(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
+    private void createMulticastPuntFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         if (puntLldpToController) {
             createLldpFlows(tx, dpId);
         }
     }
 
-    private void createLldpFlows(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
+    private void createLldpFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         createLldpFlow(tx, dpId, ElanConstants.LLDP_DST_1, "LLDP dMac Table Flow 1");
         createLldpFlow(tx, dpId, ElanConstants.LLDP_DST_2, "LLDP dMac Table Flow 2");
         createLldpFlow(tx, dpId, ElanConstants.LLDP_DST_3, "LLDP dMac Table Flow 3");
     }
 
-    private void createLldpFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId, String dstMac,
+    private void createLldpFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId, String dstMac,
             String flowName) {
         List<MatchInfo> mkMatches = new ArrayList<>();
         mkMatches.add(new MatchEthernetType(ElanConstants.LLDP_ETH_TYPE));
@@ -209,7 +211,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         mdsalManager.addFlow(tx, lldpFlow);
     }
 
-    private void setupTableMissSmacFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
+    private void setupTableMissSmacFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         List<ActionInfo> actionsInfos = new ArrayList<>();
         actionsInfos.add(new ActionPuntToController());
         actionsInfos.add(new ActionLearn(0, tempSmacLearnTimeout, 0, ElanConstants.COOKIE_ELAN_LEARNED_SMAC, 0,
@@ -237,7 +239,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         addSmacLearnedTableFlow(tx, dpId);
     }
 
-    private void addSmacBaseTableFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
+    private void addSmacBaseTableFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         // T48 - resubmit to T49 & T50
         List<ActionInfo> actionsInfo = new ArrayList<>();
         actionsInfo.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_LEARNED_TABLE));
@@ -251,22 +253,23 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         mdsalManager.addFlow(tx, doubleResubmitTable);
     }
 
-    private void addSmacLearnedTableFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
+    private void addSmacLearnedTableFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         // T50 - match on Reg4 and goto T51
         List<MatchInfoBase> mkMatches = new ArrayList<>();
         mkMatches.add(new NxMatchRegister(NxmNxReg4.class, LEARN_MATCH_REG4_VALUE));
         List<InstructionInfo> mkInstructions = new ArrayList<>();
         mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
-        String flowRef = new StringBuffer().append(NwConstants.ELAN_SMAC_TABLE).append(NwConstants.FLOWID_SEPARATOR)
+        String flowRef = new StringBuilder().append(NwConstants.ELAN_SMAC_TABLE).append(NwConstants.FLOWID_SEPARATOR)
                 .append(LEARN_MATCH_REG4_VALUE).toString();
         FlowEntity flowEntity =
                 MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE, flowRef, 10, "ELAN sMac Table Reg4 Flow",
-                        0, 0, ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(BigInteger.valueOf(LEARN_MATCH_REG4_VALUE)),
+                        0, 0, Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_SMAC.toJava()
+                            .add(BigInteger.valueOf(LEARN_MATCH_REG4_VALUE))),
                         mkMatches, mkInstructions);
         mdsalManager.addFlow(tx, flowEntity);
     }
 
-    private void setupTableMissDmacFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
+    private void setupTableMissDmacFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         List<MatchInfo> mkMatches = new ArrayList<>();
 
         List<InstructionInfo> mkInstructions = new ArrayList<>();
@@ -279,7 +282,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         mdsalManager.addFlow(tx, flowEntity);
     }
 
-    private void setupExternalL2vniTableMissFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpnId) {
+    private void setupExternalL2vniTableMissFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpnId) {
         List<MatchInfo> matches = new ArrayList<>();
         List<ActionInfo> actionsInfos = Collections.singletonList(new ActionNxResubmit(NwConstants
                         .LPORT_DISPATCHER_TABLE));
@@ -292,7 +295,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
     }
 
 
-    private void createL2ControlProtocolDropFlows(TypedWriteTransaction<Configuration> tx, BigInteger dpId) {
+    private void createL2ControlProtocolDropFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         List<MatchInfo> mkMatches = new ArrayList<>();
         MatchEthernetDestination matchEthDst =
                 new MatchEthernetDestination(new MacAddress(ElanConstants.L2_CONTROL_PACKETS_DMAC),
@@ -315,20 +318,15 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         mdsalManager.addFlow(tx, flow);
     }
 
-    private String getTableMissFlowRef(long tableId) {
+    private static String getTableMissFlowRef(long tableId) {
         return String.valueOf(tableId);
     }
 
-    @Override
-    protected ElanNodeListener getDataTreeChangeListener() {
-        return ElanNodeListener.this;
-    }
-
-    private void setupTableMissApResponderFlow(TypedWriteTransaction<Configuration> tx, final BigInteger dpnId) {
+    private void setupTableMissApResponderFlow(TypedWriteTransaction<Configuration> tx, final Uint64 dpnId) {
         mdsalManager.addFlow(tx, ArpResponderUtil.getArpResponderTableMissFlow(dpnId));
     }
 
-    private void setupTableMissArpCheckFlow(TypedWriteTransaction<Configuration> tx, BigInteger dpnId) {
+    private void setupTableMissArpCheckFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpnId) {
         mdsalManager.addFlow(tx,
                 MDSALUtil.buildFlowEntity(dpnId, NwConstants.ARP_CHECK_TABLE,
                         String.valueOf("L2.ELAN." + NwConstants.ARP_CHECK_TABLE), NwConstants.TABLE_MISS_PRIORITY,
@@ -337,7 +335,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
                         Collections.singletonList(new InstructionGotoTable(NwConstants.ELAN_BASE_TABLE))));
     }
 
-    private void createArpRequestMatchFlows(BigInteger dpId, TypedReadWriteTransaction<Configuration> tx)
+    private void createArpRequestMatchFlows(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx)
             throws ExecutionException, InterruptedException {
         long arpRequestGroupId = ArpResponderUtil.retrieveStandardArpResponderGroupId(idManagerService);
         List<BucketInfo> buckets = ArpResponderUtil.getDefaultBucketInfos(NwConstants.ARP_RESPONDER_TABLE);
@@ -355,7 +353,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         eventCallbacks.onAddOrUpdate(LogicalDatastoreType.CONFIGURATION,
                 ElanUtils.getGroupInstanceid(dpId, arpRequestGroupId), (unused, newGroupId) -> {
                 LOG.info("group {} added in the config", arpRequestGroupId);
-                ListenableFutures.addErrorLogging(
+                LoggingFutures.addErrorLogging(
                         txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
                             innerConfTx -> createArpRequestMatchFlowsForGroup(dpId, arpRequestGroupId,
                                     innerConfTx)),
@@ -366,7 +364,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
 
     }
 
-    private void createArpRequestMatchFlowsForGroup(BigInteger dpId, long arpRequestGroupId,
+    private void createArpRequestMatchFlowsForGroup(Uint64 dpId, long arpRequestGroupId,
             TypedReadWriteTransaction<Configuration> tx) {
         FlowEntity arpReqArpCheckTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.ARP_CHECK_TABLE,
                 NwConstants.ARP_REQUEST, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REQUEST), () ->
@@ -378,7 +376,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         mdsalManager.addFlow(tx, arpReqArpCheckTbl);
     }
 
-    private void createArpResponseMatchFlows(BigInteger dpId, TypedReadWriteTransaction<Configuration> tx) {
+    private void createArpResponseMatchFlows(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx) {
         FlowEntity arpRepArpCheckTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.ARP_CHECK_TABLE,
                 NwConstants.ARP_REPLY, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REPLY), () ->
                         Arrays.asList(new ActionNxResubmit(NwConstants.ARP_LEARN_TABLE_1),
@@ -388,12 +386,12 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         mdsalManager.addFlow(tx, arpRepArpCheckTbl);
     }
 
-    private void createArpPuntAndLearnFlow(BigInteger dpId, TypedReadWriteTransaction<Configuration> tx) {
+    private void createArpPuntAndLearnFlow(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx) {
         LOG.debug("adding arp punt and learn entry in table {}", NwConstants.ARP_LEARN_TABLE_1);
 
         List<MatchInfo> matches = new ArrayList<>();
         List<ActionInfo> actions = new ArrayList<>();
-        BigInteger cookie = new BigInteger("88880000", 16);
+        Uint64 cookie = Uint64.valueOf("88880000", 16).intern();
 
         matches.add(MatchEthernetType.ARP);
         actions.add(new ActionPuntToController());
@@ -441,7 +439,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         mdsalManager.addFlow(tx, flow);
     }
 
-    private void addGarpLearnMatchFlow(BigInteger dpId, TypedReadWriteTransaction<Configuration> tx) {
+    private void addGarpLearnMatchFlow(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx) {
         List<ActionInfo> actions = new ArrayList<>();
         List<MatchInfoBase> matches = new ArrayList<>();
 
@@ -462,7 +460,7 @@ public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, Elan
         mdsalManager.addFlow(tx, garpFlow);
     }
 
-    private void addArpLearnMatchFlow(BigInteger dpId, TypedReadWriteTransaction<Configuration> tx) {
+    private void addArpLearnMatchFlow(Uint64 dpId, TypedReadWriteTransaction<Configuration> tx) {
         List<ActionInfo> actions = new ArrayList<>();
         List<MatchInfoBase> matches = new ArrayList<>();