catch and log cluster singleton service registration exceptions
[groupbasedpolicy.git] / neutron-vpp-mapper / src / main / java / org / opendaylight / controller / config / yang / config / neutron_vpp_mapper / impl / NeutronVppMapperInstance.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.controller.config.yang.config.neutron_vpp_mapper.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.groupbasedpolicy.neutron.vpp.mapper.NeutronVppMapper;
17 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
20 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class NeutronVppMapperInstance implements ClusterSingletonService, AutoCloseable {
25
26     private static final Logger LOG = LoggerFactory.getLogger(NeutronVppMapperInstance.class);
27
28     private static final ServiceGroupIdentifier IDENTIFIER =
29             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
30     private final DataBroker dataBroker;
31     private final String socketPath;
32     private final String socketPrefix;
33     private final String routingNode;
34     private final ClusterSingletonServiceProvider clusterSingletonService;
35     private ClusterSingletonServiceRegistration singletonServiceRegistration;
36     private NeutronVppMapper mapper;
37
38     public NeutronVppMapperInstance(final DataBroker dataBroker,
39                                     final String socketPath,
40                                     final String socketPrefix,
41                                     final String routingNode,
42                                     final ClusterSingletonServiceProvider clusterSingletonService) {
43         this.dataBroker = Preconditions.checkNotNull(dataBroker);
44         this.socketPath = Preconditions.checkNotNull(socketPath);
45         this.socketPrefix = Preconditions.checkNotNull(socketPrefix);
46         this.routingNode = Preconditions.checkNotNull(routingNode);
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         mapper = new NeutronVppMapper(socketPath, socketPrefix, routingNode, dataBroker);
63     }
64
65     @Override
66     public ListenableFuture<Void> closeServiceInstance() {
67         LOG.info("Instance {} closed", this.getClass().getSimpleName());
68         mapper.close();
69         return Futures.immediateFuture(null);
70     }
71
72     @Override
73     public void close() throws Exception {
74         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
75         if (singletonServiceRegistration != null) {
76             try {
77                 singletonServiceRegistration.close();
78             } catch (Exception e) {
79                 LOG.warn("{} closed unexpectedly.", this.getClass().getSimpleName(), e.getMessage());
80             }
81             singletonServiceRegistration = null;
82         }
83     }
84
85     @Override
86     public ServiceGroupIdentifier getIdentifier() {
87         return IDENTIFIER;
88     }
89 }