7142c3ead0c9191e6ce20d19832feb2e9367e566
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / controller / config / yang / config / groupbasedpolicy / StatisticsManagerImplInstance.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.groupbasedpolicy;
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.api.StatisticsManager;
16 import org.opendaylight.groupbasedpolicy.statistics.StatisticsManagerImpl;
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.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.StatRecords;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class StatisticsManagerImplInstance implements ClusterSingletonService, StatisticsManager, AutoCloseable {
26
27     private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImplInstance.class);
28
29     private static final ServiceGroupIdentifier IDENTIFIER =
30             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
31     private final DataBroker dataBroker;
32     private ClusterSingletonServiceProvider clusterSingletonService;
33     private ClusterSingletonServiceRegistration singletonServiceRegistration;
34     private StatisticsManagerImpl statsManager;
35
36     public StatisticsManagerImplInstance(final DataBroker dataBroker,
37                                          final ClusterSingletonServiceProvider clusterSingletonService) {
38         this.dataBroker = Preconditions.checkNotNull(dataBroker);
39         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
40     }
41
42     @Override
43     public boolean writeStat(StatRecords record) {
44         return statsManager.writeStat(record);
45     }
46
47     @Override
48     public StatRecords readStats() {
49         return statsManager.readStats();
50     }
51
52     public void initialize() {
53         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
54         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
55     }
56
57     @Override
58     public void instantiateServiceInstance() {
59         LOG.info("Instantiating {}", this.getClass().getSimpleName());
60         statsManager = new StatisticsManagerImpl(dataBroker);
61     }
62
63     @Override
64     public ListenableFuture<Void> closeServiceInstance() {
65         LOG.info("Instance {} closed", this.getClass().getSimpleName());
66         statsManager.close();
67         return Futures.immediateFuture(null);
68     }
69
70     @Override
71     public void close() throws Exception {
72         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
73         if (singletonServiceRegistration != null) {
74             try {
75                 singletonServiceRegistration.close();
76             } catch (Exception e) {
77                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
78             }
79             singletonServiceRegistration = null;
80         }
81     }
82
83     @Override
84     public ServiceGroupIdentifier getIdentifier() {
85         return IDENTIFIER;
86     }
87 }