2 * Copyright (c) 2013 Cisco Systems, Inc. and others. 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.topology.provider;
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.List;
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.concepts.KeyMapping;
26 import org.opendaylight.protocol.pcep.PCEPCapability;
27 import org.opendaylight.protocol.pcep.PCEPDispatcher;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyContext;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.NetworkTopologyPcepProgrammingService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.NetworkTopologyPcepService;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public final class PCEPTopologyProvider extends DefaultTopologyReference implements AutoCloseable {
38 private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProvider.class);
39 private final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network;
40 private final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element;
41 private final ServerSessionManager manager;
42 private final Channel channel;
44 private PCEPTopologyProvider(final Channel channel, final InstanceIdentifier<Topology> topology, final ServerSessionManager manager,
45 final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element,
46 final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network) {
48 this.channel = Preconditions.checkNotNull(channel);
49 this.manager = Preconditions.checkNotNull(manager);
50 this.element = Preconditions.checkNotNull(element);
51 this.network = Preconditions.checkNotNull(network);
54 public static PCEPTopologyProvider create(final PCEPDispatcher dispatcher, final InetSocketAddress address, final Optional<KeyMapping> keys,
55 final InstructionScheduler scheduler, final DataBroker dataBroker, final RpcProviderRegistry rpcRegistry,
56 final InstanceIdentifier<Topology> topology, final TopologySessionListenerFactory listenerFactory,
57 final Optional<PCEPTopologyProviderRuntimeRegistrator> runtimeRootRegistrator, final int rpcTimeout) throws InterruptedException,
58 ExecutionException, ReadFailedException, TransactionCommitFailedException {
59 List<PCEPCapability> capabilities = dispatcher.getPCEPSessionNegotiatorFactory().getPCEPSessionProposalFactory().getCapabilities();
60 boolean statefulCapability = false;
61 for (final PCEPCapability capability : capabilities) {
62 if (capability.isStateful()) {
63 statefulCapability = true;
67 if (!statefulCapability && listenerFactory != null) {
68 throw new IllegalStateException("Stateful capability not defined, aborting PCEP Topology Provider instantiation");
71 final ServerSessionManager manager = new ServerSessionManager(dataBroker, topology, listenerFactory, rpcTimeout);
72 if (runtimeRootRegistrator.isPresent()) {
73 manager.registerRuntimeRootRegistartion(runtimeRootRegistrator.get());
75 final ChannelFuture f = dispatcher.createServer(address, keys, manager, manager);
78 final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element = rpcRegistry.addRoutedRpcImplementation(
79 NetworkTopologyPcepService.class, new TopologyRPCs(manager));
80 element.registerPath(NetworkTopologyContext.class, topology);
82 final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network = rpcRegistry.addRoutedRpcImplementation(
83 NetworkTopologyPcepProgrammingService.class, new TopologyProgramming(scheduler, manager));
84 network.registerPath(NetworkTopologyContext.class, topology);
86 return new PCEPTopologyProvider(f.channel(), topology, manager, element, network);
90 public void close() throws InterruptedException {
92 this.channel.close().sync();
93 LOG.debug("Server channel {} closed", this.channel);
94 } catch (InterruptedException e) {
95 LOG.error("Failed to close channel {}", this.channel, e);
99 PCEPTopologyProvider.this.network.close();
100 } catch (final Exception e) {
101 LOG.error("Failed to unregister network-level RPCs", e);
104 PCEPTopologyProvider.this.element.close();
105 } catch (final Exception e) {
106 LOG.error("Failed to unregister element-level RPCs", e);
109 PCEPTopologyProvider.this.manager.close();
110 } catch (final Exception e) {
111 LOG.error("Failed to shutdown session manager", e);