2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.qosservice;
11 import javax.annotation.PostConstruct;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
16 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 public class QosAlertEosHandler implements EntityOwnershipListener, AutoCloseable {
28 private static EntityOwnershipService entityOwnershipService;
29 private static QosAlertManager qosAlertManager;
30 private static EntityOwnershipListenerRegistration listenerRegistration;
31 private static EntityOwnershipCandidateRegistration candidateRegistration;
32 private static final Logger LOG = LoggerFactory.getLogger(QosAlertEosHandler.class);
35 public QosAlertEosHandler(final EntityOwnershipService eos, final QosAlertManager qam) {
36 entityOwnershipService = eos;
37 qosAlertManager = qam;
42 registerQosAlertEosListener();
48 listenerRegistration.close();
49 candidateRegistration.close();
50 LOG.trace("entity ownership unregisterated");
53 private void registerQosAlertEosListener() {
54 listenerRegistration =
55 entityOwnershipService.registerListener(QosConstants.QOS_ALERT_OWNER_ENTITY_TYPE, this);
56 Entity instanceEntity = new Entity(
57 QosConstants.QOS_ALERT_OWNER_ENTITY_TYPE, QosConstants.QOS_ALERT_OWNER_ENTITY_TYPE);
59 candidateRegistration = entityOwnershipService.registerCandidate(instanceEntity);
60 } catch (CandidateAlreadyRegisteredException e) {
61 LOG.warn("qosalet instance entity {} was already "
62 + "registered for ownership", instanceEntity, e);
64 LOG.trace("entity ownership registeration successful");
68 public void ownershipChanged(EntityOwnershipChange entityOwnershipChange) {
69 LOG.trace("ownershipChanged: {}", entityOwnershipChange);
71 if ((entityOwnershipChange.hasOwner() && entityOwnershipChange.isOwner())
72 || (!entityOwnershipChange.hasOwner() && entityOwnershipChange.wasOwner())) {
73 qosAlertManager.setQosAlertOwner(true); // continue polling until new owner is elected
75 qosAlertManager.setQosAlertOwner(false); // no longer an owner