2 * Copyright (c) 2017 AT&T Intellectual Property. 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.tunnel.provider;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Dictionary;
15 import java.util.Hashtable;
16 import javax.annotation.Nonnull;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
19 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
20 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
24 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.TopologyTunnelPcepProgrammingService;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.osgi.framework.ServiceRegistration;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public final class PCEPTunnelClusterSingletonService implements ClusterSingletonService, AutoCloseable {
38 private static final Logger LOG = LoggerFactory.getLogger(PCEPTunnelClusterSingletonService.class);
39 private final PCEPTunnelTopologyProvider ttp;
40 private final TunnelProgramming tp;
41 private final ServiceGroupIdentifier sgi;
42 private final TopologyId tunnelTopologyId;
43 private final TunnelProviderDependencies dependencies;
45 private ServiceRegistration<?> serviceRegistration;
47 private ClusterSingletonServiceRegistration pcepTunnelCssReg;
49 private BindingAwareBroker.RoutedRpcRegistration<TopologyTunnelPcepProgrammingService> reg;
51 public PCEPTunnelClusterSingletonService(
52 final TunnelProviderDependencies dependencies,
53 final InstanceIdentifier<Topology> pcepTopology,
54 final TopologyId tunnelTopologyId
56 this.dependencies = requireNonNull(dependencies);
57 this.tunnelTopologyId = requireNonNull(tunnelTopologyId);
58 final TopologyId pcepTopologyId = pcepTopology.firstKeyOf(Topology.class).getTopologyId();
60 final WaitingServiceTracker<InstructionScheduler> schedulerTracker =
61 WaitingServiceTracker.create(InstructionScheduler.class,
62 dependencies.getBundleContext(), "(" + InstructionScheduler.class.getName()
63 + "=" + pcepTopologyId.getValue() + ")");
64 final InstructionScheduler scheduler = schedulerTracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
65 schedulerTracker.close();
67 final InstanceIdentifier<Topology> tunnelTopology = InstanceIdentifier.builder(NetworkTopology.class)
68 .child(Topology.class, new TopologyKey(tunnelTopologyId)).build();
69 this.ttp = new PCEPTunnelTopologyProvider(dependencies.getDataBroker(), pcepTopology, pcepTopologyId,
70 tunnelTopology, tunnelTopologyId);
73 this.sgi = scheduler.getIdentifier();
74 this.tp = new TunnelProgramming(scheduler, dependencies);
77 final Dictionary<String, String> properties = new Hashtable<>();
78 properties.put(PCEPTunnelTopologyProvider.class.getName(), tunnelTopologyId.getValue());
79 this.serviceRegistration = dependencies.getBundleContext()
80 .registerService(DefaultTopologyReference.class.getName(), this.ttp, properties);
82 LOG.info("PCEP Tunnel Cluster Singleton service {} registered", getIdentifier().getValue());
83 this.pcepTunnelCssReg = dependencies.getCssp().registerClusterSingletonService(this);
88 public synchronized void instantiateServiceInstance() {
89 LOG.info("Instantiate PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getValue());
90 this.reg = this.dependencies.getRpcProviderRegistry()
91 .addRoutedRpcImplementation(TopologyTunnelPcepProgrammingService.class, this.tp);
93 final InstanceIdentifier<Topology> topology = InstanceIdentifier
94 .builder(NetworkTopology.class).child(Topology.class, new TopologyKey(this.tunnelTopologyId)).build();
95 this.reg.registerPath(NetworkTopologyContext.class, topology);
100 public synchronized ListenableFuture<Void> closeServiceInstance() {
101 LOG.info("Close Service Instance PCEP Tunnel Topology Provider Singleton Service {}",
102 getIdentifier().getValue());
106 return Futures.immediateFuture(null);
111 public ServiceGroupIdentifier getIdentifier() {
116 @SuppressWarnings("checkstyle:IllegalCatch")
117 public synchronized void close() {
118 LOG.info("Close PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getValue());
120 if (this.pcepTunnelCssReg != null) {
122 this.pcepTunnelCssReg.close();
123 } catch (final Exception e) {
124 LOG.debug("Failed to close PCEP Tunnel Topology service {}", this.sgi.getValue(), e);
126 this.pcepTunnelCssReg = null;
128 if (this.serviceRegistration != null) {
129 this.serviceRegistration.unregister();
130 this.serviceRegistration = null;