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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.util.concurrent.FluentFuture;
14 import io.netty.channel.Channel;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.ChannelFutureListener;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 import org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyConfiguration;
21 import org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyProviderDependencies;
22 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
23 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
25 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
26 import org.opendaylight.mdsal.common.api.CommitInfo;
27 import org.opendaylight.protocol.pcep.PCEPCapability;
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.rev171025.NetworkTopologyPcepProgrammingService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.NetworkTopologyPcepService;
32 public final class PCEPTopologyProvider extends DefaultTopologyReference {
33 private static final String STATEFUL_NOT_DEFINED = "Stateful capability not defined, aborting PCEP Topology"
34 + " Provider instantiation";
35 private final ServerSessionManager manager;
36 private final PCEPTopologyProviderDependencies dependenciesProvider;
37 private final PCEPTopologyConfiguration configDependencies;
38 private final InstructionScheduler scheduler;
39 private RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network;
40 private RoutedRpcRegistration<NetworkTopologyPcepService> element;
41 private Channel channel;
43 private PCEPTopologyProvider(
44 final PCEPTopologyConfiguration configDependencies,
45 final PCEPTopologyProviderDependencies dependenciesProvider,
46 final ServerSessionManager manager, final InstructionScheduler scheduler) {
47 super(configDependencies.getTopology());
48 this.dependenciesProvider = requireNonNull(dependenciesProvider);
49 this.configDependencies = configDependencies;
50 this.manager = requireNonNull(manager);
51 this.scheduler = requireNonNull(scheduler);
54 public static PCEPTopologyProvider create(final PCEPTopologyProviderDependencies dependenciesProvider,
55 final InstructionScheduler scheduler, final PCEPTopologyConfiguration configDependencies) {
56 final List<PCEPCapability> capabilities = dependenciesProvider.getPCEPDispatcher()
57 .getPCEPSessionNegotiatorFactory().getPCEPSessionProposalFactory().getCapabilities();
58 final Optional<PCEPCapability> statefulCapability = capabilities
60 .filter(PCEPCapability::isStateful)
63 final TopologySessionListenerFactory listenerFactory = dependenciesProvider.getTopologySessionListenerFactory();
64 if (!statefulCapability.isPresent()) {
65 throw new IllegalStateException(STATEFUL_NOT_DEFINED);
68 final ServerSessionManager manager = new ServerSessionManager(dependenciesProvider, listenerFactory,
71 return new PCEPTopologyProvider(configDependencies, dependenciesProvider, manager, scheduler);
74 public void instantiateServiceInstance() throws ExecutionException, InterruptedException {
75 final RpcProviderRegistry rpcRegistry = this.dependenciesProvider.getRpcProviderRegistry();
77 this.element = requireNonNull(rpcRegistry
78 .addRoutedRpcImplementation(NetworkTopologyPcepService.class, new TopologyRPCs(this.manager)));
79 this.element.registerPath(NetworkTopologyContext.class, this.configDependencies.getTopology());
81 this.network = requireNonNull(rpcRegistry
82 .addRoutedRpcImplementation(NetworkTopologyPcepProgrammingService.class,
83 new TopologyProgramming(this.scheduler, this.manager)));
84 this.network.registerPath(NetworkTopologyContext.class, this.configDependencies.getTopology());
86 this.manager.instantiateServiceInstance();
87 final ChannelFuture channelFuture = this.dependenciesProvider.getPCEPDispatcher()
88 .createServer(this.manager.getPCEPDispatcherDependencies());
90 this.channel = channelFuture.channel();
93 public FluentFuture<? extends CommitInfo> closeServiceInstance() {
94 //FIXME return also channelClose once ListenableFuture implements wildcard
95 this.channel.close().addListener((ChannelFutureListener) future ->
96 checkArgument(future.isSuccess(), "Channel failed to close: %s", future.cause()));
98 if (this.network != null) {
102 if (this.element != null) {
103 this.element.close();
106 return this.manager.closeServiceInstance();