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