catch and log cluster singleton service registration exceptions
[groupbasedpolicy.git] / neutron-ovsdb / src / main / java / org / opendaylight / controller / config / yang / config / neutron_ovsdb / impl / NeutronOvsdbInstance.java
1 /*
2  * Copyright (c) 2015 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.controller.config.yang.config.neutron_ovsdb.impl;
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.RpcProviderRegistry;
17 import org.opendaylight.groupbasedpolicy.neutron.ovsdb.NeutronOvsdb;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.ovsdb.params.rev160812.IntegrationBridgeSetting;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class NeutronOvsdbInstance implements ClusterSingletonService, AutoCloseable {
28
29     private static final Logger LOG = LoggerFactory.getLogger(NeutronOvsdbInstance.class);
30
31     private static final ServiceGroupIdentifier IDENTIFIER =
32             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
33     private final DataBroker dataBroker;
34     private final IntegrationBridgeSetting settings;
35     private final ClusterSingletonServiceProvider clusterSingletonService;
36     private final RpcProviderRegistry rpcBroker;
37     private ClusterSingletonServiceRegistration singletonServiceRegistration;
38     private NeutronOvsdb neutronOvsdb;
39
40     public NeutronOvsdbInstance(final DataBroker dataBroker,
41                                 final RpcProviderRegistry rpcBroker,
42                                 final IntegrationBridgeSetting settings,
43                                 final ClusterSingletonServiceProvider clusterSingletonService) {
44         this.dataBroker = Preconditions.checkNotNull(dataBroker);
45         this.rpcBroker = Preconditions.checkNotNull(rpcBroker);
46         this.settings = Preconditions.checkNotNull(settings);
47         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
48     }
49
50     public void initialize() {
51         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
52         try {
53             singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
54         } catch (Exception e) {
55             LOG.warn("Exception thrown while registering cluster singleton service in {}", this.getClass(), e.getMessage());
56         }
57     }
58
59     @Override
60     public void instantiateServiceInstance() {
61         LOG.info("Instantiating {}", this.getClass().getSimpleName());
62         final EndpointService epService = rpcBroker.getRpcService(EndpointService.class);
63         neutronOvsdb = new NeutronOvsdb(dataBroker, epService, settings);
64     }
65
66     @Override
67     public ListenableFuture<Void> closeServiceInstance() {
68         LOG.info("Instance {} closed", this.getClass().getSimpleName());
69         try {
70             neutronOvsdb.close();
71         } catch (Exception e) {
72             LOG.warn("Instance close failed to ... ", e);
73         }
74         return Futures.immediateFuture(null);
75     }
76
77     @Override
78     public void close() throws Exception {
79         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
80         if (singletonServiceRegistration != null) {
81             try {
82                 singletonServiceRegistration.close();
83             } catch (Exception e) {
84                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
85             }
86             singletonServiceRegistration = null;
87         }
88     }
89
90     @Override
91     public ServiceGroupIdentifier getIdentifier() {
92         return IDENTIFIER;
93     }
94 }