BUG-2299: allow PCEP extensions to be reconfigured
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / PCEPTopologyProvider.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  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.topology.provider;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelFutureListener;
15 import java.net.InetSocketAddress;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
18 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
19 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistrator;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
25 import org.opendaylight.protocol.pcep.PCEPDispatcher;
26 import org.opendaylight.tcpmd5.api.KeyMapping;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyContext;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.NetworkTopologyPcepProgrammingService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.NetworkTopologyPcepService;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public final class PCEPTopologyProvider extends DefaultTopologyReference implements AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProvider.class);
38     private final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network;
39     private final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element;
40     private final ServerSessionManager manager;
41     private final Channel channel;
42
43     private PCEPTopologyProvider(final Channel channel, final InstanceIdentifier<Topology> topology, final ServerSessionManager manager,
44             final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element,
45             final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network) {
46         super(topology);
47         this.channel = Preconditions.checkNotNull(channel);
48         this.manager = Preconditions.checkNotNull(manager);
49         this.element = Preconditions.checkNotNull(element);
50         this.network = Preconditions.checkNotNull(network);
51     }
52
53     public static PCEPTopologyProvider create(final PCEPDispatcher dispatcher, final InetSocketAddress address, final KeyMapping keys,
54             final InstructionScheduler scheduler, final DataBroker dataBroker, final RpcProviderRegistry rpcRegistry,
55             final InstanceIdentifier<Topology> topology, final TopologySessionListenerFactory listenerFactory,
56             final Optional<PCEPTopologyProviderRuntimeRegistrator> runtimeRootRegistrator) throws InterruptedException,
57             ExecutionException, ReadFailedException, TransactionCommitFailedException {
58
59         final ServerSessionManager manager = new ServerSessionManager(dataBroker, topology, listenerFactory);
60         if (runtimeRootRegistrator.isPresent()) {
61             manager.registerRuntimeRootRegistartion(runtimeRootRegistrator.get());
62         }
63         final ChannelFuture f = dispatcher.createServer(address, keys, manager);
64         f.get();
65
66         final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element = rpcRegistry.addRoutedRpcImplementation(
67                 NetworkTopologyPcepService.class, new TopologyRPCs(manager));
68         element.registerPath(NetworkTopologyContext.class, topology);
69
70         final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network = rpcRegistry.addRoutedRpcImplementation(
71                 NetworkTopologyPcepProgrammingService.class, new TopologyProgramming(scheduler, manager));
72         network.registerPath(NetworkTopologyContext.class, topology);
73
74         return new PCEPTopologyProvider(f.channel(), topology, manager, element, network);
75     }
76
77     @Override
78     public void close() throws InterruptedException {
79         LOG.debug("Closing server channel {}", this.channel);
80
81         this.channel.close().addListener(new ChannelFutureListener() {
82             @Override
83             public void operationComplete(final ChannelFuture f) {
84                 LOG.debug("Server channel {} closed", f.channel());
85
86                 try {
87                     PCEPTopologyProvider.this.network.close();
88                 } catch (final Exception e) {
89                     LOG.error("Failed to unregister network-level RPCs", e);
90                 }
91                 try {
92                     PCEPTopologyProvider.this.element.close();
93                 } catch (final Exception e) {
94                     LOG.error("Failed to unregister element-level RPCs", e);
95                 }
96                 try {
97                     PCEPTopologyProvider.this.manager.close();
98                 } catch (final Exception e) {
99                     LOG.error("Failed to shutdown session manager", e);
100                 }
101             }
102         }).await();
103     }
104 }