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.base.Preconditions;
13 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.concurrent.TimeUnit;
16 import org.checkerframework.checker.lock.qual.GuardedBy;
17 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
18 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
22 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
27 import org.opendaylight.yangtools.concepts.Registration;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.Constants;
31 import org.osgi.framework.FrameworkUtil;
32 import org.osgi.framework.InvalidSyntaxException;
33 import org.osgi.framework.ServiceRegistration;
34 import org.osgi.util.tracker.ServiceTracker;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public final class PCEPTunnelClusterSingletonService implements ClusterSingletonService, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(PCEPTunnelClusterSingletonService.class);
41 private final PCEPTunnelTopologyProvider ttp;
42 private final TunnelProgramming tp;
43 private final ServiceGroupIdentifier sgi;
44 private final TopologyId tunnelTopologyId;
45 private final TunnelProviderDependencies dependencies;
47 private ServiceRegistration<?> serviceRegistration;
49 private ClusterSingletonServiceRegistration pcepTunnelCssReg;
51 private Registration reg;
53 public PCEPTunnelClusterSingletonService(
54 final TunnelProviderDependencies dependencies,
55 final InstanceIdentifier<Topology> pcepTopology,
56 final TopologyId tunnelTopologyId
58 this.dependencies = requireNonNull(dependencies);
59 this.tunnelTopologyId = requireNonNull(tunnelTopologyId);
60 final TopologyId pcepTopologyId = pcepTopology.firstKeyOf(Topology.class).getTopologyId();
61 final BundleContext bundleContext = dependencies.getBundleContext();
63 final InstructionScheduler scheduler;
64 ServiceTracker<InstructionScheduler, ?> tracker = null;
66 tracker = new ServiceTracker<>(bundleContext,
67 bundleContext.createFilter("(&(%s=%s)%s)".formatted(
68 Constants.OBJECTCLASS, InstructionScheduler.class.getName(),
69 "(" + InstructionScheduler.class.getName() + "=" + pcepTopologyId.getValue() + ")")),
72 scheduler = (InstructionScheduler) tracker.waitForService(
73 TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES));
74 Preconditions.checkState(scheduler != null, "InstructionScheduler service not found");
75 } catch (InvalidSyntaxException | InterruptedException e) {
76 throw new IllegalStateException("Error retrieving InstructionScheduler service", e);
78 if (tracker != null) {
83 final InstanceIdentifier<Topology> tunnelTopology = InstanceIdentifier.builder(NetworkTopology.class)
84 .child(Topology.class, new TopologyKey(tunnelTopologyId)).build();
85 ttp = new PCEPTunnelTopologyProvider(dependencies.getDataBroker(), pcepTopology, pcepTopologyId,
86 tunnelTopology, tunnelTopologyId);
89 sgi = scheduler.getIdentifier();
90 tp = new TunnelProgramming(scheduler, dependencies);
93 serviceRegistration = bundleContext.registerService(DefaultTopologyReference.class, ttp,
94 FrameworkUtil.asDictionary(Map.of(
95 PCEPTunnelTopologyProvider.class.getName(), tunnelTopologyId.getValue())));
97 LOG.info("PCEP Tunnel Cluster Singleton service {} registered", getIdentifier().getName());
98 pcepTunnelCssReg = dependencies.getCssp().registerClusterSingletonService(this);
102 public synchronized void instantiateServiceInstance() {
103 LOG.info("Instantiate PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getName());
105 reg = tp.register(InstanceIdentifier.builder(NetworkTopology.class)
106 .child(Topology.class, new TopologyKey(tunnelTopologyId))
112 public synchronized ListenableFuture<? extends CommitInfo> closeServiceInstance() {
113 LOG.info("Close Service Instance PCEP Tunnel Topology Provider Singleton Service {}",
114 getIdentifier().getName());
118 return CommitInfo.emptyFluentFuture();
122 public ServiceGroupIdentifier getIdentifier() {
127 @SuppressWarnings("checkstyle:IllegalCatch")
128 public synchronized void close() {
129 LOG.info("Close PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getName());
131 if (pcepTunnelCssReg != null) {
133 pcepTunnelCssReg.close();
134 } catch (final Exception e) {
135 LOG.debug("Failed to close PCEP Tunnel Topology service {}", sgi.getName(), e);
137 pcepTunnelCssReg = null;
139 if (serviceRegistration != null) {
140 serviceRegistration.unregister();
141 serviceRegistration = null;