2 * Copyright (c) 2016 Cisco Systems, Inc. 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
8 package org.opendaylight.lispflowmapping.clustering;
10 import static org.opendaylight.lispflowmapping.clustering.util.ClusteringUtil.LISPFLOWMAPPING_ENTITY_NAME;
11 import static org.opendaylight.lispflowmapping.clustering.util.ClusteringUtil.LISPFLOWMAPPING_ENTITY_TYPE;
13 import com.google.common.base.Optional;
14 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
15 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
20 import org.opendaylight.lispflowmapping.clustering.api.ClusterNodeModuleSwitcher;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Class responsible for turning on|off module in node cluster.
27 public class ClusterNodeModulSwitcherImpl implements EntityOwnershipListener {
29 private static final Logger LOG = LoggerFactory.getLogger(ClusterNodeModulSwitcherImpl.class);
30 private final EntityOwnershipService entityOwnershipService;
31 private final Entity entity;
32 private boolean moduleIsRunning = true;
33 private ClusterNodeModuleSwitcher module;
35 public ClusterNodeModulSwitcherImpl(final EntityOwnershipService entityOwnershipService) {
36 this.entityOwnershipService = entityOwnershipService;
37 this.entityOwnershipService.registerListener(LISPFLOWMAPPING_ENTITY_NAME, this);
38 entity = new Entity(LISPFLOWMAPPING_ENTITY_NAME, LISPFLOWMAPPING_ENTITY_TYPE);
40 this.entityOwnershipService.registerCandidate(entity);
41 } catch (CandidateAlreadyRegisteredException e) {
42 LOG.debug("Candidate already registered. Trace: {}", e);
47 public void ownershipChanged(final EntityOwnershipChange entityOwnershipChange) {
48 LOG.debug("Entity ownership change message received.");
49 switchModuleState(entityOwnershipChange.isOwner());
52 public void switchModuleByEntityOwnership() {
53 switchModuleState(isMaster());
56 private void switchModuleState(final boolean isOwner) {
58 if (!isOwner && moduleIsRunning) {
60 moduleIsRunning = false;
61 LOG.debug("Module {} was stopped.", module.getClass().getName());
62 } else if (isOwner && !moduleIsRunning) {
64 moduleIsRunning = true;
65 LOG.debug("Module {} was restarted.", module.getClass().getName());
68 LOG.debug("Module wasn't initialized yet.");
72 public boolean isMaster() {
73 final Optional<EntityOwnershipState> ownershipState = entityOwnershipService.getOwnershipState(entity);
74 if (ownershipState.isPresent()) {
75 return ownershipState.get().isOwner();
77 LOG.debug("Ownership state information wasn't present in entity ownership service.");
82 public void setModule(final ClusterNodeModuleSwitcher module) {