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 static java.util.Objects.requireNonNull;
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;
36 public final class PCEPTopologyProviderBean implements PCEPTopologyProviderDependenciesProvider, AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProviderBean.class);
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;
48 private PCEPTopologyProviderBeanCSS pcepTopoProviderCSS;
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);
67 synchronized ListenableFuture<Void> closeServiceInstance() {
68 if (this.pcepTopoProviderCSS != null) {
69 return this.pcepTopoProviderCSS.closeServiceInstance();
71 return Futures.immediateFuture(null);
75 public synchronized void close() {
76 if (this.pcepTopoProviderCSS != null) {
77 this.pcepTopoProviderCSS.close();
78 this.pcepTopoProviderCSS = null;
82 synchronized void start(final PCEPTopologyConfigDependencies configDependencies) {
83 Preconditions.checkState(this.pcepTopoProviderCSS == null,
84 "Previous instance %s was not closed.", this);
86 this.pcepTopoProviderCSS = new PCEPTopologyProviderBeanCSS(configDependencies);
87 } catch (final Exception e) {
88 LOG.debug("Failed to create PCEPTopologyProvider {}", configDependencies.getTopologyId().getValue(), e);
93 public PCEPDispatcher getPCEPDispatcher() {
94 return this.pcepDispatcher;
98 public RpcProviderRegistry getRpcProviderRegistry() {
99 return this.rpcProviderRegistry;
103 public DataBroker getDataBroker() {
104 return this.dataBroker;
108 public TopologySessionListenerFactory getTopologySessionListenerFactory() {
109 return this.sessionListenerFactory;
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;
118 private boolean serviceInstantiated;
120 PCEPTopologyProviderBeanCSS(final PCEPTopologyConfigDependencies configDependencies) {
121 this.sgi = configDependencies.getSchedulerDependency().getIdentifier();
122 this.pcepTopoProvider = PCEPTopologyProvider.create(PCEPTopologyProviderBean.this, configDependencies);
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);
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;
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();
148 return Futures.immediateFuture(null);
153 public ServiceGroupIdentifier getIdentifier() {
158 public synchronized void close() {
159 if (this.cssRegistration != null) {
161 this.cssRegistration.close();
162 } catch (final Exception e) {
163 LOG.debug("Failed to close PCEP Topology Provider service {}", this.sgi.getValue(), e);
165 this.cssRegistration = null;
167 if (this.serviceRegistration != null) {
168 this.serviceRegistration.unregister();
169 this.serviceRegistration = null;