2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.bgpcep.pcep.topology.provider.config;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Dictionary;
14 import java.util.Hashtable;
15 import java.util.List;
16 import javax.annotation.Nonnull;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.bgpcep.pcep.topology.provider.PCEPTopologyProvider;
19 import org.opendaylight.bgpcep.pcep.topology.provider.TopologySessionListenerFactory;
20 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
26 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
27 import org.opendaylight.protocol.pcep.PCEPCapability;
28 import org.opendaylight.protocol.pcep.PCEPDispatcher;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceRegistration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public final class PCEPTopologyProviderBean implements PCEPTopologyProviderDependenciesProvider, AutoCloseable {
35 private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProviderBean.class);
37 private static final String STATEFUL_NOT_DEFINED = "Stateful capability not defined, aborting PCEP Topology " +
38 "Deployer instantiation";
39 private final PCEPDispatcher pcepDispatcher;
40 private final DataBroker dataBroker;
41 private final TopologySessionListenerFactory sessionListenerFactory;
42 private final RpcProviderRegistry rpcProviderRegistry;
43 private final BundleContext bundleContext;
44 private final ClusterSingletonServiceProvider cssp;
46 private PCEPTopologyProviderBeanCSS pcepTopoProviderCSS;
48 public PCEPTopologyProviderBean(final ClusterSingletonServiceProvider cssp, final BundleContext bundleContext,
49 final DataBroker dataBroker, final PCEPDispatcher pcepDispatcher, final RpcProviderRegistry rpcProviderRegistry,
50 final TopologySessionListenerFactory sessionListenerFactory) {
51 this.cssp = Preconditions.checkNotNull(cssp);
52 this.bundleContext = Preconditions.checkNotNull(bundleContext);
53 this.pcepDispatcher = Preconditions.checkNotNull(pcepDispatcher);
54 this.dataBroker = Preconditions.checkNotNull(dataBroker);
55 this.sessionListenerFactory = Preconditions.checkNotNull(sessionListenerFactory);
56 this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
57 final List<PCEPCapability> capabilities = this.pcepDispatcher.getPCEPSessionNegotiatorFactory()
58 .getPCEPSessionProposalFactory().getCapabilities();
59 final boolean statefulCapability = capabilities.stream().anyMatch(PCEPCapability::isStateful);
60 if (!statefulCapability) {
61 throw new IllegalStateException(STATEFUL_NOT_DEFINED);
65 synchronized ListenableFuture<Void> closeServiceInstance() {
66 if (this.pcepTopoProviderCSS != null) {
67 return this.pcepTopoProviderCSS.closeServiceInstance();
69 return Futures.immediateFuture(null);
73 public synchronized void close() {
74 if (this.pcepTopoProviderCSS != null) {
75 this.pcepTopoProviderCSS.close();
76 this.pcepTopoProviderCSS = null;
80 synchronized void start(final PCEPTopologyConfigDependencies configDependencies) {
81 Preconditions.checkState(this.pcepTopoProviderCSS == null,
82 "Previous instance %s was not closed.", this);
84 this.pcepTopoProviderCSS = new PCEPTopologyProviderBeanCSS(configDependencies);
85 } catch (final Exception e) {
86 LOG.debug("Failed to create PCEPTopologyProvider {}", configDependencies.getTopologyId().getValue(), e);
91 public PCEPDispatcher getPCEPDispatcher() {
92 return this.pcepDispatcher;
96 public RpcProviderRegistry getRpcProviderRegistry() {
97 return this.rpcProviderRegistry;
101 public DataBroker getDataBroker() {
102 return this.dataBroker;
106 public TopologySessionListenerFactory getTopologySessionListenerFactory() {
107 return this.sessionListenerFactory;
110 private class PCEPTopologyProviderBeanCSS implements ClusterSingletonService, AutoCloseable {
111 private final ServiceGroupIdentifier sgi;
112 private ServiceRegistration<?> serviceRegistration;
113 private ClusterSingletonServiceRegistration cssRegistration;
114 private final PCEPTopologyProvider pcepTopoProvider;
116 private boolean serviceInstantiated;
118 PCEPTopologyProviderBeanCSS(final PCEPTopologyConfigDependencies configDependencies) {
119 this.sgi = configDependencies.getSchedulerDependency().getIdentifier();
120 this.pcepTopoProvider = PCEPTopologyProvider.create(PCEPTopologyProviderBean.this, configDependencies);
122 final Dictionary<String, String> properties = new Hashtable<>();
123 properties.put(PCEPTopologyProvider.class.getName(), configDependencies.getTopologyId().getValue());
124 this.serviceRegistration = PCEPTopologyProviderBean.this.bundleContext
125 .registerService(DefaultTopologyReference.class.getName(), this.pcepTopoProvider, properties);
126 LOG.info("PCEP Topology Provider service {} registered", getIdentifier().getValue());
127 this.cssRegistration = PCEPTopologyProviderBean.this.cssp.registerClusterSingletonService(this);
131 public synchronized void instantiateServiceInstance() {
132 LOG.info("PCEP Topology Provider Singleton Service {} instantiated", getIdentifier().getValue());
133 if (this.pcepTopoProvider != null) {
134 this.pcepTopoProvider.instantiateServiceInstance();
135 this.serviceInstantiated = true;
140 public synchronized ListenableFuture<Void> closeServiceInstance() {
141 LOG.info("Close PCEP Topology Provider Singleton Service {}", getIdentifier().getValue());
142 if (this.pcepTopoProvider != null && this.serviceInstantiated) {
143 this.serviceInstantiated = false;
144 return this.pcepTopoProvider.closeServiceInstance();
146 return Futures.immediateFuture(null);
151 public ServiceGroupIdentifier getIdentifier() {
156 public synchronized void close() {
157 if (this.cssRegistration != null) {
159 this.cssRegistration.close();
160 } catch (final Exception e) {
161 LOG.debug("Failed to close PCEP Topology Provider service {}", this.sgi.getValue(), e);
163 this.cssRegistration = null;
165 if (this.serviceRegistration != null) {
166 this.serviceRegistration.unregister();
167 this.serviceRegistration = null;