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.Dictionary;
15 import java.util.Hashtable;
16 import java.util.concurrent.TimeUnit;
17 import javax.annotation.Nonnull;
18 import javax.annotation.concurrent.GuardedBy;
19 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
20 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
22 import org.opendaylight.mdsal.common.api.CommitInfo;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
25 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyContext;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.TopologyTunnelPcepProgrammingService;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.osgi.framework.Constants;
34 import org.osgi.framework.InvalidSyntaxException;
35 import org.osgi.framework.ServiceRegistration;
36 import org.osgi.util.tracker.ServiceTracker;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public final class PCEPTunnelClusterSingletonService implements ClusterSingletonService, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(PCEPTunnelClusterSingletonService.class);
43 private final PCEPTunnelTopologyProvider ttp;
44 private final TunnelProgramming tp;
45 private final ServiceGroupIdentifier sgi;
46 private final TopologyId tunnelTopologyId;
47 private final TunnelProviderDependencies dependencies;
49 private ServiceRegistration<?> serviceRegistration;
51 private ClusterSingletonServiceRegistration pcepTunnelCssReg;
53 private BindingAwareBroker.RoutedRpcRegistration<TopologyTunnelPcepProgrammingService> reg;
55 public PCEPTunnelClusterSingletonService(
56 final TunnelProviderDependencies dependencies,
57 final InstanceIdentifier<Topology> pcepTopology,
58 final TopologyId tunnelTopologyId
60 this.dependencies = requireNonNull(dependencies);
61 this.tunnelTopologyId = requireNonNull(tunnelTopologyId);
62 final TopologyId pcepTopologyId = pcepTopology.firstKeyOf(Topology.class).getTopologyId();
64 final InstructionScheduler scheduler;
65 ServiceTracker<InstructionScheduler, ?> tracker = null;
67 tracker = new ServiceTracker<>(dependencies.getBundleContext(),
68 dependencies.getBundleContext().createFilter(String.format("(&(%s=%s)%s)", Constants.OBJECTCLASS,
69 InstructionScheduler.class.getName(), "(" + InstructionScheduler.class.getName()
70 + "=" + pcepTopologyId.getValue() + ")")), null);
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 this.ttp = new PCEPTunnelTopologyProvider(dependencies.getDataBroker(), pcepTopology, pcepTopologyId,
86 tunnelTopology, tunnelTopologyId);
89 this.sgi = scheduler.getIdentifier();
90 this.tp = new TunnelProgramming(scheduler, dependencies);
93 final Dictionary<String, String> properties = new Hashtable<>();
94 properties.put(PCEPTunnelTopologyProvider.class.getName(), tunnelTopologyId.getValue());
95 this.serviceRegistration = dependencies.getBundleContext()
96 .registerService(DefaultTopologyReference.class.getName(), this.ttp, properties);
98 LOG.info("PCEP Tunnel Cluster Singleton service {} registered", getIdentifier().getValue());
99 this.pcepTunnelCssReg = dependencies.getCssp().registerClusterSingletonService(this);
104 public synchronized void instantiateServiceInstance() {
105 LOG.info("Instantiate PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getValue());
106 this.reg = this.dependencies.getRpcProviderRegistry()
107 .addRoutedRpcImplementation(TopologyTunnelPcepProgrammingService.class, this.tp);
109 final InstanceIdentifier<Topology> topology = InstanceIdentifier
110 .builder(NetworkTopology.class).child(Topology.class, new TopologyKey(this.tunnelTopologyId)).build();
111 this.reg.registerPath(NetworkTopologyContext.class, topology);
116 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
117 LOG.info("Close Service Instance PCEP Tunnel Topology Provider Singleton Service {}",
118 getIdentifier().getValue());
122 return CommitInfo.emptyFluentFuture();
127 public ServiceGroupIdentifier getIdentifier() {
132 @SuppressWarnings("checkstyle:IllegalCatch")
133 public synchronized void close() {
134 LOG.info("Close PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getValue());
136 if (this.pcepTunnelCssReg != null) {
138 this.pcepTunnelCssReg.close();
139 } catch (final Exception e) {
140 LOG.debug("Failed to close PCEP Tunnel Topology service {}", this.sgi.getValue(), e);
142 this.pcepTunnelCssReg = null;
144 if (this.serviceRegistration != null) {
145 this.serviceRegistration.unregister();
146 this.serviceRegistration = null;