1f1f0b74a4d8ec601bc27cebae605c15b29d8289
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / AbstractBGPExtensionProviderActivator.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.parser.spi;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.List;
14 import javax.annotation.concurrent.GuardedBy;
15 import org.opendaylight.yangtools.concepts.Registration;
16
17 public abstract class AbstractBGPExtensionProviderActivator implements AutoCloseable, BGPExtensionProviderActivator {
18     @GuardedBy("this")
19     private List<? extends Registration> registrations;
20
21     @GuardedBy("this")
22     protected abstract List<? extends Registration> startImpl(BGPExtensionProviderContext context);
23
24     @Override
25     public final synchronized void start(final BGPExtensionProviderContext context) {
26         checkState(this.registrations == null);
27         this.registrations = requireNonNull(startImpl(context));
28     }
29
30     @Override
31     public final synchronized void stop() {
32         if (this.registrations == null) {
33             return;
34         }
35
36         this.registrations.forEach(Registration::close);
37         this.registrations = null;
38     }
39
40     @Override
41     public final void close() {
42         stop();
43     }
44 }