8728298c1317f8cad54b658f451550613292cc26
[mdsal.git] / singleton-service / mdsal-singleton-dom-impl / src / main / java / org / opendaylight / mdsal / singleton / dom / impl / ClusterSingletonServiceRegistrationDelegator.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
9 package org.opendaylight.mdsal.singleton.dom.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
14 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
15 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
16
17 /**
18  * Package protected help class represent a Delegator for {@link ClusterSingletonService}
19  * instance and {@link ClusterSingletonServiceRegistration} implementation.
20  * Close registration means remove {@link ClusterSingletonService} instance from internal
21  * ClusterSingletonServiceGroup list reference.
22  *
23  *<p>
24  * Close {@link ClusterSingletonServiceRegistration} is prepared for a possible restart
25  * service or application in osgi container. Any another services from group can not be
26  * stopped.
27  */
28 class ClusterSingletonServiceRegistrationDelegator
29         implements ClusterSingletonServiceRegistration, ClusterSingletonService {
30
31     private final ClusterSingletonService service;
32     private final ClusterSingletonServiceGroup<?, ?, ?> group;
33
34     ClusterSingletonServiceRegistrationDelegator(final ClusterSingletonService service,
35             final ClusterSingletonServiceGroup<?, ?, ?> group) {
36         this.service = Preconditions.checkNotNull(service);
37         this.group = Preconditions.checkNotNull(group);
38     }
39
40     @Override
41     public void close() throws Exception {
42         group.unregisterService(this);
43     }
44
45     @Override
46     public void instantiateServiceInstance() {
47         service.instantiateServiceInstance();
48     }
49
50     @Override
51     public ListenableFuture<Void> closeServiceInstance() {
52         return service.closeServiceInstance();
53     }
54
55     @Override
56     public ServiceGroupIdentifier getIdentifier() {
57         return service.getIdentifier();
58     }
59
60     public String getServiceGroupIdentifier() {
61         return service.getIdentifier().getValue();
62     }
63 }