Merge "BUG-2182 : included data-change-counter in feature file"
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / AbstractRIBExtensionProviderActivator.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.bgp.rib.spi;
9
10 import com.google.common.base.Preconditions;
11
12 import java.util.List;
13
14 import javax.annotation.concurrent.GuardedBy;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public abstract class AbstractRIBExtensionProviderActivator implements AutoCloseable, RIBExtensionProviderActivator {
20     private static final Logger LOG = LoggerFactory.getLogger(AbstractRIBExtensionProviderActivator.class);
21
22     @GuardedBy("this")
23     private List<AutoCloseable> registrations;
24
25     @GuardedBy("this")
26     protected abstract List<AutoCloseable> startRIBExtensionProviderImpl(RIBExtensionProviderContext context);
27
28     @Override
29     public final synchronized void startRIBExtensionProvider(final RIBExtensionProviderContext context) {
30         Preconditions.checkState(this.registrations == null);
31
32         this.registrations = Preconditions.checkNotNull(startRIBExtensionProviderImpl(context));
33     }
34
35     @Override
36     public final synchronized void stopRIBExtensionProvider() {
37         Preconditions.checkState(this.registrations != null);
38
39         for (final AutoCloseable r : this.registrations) {
40             try {
41                 r.close();
42             } catch (final Exception e) {
43                 LOG.warn("Failed to close registration", e);
44             }
45         }
46
47         this.registrations = null;
48     }
49
50     @Override
51     public final void close() {
52         stopRIBExtensionProvider();
53     }
54 }