catch and log cluster singleton service registration exceptions
[groupbasedpolicy.git] / renderers / ios-xe / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ios_xe_provider / impl / config / IosXeProviderInstance.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.groupbasedpolicy.renderer.ios_xe_provider.impl.config;
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 org.opendaylight.controller.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
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.renderer.ios_xe_provider.impl.IosXeRendererProviderImpl;
19 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.spi.SxpEpProviderProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
23 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.ip.sgt.distribution.rev160715.IpSgtDistributionService;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class IosXeProviderInstance implements ClusterSingletonService, AutoCloseable {
29
30     private static final Logger LOG = LoggerFactory.getLogger(IosXeProviderInstance.class);
31
32     private static final ServiceGroupIdentifier IDENTIFIER =
33             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
34     private final DataBroker dataBroker;
35     private final BindingAwareBroker bindingAwareBroker;
36     private final ClusterSingletonServiceProvider clusterSingletonService;
37     private final SxpEpProviderProvider sxpEpProvider;
38     private final IpSgtDistributionService ipSgtDistributor;
39     private ClusterSingletonServiceRegistration singletonServiceRegistration;
40     private IosXeRendererProviderImpl renderer;
41
42     public IosXeProviderInstance(final DataBroker dataBroker,
43                                  final BindingAwareBroker broker,
44                                  final ClusterSingletonServiceProvider clusterSingletonService,
45                                  final SxpEpProviderProvider sxpEpProvider,
46                                  final RpcProviderRegistry rpcProviderRegistry) {
47         this.dataBroker = Preconditions.checkNotNull(dataBroker);
48         this.bindingAwareBroker = Preconditions.checkNotNull(broker);
49         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
50         this.sxpEpProvider = Preconditions.checkNotNull(sxpEpProvider);
51         this.ipSgtDistributor = Preconditions.checkNotNull(rpcProviderRegistry.getRpcService(IpSgtDistributionService.class));
52     }
53
54     public void initialize() {
55         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
56         try {
57         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
58         } catch (Exception e) {
59             LOG.warn("Exception thrown while registering cluster singleton service in {}", this.getClass(), e.getMessage());
60         }
61     }
62
63     @Override
64     public void instantiateServiceInstance() {
65         LOG.info("Instantiating {}", this.getClass().getSimpleName());
66         renderer = new IosXeRendererProviderImpl(dataBroker, bindingAwareBroker, sxpEpProvider, ipSgtDistributor);
67     }
68
69     @Override
70     public ListenableFuture<Void> closeServiceInstance() {
71         LOG.info("Instance {} closed", this.getClass().getSimpleName());
72         renderer.close();
73         return Futures.immediateFuture(null);
74     }
75
76     @Override
77     public void close() throws Exception {
78         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
79         if (singletonServiceRegistration != null) {
80             try {
81                 singletonServiceRegistration.close();
82             } catch (Exception e) {
83                 LOG.warn("{} closed unexpectedly. Cause: {}", e.getMessage());
84             }
85             singletonServiceRegistration = null;
86         }
87     }
88
89     @Override
90     public ServiceGroupIdentifier getIdentifier() {
91         return IDENTIFIER;
92     }
93 }