0b2699e443b47bb8a695900298a7c93c8a0987fd
[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.Preconditions;
11 import io.netty.channel.Channel;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.ChannelFutureListener;
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.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.protocol.pcep.PCEPDispatcher;
24 import org.opendaylight.tcpmd5.api.KeyMapping;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.NetworkTopologyPcepProgrammingService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.NetworkTopologyPcepService;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public final class PCEPTopologyProvider extends DefaultTopologyReference implements AutoCloseable {
34
35     private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProvider.class);
36     private final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network;
37     private final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element;
38     private final ServerSessionManager manager;
39     private final Channel channel;
40
41     private PCEPTopologyProvider(final Channel channel, final InstanceIdentifier<Topology> topology, final ServerSessionManager manager,
42             final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element,
43             final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network) {
44         super(topology);
45         this.channel = Preconditions.checkNotNull(channel);
46         this.manager = Preconditions.checkNotNull(manager);
47         this.element = Preconditions.checkNotNull(element);
48         this.network = Preconditions.checkNotNull(network);
49     }
50
51     public static PCEPTopologyProvider create(final PCEPDispatcher dispatcher, final InetSocketAddress address, final KeyMapping keys,
52             final InstructionScheduler scheduler, final DataBroker dataBroker, final RpcProviderRegistry rpcRegistry,
53             final InstanceIdentifier<Topology> topology, final TopologySessionListenerFactory listenerFactory) throws InterruptedException,
54             ExecutionException, ReadFailedException, TransactionCommitFailedException {
55
56         final ServerSessionManager manager = new ServerSessionManager(dataBroker, topology, listenerFactory);
57         final ChannelFuture f = dispatcher.createServer(address, keys, manager);
58         f.get();
59
60         final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> element = rpcRegistry.addRoutedRpcImplementation(
61                 NetworkTopologyPcepService.class, new TopologyRPCs(manager));
62         element.registerPath(NetworkTopologyContext.class, topology);
63
64         final BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> network = rpcRegistry.addRoutedRpcImplementation(
65                 NetworkTopologyPcepProgrammingService.class, new TopologyProgramming(scheduler, manager));
66         network.registerPath(NetworkTopologyContext.class, topology);
67
68         return new PCEPTopologyProvider(f.channel(), topology, manager, element, network);
69     }
70
71     @Override
72     public void close() {
73         LOG.debug("Closing server channel {}", channel);
74
75         channel.close().addListener(new ChannelFutureListener() {
76             @Override
77             public void operationComplete(final ChannelFuture f) {
78                 LOG.debug("Server channel {} closed", f.channel());
79
80                 try {
81                     network.close();
82                 } catch (Exception e) {
83                     LOG.error("Failed to unregister network-level RPCs", e);
84                 }
85                 try {
86                     element.close();
87                 } catch (Exception e) {
88                     LOG.error("Failed to unregister element-level RPCs", e);
89                 }
90                 try {
91                     manager.close();
92                 } catch (Exception e) {
93                     LOG.error("Failed to shutdown session manager", e);
94                 }
95             }
96         });
97     }
98 }