Bug-5612: ODL(PCEP) infinitely waits for the response from peer for addlsp
[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 java.net.InetSocketAddress;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
17 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
18 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistrator;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.protocol.pcep.PCEPDispatcher;
25 import org.opendaylight.tcpmd5.api.KeyMapping;
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.pcep.programming.rev131106.NetworkTopologyPcepProgrammingService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.NetworkTopologyPcepService;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public final class PCEPTopologyProvider extends DefaultTopologyReference implements AutoCloseable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProvider.class);
37     private final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network;
38     private final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element;
39     private final ServerSessionManager manager;
40     private final Channel channel;
41
42     private PCEPTopologyProvider(final Channel channel, final InstanceIdentifier<Topology> topology, final ServerSessionManager manager,
43             final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element,
44             final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network) {
45         super(topology);
46         this.channel = Preconditions.checkNotNull(channel);
47         this.manager = Preconditions.checkNotNull(manager);
48         this.element = Preconditions.checkNotNull(element);
49         this.network = Preconditions.checkNotNull(network);
50     }
51
52     public static PCEPTopologyProvider create(final PCEPDispatcher dispatcher, final InetSocketAddress address, final Optional<KeyMapping> keys,
53             final InstructionScheduler scheduler, final DataBroker dataBroker, final RpcProviderRegistry rpcRegistry,
54             final InstanceIdentifier<Topology> topology, final TopologySessionListenerFactory listenerFactory,
55             final Optional<PCEPTopologyProviderRuntimeRegistrator> runtimeRootRegistrator, final int rpcTimeout) throws InterruptedException,
56             ExecutionException, ReadFailedException, TransactionCommitFailedException {
57
58         final ServerSessionManager manager = new ServerSessionManager(dataBroker, topology, listenerFactory, rpcTimeout);
59         if (runtimeRootRegistrator.isPresent()) {
60             manager.registerRuntimeRootRegistartion(runtimeRootRegistrator.get());
61         }
62         final ChannelFuture f = dispatcher.createServer(address, keys, manager, manager);
63         f.get();
64
65         final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element = rpcRegistry.addRoutedRpcImplementation(
66                 NetworkTopologyPcepService.class, new TopologyRPCs(manager));
67         element.registerPath(NetworkTopologyContext.class, topology);
68
69         final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network = rpcRegistry.addRoutedRpcImplementation(
70                 NetworkTopologyPcepProgrammingService.class, new TopologyProgramming(scheduler, manager));
71         network.registerPath(NetworkTopologyContext.class, topology);
72
73         return new PCEPTopologyProvider(f.channel(), topology, manager, element, network);
74     }
75
76     @Override
77     public void close() throws InterruptedException {
78         try {
79             this.channel.close().sync();
80             LOG.debug("Server channel {} closed", this.channel);
81         } catch (InterruptedException e) {
82             LOG.error("Failed to close channel {}", this.channel, e);
83         }
84
85         try {
86             PCEPTopologyProvider.this.network.close();
87         } catch (final Exception e) {
88             LOG.error("Failed to unregister network-level RPCs", e);
89         }
90         try {
91             PCEPTopologyProvider.this.element.close();
92         } catch (final Exception e) {
93             LOG.error("Failed to unregister element-level RPCs", e);
94         }
95         try {
96             PCEPTopologyProvider.this.manager.close();
97         } catch (final Exception e) {
98             LOG.error("Failed to shutdown session manager", e);
99         }
100     }
101 }