Revert "Clustering - Importing cluster bundle to implementation bundle."
[lispflowmapping.git] / mappingservice / clustering / src / main / java / org / opendaylight / lispflowmapping / clustering / ClusterNodeModulSwitcherImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.lispflowmapping.clustering;
9
10 import static org.opendaylight.lispflowmapping.clustering.util.ClusteringUtil.LISPFLOWMAPPING_ENTITY_NAME;
11 import static org.opendaylight.lispflowmapping.clustering.util.ClusteringUtil.LISPFLOWMAPPING_ENTITY_TYPE;
12
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;
23
24 /**
25  * Class responsible for turning on|off module in node cluster.
26  */
27 public class ClusterNodeModulSwitcherImpl implements EntityOwnershipListener  {
28
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;
34
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);
39         try {
40             this.entityOwnershipService.registerCandidate(entity);
41         } catch (CandidateAlreadyRegisteredException e) {
42             LOG.debug("Candidate already registered. Trace: {}", e);
43         }
44     }
45
46     @Override
47     public void ownershipChanged(final EntityOwnershipChange entityOwnershipChange) {
48         LOG.debug("Entity ownership change message received.");
49         switchModuleState(entityOwnershipChange.isOwner());
50     }
51
52     public void switchModuleByEntityOwnership() {
53         switchModuleState(isMaster());
54     }
55
56     private void switchModuleState(final boolean isOwner) {
57         if (module != null) {
58             if (!isOwner && moduleIsRunning) {
59                 module.stopModule();
60                 moduleIsRunning = false;
61                 LOG.debug("Module {} was stopped.", module.getClass().getName());
62             } else if (isOwner && !moduleIsRunning) {
63                 module.startModule();
64                 moduleIsRunning = true;
65                 LOG.debug("Module {} was restarted.", module.getClass().getName());
66             }
67         } else {
68             LOG.debug("Module wasn't initialized yet.");
69         }
70     }
71
72     public boolean isMaster() {
73         final Optional<EntityOwnershipState> ownershipState = entityOwnershipService.getOwnershipState(entity);
74         if (ownershipState.isPresent()) {
75             return ownershipState.get().isOwner();
76         } else {
77             LOG.debug("Ownership state information wasn't present in entity ownership service.");
78         }
79         return false;
80     }
81
82     public void setModule(final ClusterNodeModuleSwitcher module) {
83         this.module = module;
84     }
85 }