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;
45 private PCEPTopologyProviderBeanCSS pcepTopoProviderCSS;
47 public PCEPTopologyProviderBean(final ClusterSingletonServiceProvider cssp, final BundleContext bundleContext,
48 final DataBroker dataBroker, final PCEPDispatcher pcepDispatcher, final RpcProviderRegistry rpcProviderRegistry,
49 final TopologySessionListenerFactory sessionListenerFactory) {
50 this.cssp = Preconditions.checkNotNull(cssp);
51 this.bundleContext = Preconditions.checkNotNull(bundleContext);
52 this.pcepDispatcher = Preconditions.checkNotNull(pcepDispatcher);
53 this.dataBroker = Preconditions.checkNotNull(dataBroker);
54 this.sessionListenerFactory = Preconditions.checkNotNull(sessionListenerFactory);
55 this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
56 final List<PCEPCapability> capabilities = this.pcepDispatcher.getPCEPSessionNegotiatorFactory()
57 .getPCEPSessionProposalFactory().getCapabilities();
58 final boolean statefulCapability = capabilities.stream().anyMatch(PCEPCapability::isStateful);
59 if (!statefulCapability) {
60 throw new IllegalStateException(STATEFUL_NOT_DEFINED);
66 if (this.pcepTopoProviderCSS != null) {
67 this.pcepTopoProviderCSS.close();
71 public void start(final PCEPTopologyConfigDependencies configDependencies) {
72 Preconditions.checkState(this.pcepTopoProviderCSS == null,
73 "Previous instance %s was not closed.", this);
75 this.pcepTopoProviderCSS = new PCEPTopologyProviderBeanCSS(configDependencies);
76 } catch (final Exception e) {
77 LOG.debug("Failed to create PCEPTopologyProvider {}", configDependencies.getTopologyId().getValue(), e);
82 public PCEPDispatcher getPCEPDispatcher() {
83 return this.pcepDispatcher;
87 public RpcProviderRegistry getRpcProviderRegistry() {
88 return this.rpcProviderRegistry;
92 public DataBroker getDataBroker() {
93 return this.dataBroker;
97 public TopologySessionListenerFactory getTopologySessionListenerFactory() {
98 return this.sessionListenerFactory;
101 private class PCEPTopologyProviderBeanCSS implements ClusterSingletonService, AutoCloseable {
102 private final ServiceGroupIdentifier sgi;
103 private ServiceRegistration<?> serviceRegistration;
104 private ClusterSingletonServiceRegistration cssRegistration;
105 private final PCEPTopologyProvider pcepTopoProvider;
107 private boolean serviceInstantiated;
109 PCEPTopologyProviderBeanCSS(final PCEPTopologyConfigDependencies configDependencies) throws Exception {
110 this.sgi = configDependencies.getSchedulerDependency().getIdentifier();
111 this.pcepTopoProvider = PCEPTopologyProvider.create(PCEPTopologyProviderBean.this, configDependencies);
113 final Dictionary<String, String> properties = new Hashtable<>();
114 properties.put(PCEPTopologyProvider.class.getName(), configDependencies.getTopologyId().getValue());
115 this.serviceRegistration = PCEPTopologyProviderBean.this.bundleContext
116 .registerService(DefaultTopologyReference.class.getName(), this.pcepTopoProvider, properties);
117 LOG.info("PCEP Topology Provider service {} registered", getIdentifier().getValue());
118 this.cssRegistration = PCEPTopologyProviderBean.this.cssp.registerClusterSingletonService(this);
122 public synchronized void instantiateServiceInstance() {
123 LOG.info("Topology Provider Singleton Service {} instantiated", getIdentifier().getValue());
124 if (this.pcepTopoProvider != null) {
125 this.pcepTopoProvider.instantiateServiceInstance();
126 this.serviceInstantiated = true;
131 public synchronized ListenableFuture<Void> closeServiceInstance() {
132 LOG.info("Close Topology Provider Singleton Service {}", getIdentifier().getValue());
133 if (this.pcepTopoProvider != null && this.serviceInstantiated) {
134 this.serviceInstantiated = false;
135 return this.pcepTopoProvider.closeServiceInstance();
137 return Futures.immediateFuture(null);
142 public ServiceGroupIdentifier getIdentifier() {
147 public void close() {
148 if (this.cssRegistration != null) {
150 this.cssRegistration.close();
151 } catch (final Exception e) {
152 LOG.debug("Failed to close PCEP Topology Provider service {}", this.sgi.getValue(), e);
154 this.cssRegistration = null;
156 if (this.serviceRegistration != null) {
157 this.serviceRegistration.unregister();
158 this.serviceRegistration = null;