51c9d251fc8b7eb2606eaec0c5f587b9ded7da06
[bgpcep.git] / pcep / tunnel / tunnel-provider / src / main / java / org / opendaylight / bgpcep / pcep / tunnel / provider / PCEPTunnelClusterSingletonService.java
1 /*
2  * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.bgpcep.pcep.tunnel.provider;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Map;
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;
37
38 public final class PCEPTunnelClusterSingletonService implements ClusterSingletonService, AutoCloseable {
39
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;
46     @GuardedBy("this")
47     private ServiceRegistration<?> serviceRegistration;
48     @GuardedBy("this")
49     private ClusterSingletonServiceRegistration pcepTunnelCssReg;
50     @GuardedBy("this")
51     private Registration reg;
52
53     public PCEPTunnelClusterSingletonService(
54             final TunnelProviderDependencies dependencies,
55             final InstanceIdentifier<Topology> pcepTopology,
56             final TopologyId tunnelTopologyId
57     ) {
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();
62
63         final InstructionScheduler scheduler;
64         ServiceTracker<InstructionScheduler, ?> tracker = null;
65         try {
66             tracker = new ServiceTracker<>(bundleContext,
67                 bundleContext.createFilter("(&(%s=%s)%s)".formatted(
68                     Constants.OBJECTCLASS, InstructionScheduler.class.getName(),
69                     "(" + InstructionScheduler.class.getName() + "=" + pcepTopologyId.getValue() + ")")),
70                 null);
71             tracker.open();
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);
77         } finally {
78             if (tracker != null) {
79                 tracker.close();
80             }
81         }
82
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);
87
88
89         sgi = scheduler.getIdentifier();
90         tp = new TunnelProgramming(scheduler, dependencies);
91
92
93         serviceRegistration = bundleContext.registerService(DefaultTopologyReference.class, ttp,
94             FrameworkUtil.asDictionary(Map.of(
95                 PCEPTunnelTopologyProvider.class.getName(), tunnelTopologyId.getValue())));
96
97         LOG.info("PCEP Tunnel Cluster Singleton service {} registered", getIdentifier().getName());
98         pcepTunnelCssReg = dependencies.getCssp().registerClusterSingletonService(this);
99     }
100
101     @Override
102     public synchronized void instantiateServiceInstance() {
103         LOG.info("Instantiate PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getName());
104
105         reg = tp.register(InstanceIdentifier.builder(NetworkTopology.class)
106             .child(Topology.class, new TopologyKey(tunnelTopologyId))
107             .build());
108         ttp.init();
109     }
110
111     @Override
112     public synchronized ListenableFuture<? extends CommitInfo> closeServiceInstance() {
113         LOG.info("Close Service Instance PCEP Tunnel Topology Provider Singleton Service {}",
114                 getIdentifier().getName());
115         reg.close();
116         tp.close();
117         ttp.close();
118         return CommitInfo.emptyFluentFuture();
119     }
120
121     @Override
122     public ServiceGroupIdentifier getIdentifier() {
123         return sgi;
124     }
125
126     @Override
127     @SuppressWarnings("checkstyle:IllegalCatch")
128     public synchronized void close() {
129         LOG.info("Close PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getName());
130
131         if (pcepTunnelCssReg != null) {
132             try {
133                 pcepTunnelCssReg.close();
134             } catch (final Exception e) {
135                 LOG.debug("Failed to close PCEP Tunnel Topology service {}", sgi.getName(), e);
136             }
137             pcepTunnelCssReg = null;
138         }
139         if (serviceRegistration != null) {
140             serviceRegistration.unregister();
141             serviceRegistration = null;
142         }
143     }
144 }