Remove sal.core.api.RpcRoutingContext
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / osgi / AbstractBrokerServiceProxy.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.dom.broker.osgi;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.Set;
16
17 import javax.annotation.Nullable;
18
19 import org.opendaylight.controller.sal.core.api.BrokerService;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.osgi.framework.ServiceReference;
22
23 public abstract class AbstractBrokerServiceProxy<T extends BrokerService> implements AutoCloseable, BrokerService {
24
25     private T delegate;
26     private final ServiceReference<T> reference;
27
28     public AbstractBrokerServiceProxy(final @Nullable ServiceReference<T> ref, final T delegate) {
29         this.delegate = checkNotNull(delegate, "Delegate should not be null.");
30         this.reference = ref;
31     }
32
33     protected final T getDelegate() {
34         checkState(delegate != null, "Proxy was closed and unregistered.");
35         return delegate;
36     }
37
38     protected final ServiceReference<T> getReference() {
39         return reference;
40     }
41
42     private final Set<Registration> registrations = Collections.synchronizedSet(new HashSet<Registration>());
43
44     protected <R extends Registration> R addRegistration(final R registration) {
45         if (registration != null) {
46             registrations.add(registration);
47         }
48         return registration;
49     }
50
51     protected void closeBeforeUnregistrations() {
52         // NOOP
53     }
54
55     protected void closeAfterUnregistrations() {
56         // NOOP
57     }
58
59     @Override
60     @SuppressWarnings("checkstyle:IllegalCatch")
61     public void close() {
62         if (delegate != null) {
63             delegate = null;
64             RuntimeException potentialException = new RuntimeException(
65                     "Uncaught exceptions occured during unregistration");
66             boolean hasSuppressed = false;
67             for (Registration registration : registrations) {
68                 try {
69                     registration.close();
70                 } catch (Exception e) {
71                     potentialException.addSuppressed(e);
72                     hasSuppressed = true;
73                 }
74             }
75             if (hasSuppressed) {
76                 throw potentialException;
77             }
78         }
79     }
80 }