BUG-9079 Make PCEP session recoverable from exception
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / config / PCEPTopologyProviderBean.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.bgpcep.pcep.topology.provider.config;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Dictionary;
16 import java.util.Hashtable;
17 import java.util.List;
18 import javax.annotation.Nonnull;
19 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.bgpcep.pcep.topology.provider.PCEPTopologyProvider;
21 import org.opendaylight.bgpcep.pcep.topology.provider.TopologySessionListenerFactory;
22 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
26 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
27 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
28 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
29 import org.opendaylight.protocol.pcep.PCEPCapability;
30 import org.opendaylight.protocol.pcep.PCEPDispatcher;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.ServiceRegistration;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public final class PCEPTopologyProviderBean implements PCEPTopologyProviderDependenciesProvider, AutoCloseable {
37     private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProviderBean.class);
38
39     private static final String STATEFUL_NOT_DEFINED = "Stateful capability not defined, aborting PCEP Topology " +
40         "Deployer instantiation";
41     private final PCEPDispatcher pcepDispatcher;
42     private final DataBroker dataBroker;
43     private final TopologySessionListenerFactory sessionListenerFactory;
44     private final RpcProviderRegistry rpcProviderRegistry;
45     private final BundleContext bundleContext;
46     private final ClusterSingletonServiceProvider cssp;
47     @GuardedBy("this")
48     private PCEPTopologyProviderBeanCSS pcepTopoProviderCSS;
49
50     public PCEPTopologyProviderBean(final ClusterSingletonServiceProvider cssp, final BundleContext bundleContext,
51         final DataBroker dataBroker, final PCEPDispatcher pcepDispatcher, final RpcProviderRegistry rpcProviderRegistry,
52         final TopologySessionListenerFactory sessionListenerFactory) {
53         this.cssp = requireNonNull(cssp);
54         this.bundleContext = requireNonNull(bundleContext);
55         this.pcepDispatcher = requireNonNull(pcepDispatcher);
56         this.dataBroker = requireNonNull(dataBroker);
57         this.sessionListenerFactory = requireNonNull(sessionListenerFactory);
58         this.rpcProviderRegistry = requireNonNull(rpcProviderRegistry);
59         final List<PCEPCapability> capabilities = this.pcepDispatcher.getPCEPSessionNegotiatorFactory()
60             .getPCEPSessionProposalFactory().getCapabilities();
61         final boolean statefulCapability = capabilities.stream().anyMatch(PCEPCapability::isStateful);
62         if (!statefulCapability) {
63             throw new IllegalStateException(STATEFUL_NOT_DEFINED);
64         }
65     }
66
67     synchronized ListenableFuture<Void> closeServiceInstance() {
68         if (this.pcepTopoProviderCSS != null) {
69             return this.pcepTopoProviderCSS.closeServiceInstance();
70         }
71         return Futures.immediateFuture(null);
72     }
73
74     @Override
75     public synchronized void close() {
76         if (this.pcepTopoProviderCSS != null) {
77             this.pcepTopoProviderCSS.close();
78             this.pcepTopoProviderCSS = null;
79         }
80     }
81
82     synchronized void start(final PCEPTopologyConfigDependencies configDependencies) {
83         Preconditions.checkState(this.pcepTopoProviderCSS == null,
84             "Previous instance %s was not closed.", this);
85         try {
86             this.pcepTopoProviderCSS = new PCEPTopologyProviderBeanCSS(configDependencies);
87         } catch (final Exception e) {
88             LOG.debug("Failed to create PCEPTopologyProvider {}", configDependencies.getTopologyId().getValue(), e);
89         }
90     }
91
92     @Override
93     public PCEPDispatcher getPCEPDispatcher() {
94         return this.pcepDispatcher;
95     }
96
97     @Override
98     public RpcProviderRegistry getRpcProviderRegistry() {
99         return this.rpcProviderRegistry;
100     }
101
102     @Override
103     public DataBroker getDataBroker() {
104         return this.dataBroker;
105     }
106
107     @Override
108     public TopologySessionListenerFactory getTopologySessionListenerFactory() {
109         return this.sessionListenerFactory;
110     }
111
112     private class PCEPTopologyProviderBeanCSS implements ClusterSingletonService, AutoCloseable {
113         private final ServiceGroupIdentifier sgi;
114         private ServiceRegistration<?> serviceRegistration;
115         private ClusterSingletonServiceRegistration cssRegistration;
116         private final PCEPTopologyProvider pcepTopoProvider;
117         @GuardedBy("this")
118         private boolean serviceInstantiated;
119
120         PCEPTopologyProviderBeanCSS(final PCEPTopologyConfigDependencies configDependencies) {
121                 this.sgi = configDependencies.getSchedulerDependency().getIdentifier();
122                 this.pcepTopoProvider = PCEPTopologyProvider.create(PCEPTopologyProviderBean.this, configDependencies);
123
124                 final Dictionary<String, String> properties = new Hashtable<>();
125                 properties.put(PCEPTopologyProvider.class.getName(), configDependencies.getTopologyId().getValue());
126                 this.serviceRegistration = PCEPTopologyProviderBean.this.bundleContext
127                     .registerService(DefaultTopologyReference.class.getName(), this.pcepTopoProvider, properties);
128             LOG.info("PCEP Topology Provider service {} registered", getIdentifier().getValue());
129             this.cssRegistration = PCEPTopologyProviderBean.this.cssp.registerClusterSingletonService(this);
130         }
131
132         @Override
133         public synchronized void instantiateServiceInstance() {
134             LOG.info("PCEP Topology Provider Singleton Service {} instantiated", getIdentifier().getValue());
135             if (this.pcepTopoProvider != null) {
136                 this.pcepTopoProvider.instantiateServiceInstance();
137                 this.serviceInstantiated = true;
138             }
139         }
140
141         @Override
142         public synchronized ListenableFuture<Void> closeServiceInstance() {
143             LOG.info("Close PCEP Topology Provider Singleton Service {}", getIdentifier().getValue());
144             if (this.pcepTopoProvider != null && this.serviceInstantiated) {
145                 this.serviceInstantiated = false;
146                 return this.pcepTopoProvider.closeServiceInstance();
147             }
148             return Futures.immediateFuture(null);
149         }
150
151         @Nonnull
152         @Override
153         public ServiceGroupIdentifier getIdentifier() {
154             return this.sgi;
155         }
156
157         @Override
158         public synchronized void close() {
159             if (this.cssRegistration != null) {
160                 try {
161                     this.cssRegistration.close();
162                 } catch (final Exception e) {
163                     LOG.debug("Failed to close PCEP Topology Provider service {}", this.sgi.getValue(), e);
164                 }
165                 this.cssRegistration = null;
166             }
167             if (this.serviceRegistration != null) {
168                 this.serviceRegistration.unregister();
169                 this.serviceRegistration = null;
170             }
171         }
172     }
173 }