BGPCEP-726: Migrate PCEP Tunnel config
[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.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;
35
36 public final class PCEPTunnelClusterSingletonService implements ClusterSingletonService, AutoCloseable {
37
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;
44     @GuardedBy("this")
45     private ServiceRegistration<?> serviceRegistration;
46     @GuardedBy("this")
47     private ClusterSingletonServiceRegistration pcepTunnelCssReg;
48     @GuardedBy("this")
49     private BindingAwareBroker.RoutedRpcRegistration<TopologyTunnelPcepProgrammingService> reg;
50
51     public PCEPTunnelClusterSingletonService(
52             final TunnelProviderDependencies dependencies,
53             final InstanceIdentifier<Topology> pcepTopology,
54             final TopologyId tunnelTopologyId
55     ) {
56         this.dependencies = requireNonNull(dependencies);
57         this.tunnelTopologyId = requireNonNull(tunnelTopologyId);
58         final TopologyId pcepTopologyId = pcepTopology.firstKeyOf(Topology.class).getTopologyId();
59
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();
66
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);
71
72
73         this.sgi = scheduler.getIdentifier();
74         this.tp = new TunnelProgramming(scheduler, dependencies);
75
76
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);
81
82         LOG.info("PCEP Tunnel Cluster Singleton service {} registered", getIdentifier().getValue());
83         this.pcepTunnelCssReg = dependencies.getCssp().registerClusterSingletonService(this);
84     }
85
86
87     @Override
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);
92
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);
96         this.ttp.init();
97     }
98
99     @Override
100     public synchronized ListenableFuture<Void> closeServiceInstance() {
101         LOG.info("Close Service Instance PCEP Tunnel Topology Provider Singleton Service {}",
102                 getIdentifier().getValue());
103         this.reg.close();
104         this.tp.close();
105         this.ttp.close();
106         return Futures.immediateFuture(null);
107     }
108
109     @Nonnull
110     @Override
111     public ServiceGroupIdentifier getIdentifier() {
112         return this.sgi;
113     }
114
115     @Override
116     @SuppressWarnings("checkstyle:IllegalCatch")
117     public synchronized void close() {
118         LOG.info("Close PCEP Tunnel Topology Provider Singleton Service {}", getIdentifier().getValue());
119
120         if (this.pcepTunnelCssReg != null) {
121             try {
122                 this.pcepTunnelCssReg.close();
123             } catch (final Exception e) {
124                 LOG.debug("Failed to close PCEP Tunnel Topology service {}", this.sgi.getValue(), e);
125             }
126             this.pcepTunnelCssReg = null;
127         }
128         if (this.serviceRegistration != null) {
129             this.serviceRegistration.unregister();
130             this.serviceRegistration = null;
131         }
132     }
133 }