catch and log cluster singleton service registration exceptions
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / controller / config / yang / config / groupbasedpolicy / EpRendererAugmentationRegistryImplInstance.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.controller.config.yang.config.groupbasedpolicy;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.concurrent.Future;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
17 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
18 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentation;
19 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry;
20 import org.opendaylight.groupbasedpolicy.endpoint.EndpointRpcRegistry;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
24 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.SetEndpointGroupConditionsInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnsetEndpointGroupConditionsInput;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class EpRendererAugmentationRegistryImplInstance implements ClusterSingletonService, EpRendererAugmentationRegistry, EndpointService, AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(EpRendererAugmentationRegistryImplInstance.class);
38
39     private static final ServiceGroupIdentifier IDENTIFIER =
40             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
41     private final DataBroker dataBroker;
42     private ClusterSingletonServiceProvider clusterSingletonService;
43     private final RpcProviderRegistry rpcProviderRegistry;
44     private ClusterSingletonServiceRegistration singletonServiceRegistration;
45     private EndpointRpcRegistry endpointRpcRegistry;
46     private BindingAwareBroker.RpcRegistration<EndpointService> serviceRpcRegistration;
47
48     public EpRendererAugmentationRegistryImplInstance(final DataBroker dataBroker,
49                                                       final ClusterSingletonServiceProvider clusterSingletonService,
50                                                       final RpcProviderRegistry rpcProviderRegistry) {
51         this.dataBroker = Preconditions.checkNotNull(dataBroker);
52         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
53         this.rpcProviderRegistry = rpcProviderRegistry;
54     }
55
56     @Override
57     public void register(EpRendererAugmentation epRendererAugmentation) {
58         endpointRpcRegistry.register(epRendererAugmentation);
59     }
60
61     @Override
62     public void unregister(EpRendererAugmentation epRendererAugmentation) {
63         endpointRpcRegistry.unregister(epRendererAugmentation);
64     }
65
66     @Override
67     public Future<RpcResult<Void>> unsetEndpointGroupConditions(UnsetEndpointGroupConditionsInput input) {
68         return endpointRpcRegistry.unsetEndpointGroupConditions(input);
69     }
70
71     @Override
72     public Future<RpcResult<Void>> registerEndpoint(RegisterEndpointInput input) {
73         return endpointRpcRegistry.registerEndpoint(input);
74     }
75
76     @Override
77     public Future<RpcResult<Void>> setEndpointGroupConditions(SetEndpointGroupConditionsInput input) {
78         return endpointRpcRegistry.setEndpointGroupConditions(input);
79     }
80
81     @Override
82     public Future<RpcResult<Void>> registerL3PrefixEndpoint(RegisterL3PrefixEndpointInput input) {
83         return endpointRpcRegistry.registerL3PrefixEndpoint(input);
84     }
85
86     @Override
87     public Future<RpcResult<Void>> unregisterEndpoint(UnregisterEndpointInput input) {
88         return endpointRpcRegistry.unregisterEndpoint(input);
89     }
90
91     public void initialize() {
92         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
93         try {
94             singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
95         }
96             catch (Exception e) {
97             LOG.warn("Exception while registering candidate ... ", e);
98         }
99     }
100
101     @Override
102     public void instantiateServiceInstance() {
103         LOG.info("Instantiating {}", this.getClass().getSimpleName());
104         endpointRpcRegistry = new EndpointRpcRegistry(dataBroker);
105
106         serviceRpcRegistration = rpcProviderRegistry.addRpcImplementation(EndpointService.class, this);
107     }
108
109     @Override
110     public ListenableFuture<Void> closeServiceInstance() {
111         LOG.info("Instance {} closed", this.getClass().getSimpleName());
112         endpointRpcRegistry.close();
113         serviceRpcRegistration.close();
114         return Futures.immediateFuture(null);
115     }
116
117     @Override
118     public void close() {
119         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
120         if (singletonServiceRegistration != null) {
121             try {
122                 singletonServiceRegistration.close();
123             } catch (Exception e) {
124                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
125             }
126             singletonServiceRegistration = null;
127         }
128     }
129
130     @Override
131     public ServiceGroupIdentifier getIdentifier() {
132         return IDENTIFIER;
133     }
134 }