Bug-5878: PCErr not generated by configuring 32-pcep.xml file to not advertise statef...
[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.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;
35
36 public final class PCEPTopologyProvider extends DefaultTopologyReference implements AutoCloseable {
37
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;
43
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) {
47         super(topology);
48         this.channel = Preconditions.checkNotNull(channel);
49         this.manager = Preconditions.checkNotNull(manager);
50         this.element = Preconditions.checkNotNull(element);
51         this.network = Preconditions.checkNotNull(network);
52     }
53
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;
64                 break;
65             }
66         }
67         if (!statefulCapability && listenerFactory != null) {
68             throw new IllegalStateException("Stateful capability not defined, aborting PCEP Topology Provider instantiation");
69         }
70
71         final ServerSessionManager manager = new ServerSessionManager(dataBroker, topology, listenerFactory, rpcTimeout);
72         if (runtimeRootRegistrator.isPresent()) {
73             manager.registerRuntimeRootRegistartion(runtimeRootRegistrator.get());
74         }
75         final ChannelFuture f = dispatcher.createServer(address, keys, manager, manager);
76         f.get();
77
78         final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element = rpcRegistry.addRoutedRpcImplementation(
79                 NetworkTopologyPcepService.class, new TopologyRPCs(manager));
80         element.registerPath(NetworkTopologyContext.class, topology);
81
82         final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network = rpcRegistry.addRoutedRpcImplementation(
83                 NetworkTopologyPcepProgrammingService.class, new TopologyProgramming(scheduler, manager));
84         network.registerPath(NetworkTopologyContext.class, topology);
85
86         return new PCEPTopologyProvider(f.channel(), topology, manager, element, network);
87     }
88
89     @Override
90     public void close() throws InterruptedException {
91         try {
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);
96         }
97
98         try {
99             PCEPTopologyProvider.this.network.close();
100         } catch (final Exception e) {
101             LOG.error("Failed to unregister network-level RPCs", e);
102         }
103         try {
104             PCEPTopologyProvider.this.element.close();
105         } catch (final Exception e) {
106             LOG.error("Failed to unregister element-level RPCs", e);
107         }
108         try {
109             PCEPTopologyProvider.this.manager.close();
110         } catch (final Exception e) {
111             LOG.error("Failed to shutdown session manager", e);
112         }
113     }
114 }