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.FluentFuture;
14 import java.util.Collections;
15 import java.util.Dictionary;
16 import java.util.Hashtable;
17 import java.util.concurrent.TimeUnit;
18 import org.checkerframework.checker.lock.qual.GuardedBy;
19 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
20 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
21 import org.opendaylight.mdsal.common.api.CommitInfo;
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.topology.tunnel.pcep.programming.rev181109.TopologyTunnelPcepProgrammingService;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
30 import org.opendaylight.yangtools.concepts.ObjectRegistration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.osgi.framework.Constants;
33 import org.osgi.framework.InvalidSyntaxException;
34 import org.osgi.framework.ServiceRegistration;
35 import org.osgi.util.tracker.ServiceTracker;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public final class PCEPTunnelClusterSingletonService implements ClusterSingletonService, AutoCloseable {
41 private static final Logger LOG = LoggerFactory.getLogger(PCEPTunnelClusterSingletonService.class);
42 private final PCEPTunnelTopologyProvider ttp;
43 private final TunnelProgramming tp;
44 private final ServiceGroupIdentifier sgi;
45 private final TopologyId tunnelTopologyId;
46 private final TunnelProviderDependencies dependencies;
48 private ServiceRegistration<?> serviceRegistration;
50 private ClusterSingletonServiceRegistration pcepTunnelCssReg;
52 private ObjectRegistration<TunnelProgramming> reg;
54 public PCEPTunnelClusterSingletonService(
55 final TunnelProviderDependencies dependencies,
56 final InstanceIdentifier<Topology> pcepTopology,
57 final TopologyId tunnelTopologyId
59 this.dependencies = requireNonNull(dependencies);
60 this.tunnelTopologyId = requireNonNull(tunnelTopologyId);
61 final TopologyId pcepTopologyId = pcepTopology.firstKeyOf(Topology.class).getTopologyId();
63 final InstructionScheduler scheduler;
64 ServiceTracker<InstructionScheduler, ?> tracker = null;
66 tracker = new ServiceTracker<>(dependencies.getBundleContext(),
67 dependencies.getBundleContext().createFilter(String.format("(&(%s=%s)%s)", Constants.OBJECTCLASS,
68 InstructionScheduler.class.getName(), "(" + InstructionScheduler.class.getName()
69 + "=" + pcepTopologyId.getValue() + ")")), null);
71 scheduler = (InstructionScheduler) tracker.waitForService(
72 TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES));
73 Preconditions.checkState(scheduler != null, "InstructionScheduler service not found");
74 } catch (InvalidSyntaxException | InterruptedException e) {
75 throw new IllegalStateException("Error retrieving InstructionScheduler service", e);
77 if (tracker != null) {
82 final InstanceIdentifier<Topology> tunnelTopology = InstanceIdentifier.builder(NetworkTopology.class)
83 .child(Topology.class, new TopologyKey(tunnelTopologyId)).build();
84 this.ttp = new PCEPTunnelTopologyProvider(dependencies.getDataBroker(), pcepTopology, pcepTopologyId,
85 tunnelTopology, tunnelTopologyId);
88 this.sgi = scheduler.getIdentifier();
89 this.tp = new TunnelProgramming(scheduler, dependencies);
92 final Dictionary<String, String> properties = new Hashtable<>();
93 properties.put(PCEPTunnelTopologyProvider.class.getName(), tunnelTopologyId.getValue());
94 this.serviceRegistration = dependencies.getBundleContext()
95 .registerService(DefaultTopologyReference.class.getName(), this.ttp, properties);
97 LOG.info("PCEP Tunnel Cluster Singleton service {} registered", getIdentifier().getValue());
98 this.pcepTunnelCssReg = dependencies.getCssp().registerClusterSingletonService(this);
103 public synchronized void instantiateServiceInstance() {
104 LOG.info("Instantiate PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getValue());
106 final InstanceIdentifier<Topology> topology = InstanceIdentifier
107 .builder(NetworkTopology.class).child(Topology.class, new TopologyKey(this.tunnelTopologyId)).build();
108 this.reg = this.dependencies.getRpcProviderRegistry()
109 .registerRpcImplementation(TopologyTunnelPcepProgrammingService.class,
110 this.tp, Collections.singleton(topology));
115 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
116 LOG.info("Close Service Instance PCEP Tunnel Topology Provider Singleton Service {}",
117 getIdentifier().getValue());
121 return CommitInfo.emptyFluentFuture();
125 public ServiceGroupIdentifier getIdentifier() {
130 @SuppressWarnings("checkstyle:IllegalCatch")
131 public synchronized void close() {
132 LOG.info("Close PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getValue());
134 if (this.pcepTunnelCssReg != null) {
136 this.pcepTunnelCssReg.close();
137 } catch (final Exception e) {
138 LOG.debug("Failed to close PCEP Tunnel Topology service {}", this.sgi.getValue(), e);
140 this.pcepTunnelCssReg = null;
142 if (this.serviceRegistration != null) {
143 this.serviceRegistration.unregister();
144 this.serviceRegistration = null;