Bug 6396: Integrate neutron-vpp-mapper with clustering singleton service
[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.md.sal.binding.api.DataBroker;
15 import org.opendaylight.groupbasedpolicy.neutron.vpp.mapper.NeutronVppMapper;
16 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
17 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
19 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class NeutronVppMapperInstance implements ClusterSingletonService, AutoCloseable {
24
25     private static final Logger LOG = LoggerFactory.getLogger(NeutronVppMapperInstance.class);
26
27     private static final ServiceGroupIdentifier IDENTIFIER =
28             ServiceGroupIdentifier.create(NeutronVppMapperInstance.class.getName());
29     private final DataBroker dataBroker;
30     private final String socketPath;
31     private final String socketPrefix;
32     private final ClusterSingletonServiceProvider clusterSingletonService;
33     private ClusterSingletonServiceRegistration singletonServiceRegistration;
34     private NeutronVppMapper mapper;
35
36     public NeutronVppMapperInstance(final DataBroker dataBroker,
37                                     final String socketPath,
38                                     final String socketPrefix,
39                                     final ClusterSingletonServiceProvider clusterSingletonService) {
40         this.dataBroker = Preconditions.checkNotNull(dataBroker);
41         this.socketPath = Preconditions.checkNotNull(socketPath);
42         this.socketPrefix = Preconditions.checkNotNull(socketPrefix);
43         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
44     }
45
46     public void initialize() {
47         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
48         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
49     }
50
51     @Override
52     public void instantiateServiceInstance() {
53         LOG.info("Instantiating {}", this.getClass().getSimpleName());
54         mapper = new NeutronVppMapper(socketPath, socketPrefix, dataBroker);
55     }
56
57     @Override
58     public ListenableFuture<Void> closeServiceInstance() {
59         LOG.info("Instance {} closed", this.getClass().getSimpleName());
60         mapper.close();
61         return Futures.immediateFuture(null);
62     }
63
64     @Override
65     public void close() throws Exception {
66         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
67         if (singletonServiceRegistration != null) {
68             try {
69                 singletonServiceRegistration.close();
70             } catch (Exception e) {
71                 LOG.warn("{} closed unexpectedly.", this.getClass().getSimpleName(), e.getMessage());
72             }
73             singletonServiceRegistration = null;
74         }
75     }
76
77     @Override
78     public ServiceGroupIdentifier getIdentifier() {
79         return IDENTIFIER;
80     }
81 }