Netvirt: Qos Alert patch7 - last 44/56244/5
authorArun Sharma <arun.e.sharma@ericsson.com>
Thu, 27 Apr 2017 18:47:08 +0000 (00:17 +0530)
committerArun Sharma <arun.e.sharma@ericsson.com>
Mon, 1 May 2017 18:09:16 +0000 (23:39 +0530)
*** patch #7 - Added EntityOwnershipListener  ***

Only qosalert entity owner or master only should poll for port stats.
Once a master is elected, it shall continue polling until a new master is elected.

Spec Review Link:
https://git.opendaylight.org/gerrit/50689

Change-Id: I67ad2b3a016204b8e19e2971a0329b52aa8bd4fb
Signed-off-by: Arun Sharma <arun.e.sharma@ericsson.com>
vpnservice/qosservice/impl/src/main/java/org/opendaylight/netvirt/qosservice/QosAlertEosHandler.java [new file with mode: 0644]
vpnservice/qosservice/impl/src/main/java/org/opendaylight/netvirt/qosservice/QosAlertManager.java
vpnservice/qosservice/impl/src/main/java/org/opendaylight/netvirt/qosservice/QosConstants.java
vpnservice/qosservice/impl/src/main/resources/org/opendaylight/blueprint/qosservice.xml

diff --git a/vpnservice/qosservice/impl/src/main/java/org/opendaylight/netvirt/qosservice/QosAlertEosHandler.java b/vpnservice/qosservice/impl/src/main/java/org/opendaylight/netvirt/qosservice/QosAlertEosHandler.java
new file mode 100644 (file)
index 0000000..d518e0f
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.netvirt.qosservice;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
+import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Singleton
+public class QosAlertEosHandler implements EntityOwnershipListener, AutoCloseable {
+
+    private static EntityOwnershipService entityOwnershipService;
+    private static QosAlertManager qosAlertManager;
+    private static EntityOwnershipListenerRegistration listenerRegistration;
+    private static EntityOwnershipCandidateRegistration candidateRegistration;
+    private static final Logger LOG = LoggerFactory.getLogger(QosAlertEosHandler.class);
+
+    @Inject
+    public QosAlertEosHandler(final EntityOwnershipService eos, final QosAlertManager qam) {
+        entityOwnershipService = eos;
+        qosAlertManager = qam;
+    }
+
+    @PostConstruct
+    public void init() {
+        registerQosAlertEosListener();
+    }
+
+    @Override
+    @PreDestroy
+    public void close() {
+        listenerRegistration.close();
+        candidateRegistration.close();
+        LOG.trace("entity ownership unregisterated");
+    }
+
+    private void registerQosAlertEosListener() {
+        listenerRegistration =
+                entityOwnershipService.registerListener(QosConstants.QOS_ALERT_OWNER_ENTITY_TYPE, this);
+        Entity instanceEntity = new Entity(
+                QosConstants.QOS_ALERT_OWNER_ENTITY_TYPE, QosConstants.QOS_ALERT_OWNER_ENTITY_TYPE);
+        try {
+            candidateRegistration = entityOwnershipService.registerCandidate(instanceEntity);
+        } catch (CandidateAlreadyRegisteredException e) {
+            LOG.warn("qosalet instance entity {} was already "
+                    + "registered for ownership", instanceEntity, e);
+        }
+        LOG.trace("entity ownership registeration successful");
+    }
+
+    @Override
+    public void ownershipChanged(EntityOwnershipChange entityOwnershipChange) {
+        LOG.trace("ownershipChanged: {}", entityOwnershipChange);
+
+        if ((entityOwnershipChange.hasOwner() && entityOwnershipChange.isOwner())
+                || (!entityOwnershipChange.hasOwner() && entityOwnershipChange.wasOwner())) {
+            qosAlertManager.setQosAlertOwner(true); // continue polling until new owner is elected
+        } else {
+            qosAlertManager.setQosAlertOwner(false); // no longer an owner
+        }
+    }
+}
index 421039ea4beeb058f15cd61fea9c6ecb602507b5..b34ebbc0783c1ae6aaf9efe32d446e194122a8a9 100644 (file)
@@ -123,6 +123,16 @@ public final class QosAlertManager implements Runnable {
         LOG.info("{} close done", getClass().getSimpleName());
     }
 
+    public void setQosAlertOwner(boolean isOwner) {
+        LOG.trace("qos alert set owner : {}", isOwner);
+        statsPollThreadStart = isOwner;
+        if (thread != null) {
+            thread.interrupt();
+        } else {
+            startStatsPollThread();
+        }
+    }
+
     @Override
     public void run() {
         LOG.info("Qos alert poll thread started");
index 609e8b1ca9e7a8d1d8c93c5e4c0b388a94955d79..76f8a0e3433d21e13eaebef7031636047808ee94 100644 (file)
@@ -16,4 +16,5 @@ public class QosConstants {
     public static final String ORG_OPS4J_PAX_LOGGING = "org.ops4j.pax.logging";
     public static final String FELIX_FILEINSTALL_FILENAME = "felix.fileinstall.filename";
     public static final String SERVICE_PID = "service.pid";
+    public static final String QOS_ALERT_OWNER_ENTITY_TYPE = "netvirt-qosalert-owner-entity";
 }
index 770ec7c74741529cf921c91d1ed2d82d1bceea31..1626fb5f6b833a75ac5d9cfaf7751df82c583f88 100644 (file)
@@ -18,6 +18,9 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
                interface="org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager" />
     <reference id="iNeutronVpnManager"
                interface="org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager" />
+    <reference id="entityOwnershipService"
+               interface="org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService" />
+
     <odl:rpc-service id="odlInterfaceRpcService"
                  interface="org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService" />