BUG-6647 Increase code coverage and clean up III
[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 import java.util.List;
12 import javax.annotation.concurrent.GuardedBy;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public abstract class AbstractRIBExtensionProviderActivator implements AutoCloseable, RIBExtensionProviderActivator {
17     private static final Logger LOG = LoggerFactory.getLogger(AbstractRIBExtensionProviderActivator.class);
18
19     @GuardedBy("this")
20     private List<AutoCloseable> registrations;
21
22     @GuardedBy("this")
23     protected abstract List<AutoCloseable> startRIBExtensionProviderImpl(RIBExtensionProviderContext context);
24
25     @Override
26     public final synchronized void startRIBExtensionProvider(final RIBExtensionProviderContext context) {
27         Preconditions.checkState(this.registrations == null);
28
29         this.registrations = Preconditions.checkNotNull(startRIBExtensionProviderImpl(context));
30     }
31
32     @Override
33     public final synchronized void stopRIBExtensionProvider() {
34         if (this.registrations == null) {
35             return;
36         }
37
38         for (final AutoCloseable r : this.registrations) {
39             try {
40                 r.close();
41             } catch (final Exception e) {
42                 LOG.warn("Failed to close registration", e);
43             }
44         }
45
46         this.registrations = null;
47     }
48
49     @Override
50     public final void close() {
51         stopRIBExtensionProvider();
52     }
53 }