ce19b2f40324285b0d6fdda3ad69cf05e5964052
[mdsal.git] / singleton-service / mdsal-singleton-dom-impl / src / main / java / org / opendaylight / mdsal / singleton / dom / impl / ServiceGroup.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.mdsal.singleton.dom.impl;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.eclipse.jdt.annotation.Nullable;
12 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
13 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange;
14 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
15 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
16 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
17 import org.opendaylight.yangtools.concepts.Identifiable;
18
19 /**
20  * {@link ServiceGroup} maintains a group of {@link ClusterSingletonService} instances.
21  * All EntityOwnershipChange notifications have to applied to all registered services at the same time in the same
22  * manner. All registered services have only one instantiated service instance in a cluster at one time on same
23  * Cluster Node. This is realized via a double candidate approach where a service group instance maintains a candidate
24  * registration for ownership of the service entity in the cluster and also a registration that acts as a guard to
25  * ensure a service group instance has fully closed prior to relinquishing service ownership. To achieve ownership
26  * of the service group, a service group candidate must hold ownership of both these entities.
27  */
28 abstract sealed class ServiceGroup implements Identifiable<ServiceGroupIdentifier>
29         permits ActiveServiceGroup, PlaceholderServiceGroup {
30     /**
31      * This method must be called once on startup to initialize this group and register the relevant group entity
32      * candidate. It means create relevant Group Entity Candidate Registration.
33      */
34     abstract void initialize() throws CandidateAlreadyRegisteredException;
35
36     /**
37      * This method registers a service instance for this service group. If the local node has ownership of the service
38      * group, the {@link ClusterSingletonService#instantiateServiceInstance()} method is called. Otherwise, the method
39      * is called once the local node gains ownership.
40      *
41      * @param service instance
42      */
43     abstract void registerService(ServiceRegistration reg);
44
45     /**
46      * Method provides possibility to restart some service from group without change leadership for whole group.
47      * {@link ServiceRegistration#removeRegistration()} implementation has to call this service.
48      *
49      * <p>
50      * Candidates are signed for group, so unregistration for group with one service has to trigger new election only
51      * otherwise we can see same behavior as on server without clustering.
52      *
53      * @param service instance
54      * @return Future which completes when this instance is shutdown if this was the last registration, null otherwise
55      */
56     abstract @Nullable ListenableFuture<?> unregisterService(ServiceRegistration reg);
57
58     /**
59      * Method implementation has to apply ownershipChange for all registered services.
60      *
61      * @param entity the entity whose ownership status changed
62      * @param change the change the entity underwent
63      * @param inJeopardy {@code true} if ownership is in jeopardy and the reported change may be inaccurate
64      */
65     abstract void ownershipChanged(DOMEntity entity, EntityOwnershipStateChange change, boolean inJeopardy);
66
67     /**
68      * Closes this service group. All registered service providers are also closed. Please be careful
69      * and use this method without active EOS listener only.
70      *
71      * @return {@link ListenableFuture} in list for all Future from closing {@link ClusterSingletonService}
72      */
73     abstract ListenableFuture<?> closeClusterSingletonGroup();
74 }
75