Bug 6396: Integrate vpp-renderer with clustering singleton service
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / controller / config / yang / config / vpp_provider / impl / GbpVppProviderInstance.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.vpp_provider.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.controller.sal.binding.api.BindingAwareBroker;
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 GbpVppProviderInstance implements ClusterSingletonService, AutoCloseable {
24
25     private static final Logger LOG = LoggerFactory.getLogger(GbpVppProviderInstance.class);
26
27     private static final ServiceGroupIdentifier IDENTIFIER =
28             ServiceGroupIdentifier.create(GbpVppProviderInstance.class.getName());
29     private final DataBroker dataBroker;
30     private final BindingAwareBroker bindingAwareBroker;
31     private final ClusterSingletonServiceProvider clusterSingletonService;
32     private ClusterSingletonServiceRegistration singletonServiceRegistration;
33     private VppRenderer renderer;
34
35     public GbpVppProviderInstance(final DataBroker dataBroker,
36                                   final BindingAwareBroker bindingAwareBroker,
37                                   final ClusterSingletonServiceProvider clusterSingletonService) {
38         this.dataBroker = Preconditions.checkNotNull(dataBroker);
39         this.bindingAwareBroker = Preconditions.checkNotNull(bindingAwareBroker);
40         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
41     }
42
43     public void initialize() {
44         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
45         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
46     }
47
48     @Override
49     public void instantiateServiceInstance() {
50         LOG.info("Instantiating {}", this.getClass().getSimpleName());
51         renderer = new VppRenderer(dataBroker, bindingAwareBroker);
52     }
53
54     @Override
55     public ListenableFuture<Void> closeServiceInstance() {
56         LOG.info("Instance {} closed", this.getClass().getSimpleName());
57         try {
58             renderer.close();
59         } catch (Exception e) {
60             LOG.warn("Exception while closing ... {}", e.getMessage());
61         }
62         return Futures.immediateFuture(null);
63     }
64
65     @Override
66     public void close() throws Exception {
67
68         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
69         if (singletonServiceRegistration != null) {
70             try {
71                 singletonServiceRegistration.close();
72             } catch (Exception e) {
73                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e.getMessage());
74             }
75             singletonServiceRegistration = null;
76         }
77     }
78
79     @Override
80     public ServiceGroupIdentifier getIdentifier() {
81         return IDENTIFIER;
82     }
83 }