catch and log cluster singleton service registration exceptions
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / controller / config / yang / config / groupbasedpolicy / DomainSpecificRegistryInstance.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.DomainSpecificRegistry;
19 import org.opendaylight.groupbasedpolicy.api.EndpointAugmentorRegistry;
20 import org.opendaylight.groupbasedpolicy.api.NetworkDomainAugmentorRegistry;
21 import org.opendaylight.groupbasedpolicy.base_endpoint.BaseEndpointServiceImpl;
22 import org.opendaylight.groupbasedpolicy.base_endpoint.EndpointAugmentorRegistryImpl;
23 import org.opendaylight.groupbasedpolicy.forwarding.NetworkDomainAugmentorRegistryImpl;
24 import org.opendaylight.groupbasedpolicy.renderer.RendererManager;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
26 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
27 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
28 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInput;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class DomainSpecificRegistryInstance implements ClusterSingletonService, DomainSpecificRegistry, BaseEndpointService, AutoCloseable {
37
38     private static final Logger LOG = LoggerFactory.getLogger(DomainSpecificRegistryInstance.class);
39
40     private static final ServiceGroupIdentifier IDENTIFIER =
41             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
42     private final DataBroker dataBroker;
43     private ClusterSingletonServiceProvider clusterSingletonService;
44     private final RpcProviderRegistry rpcProviderRegistry;
45     private ClusterSingletonServiceRegistration singletonServiceRegistration;
46     private EndpointAugmentorRegistryImpl endpointAugmentorRegistryImpl;
47     private NetworkDomainAugmentorRegistryImpl netDomainAugmentorRegistryImpl;
48     private BaseEndpointServiceImpl baseEndpointServiceImpl;
49     private RendererManager rendererManager;
50     private BindingAwareBroker.RpcRegistration<BaseEndpointService> baseEndpointServiceRpcRegistration;
51
52     public DomainSpecificRegistryInstance(final DataBroker dataBroker,
53                                           final ClusterSingletonServiceProvider clusterSingletonService,
54                                           final RpcProviderRegistry rpcProviderRegistry) {
55         this.dataBroker = Preconditions.checkNotNull(dataBroker);
56         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
57         this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
58     }
59
60     @Override
61     public EndpointAugmentorRegistry getEndpointAugmentorRegistry() {
62         return endpointAugmentorRegistryImpl;
63     }
64
65     @Override
66     public NetworkDomainAugmentorRegistry getNetworkDomainAugmentorRegistry() {
67         return netDomainAugmentorRegistryImpl;
68     }
69
70     @Override
71     public Future<RpcResult<Void>> unregisterEndpoint(UnregisterEndpointInput input) {
72         return baseEndpointServiceImpl.unregisterEndpoint(input);
73     }
74
75     @Override
76     public Future<RpcResult<Void>> registerEndpoint(RegisterEndpointInput input) {
77         return baseEndpointServiceImpl.registerEndpoint(input);
78     }
79
80     public void initialize() {
81         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
82         try {
83             singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
84         }
85         catch (Exception e) {
86             LOG.warn("Exception while registering candidate ... ", e);
87         }
88     }
89
90     @Override
91     public void instantiateServiceInstance() {
92         LOG.info("Instantiating {}", this.getClass().getSimpleName());
93         endpointAugmentorRegistryImpl = new EndpointAugmentorRegistryImpl();
94         netDomainAugmentorRegistryImpl = new NetworkDomainAugmentorRegistryImpl();
95         baseEndpointServiceImpl = new BaseEndpointServiceImpl(dataBroker, endpointAugmentorRegistryImpl);
96         rendererManager = new RendererManager(dataBroker, netDomainAugmentorRegistryImpl, endpointAugmentorRegistryImpl);
97
98         baseEndpointServiceRpcRegistration = rpcProviderRegistry.addRpcImplementation(BaseEndpointService.class, this);
99     }
100
101     @Override
102     public ListenableFuture<Void> closeServiceInstance() {
103         LOG.info("Instance {} closed", this.getClass().getSimpleName());
104         baseEndpointServiceImpl.close();
105         baseEndpointServiceRpcRegistration.close();
106         rendererManager.close();
107
108         return Futures.immediateFuture(null);
109     }
110
111     @Override
112     public void close() throws Exception {
113         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
114         if (singletonServiceRegistration != null) {
115             try {
116                 singletonServiceRegistration.close();
117             } catch (Exception e) {
118                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
119             }
120             singletonServiceRegistration = null;
121         }
122     }
123
124     @Override
125     public ServiceGroupIdentifier getIdentifier() {
126         return IDENTIFIER;
127     }
128 }