Modernize RPC registration
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / TopologyRPCs.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.List;
17 import java.util.Set;
18 import org.opendaylight.bgpcep.programming.spi.SuccessfulRpcResult;
19 import org.opendaylight.mdsal.binding.api.RpcProviderService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.AddLsp;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.AddLspInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.AddLspOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.AddLspOutputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.EnsureLspOperational;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.EnsureLspOperationalInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.EnsureLspOperationalOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.EnsureLspOperationalOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.RemoveLsp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.RemoveLspInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.RemoveLspOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.RemoveLspOutputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TearDownSession;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TearDownSessionInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TearDownSessionOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TearDownSessionOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TriggerSync;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TriggerSyncInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TriggerSyncOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TriggerSyncOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.UpdateLsp;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.UpdateLspInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.UpdateLspOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.UpdateLspOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
46 import org.opendaylight.yangtools.concepts.Registration;
47 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49
50 final class TopologyRPCs {
51     private final ServerSessionManager manager;
52
53     TopologyRPCs(final ServerSessionManager manager) {
54         this.manager = requireNonNull(manager);
55     }
56
57     Registration register(final RpcProviderService rpcProviderService,
58             final KeyedInstanceIdentifier<Topology, TopologyKey> path) {
59         return rpcProviderService.registerRpcImplementations(List.of(
60             (AddLsp) this::addLsp,
61             (RemoveLsp) this::removeLsp,
62             (TriggerSync) this::triggerSync,
63             (UpdateLsp) this::updateLsp,
64             (EnsureLspOperational) this::ensureLspOperational,
65             (TearDownSession) this::tearDownSession), Set.of(path));
66     }
67
68     @VisibleForTesting
69     ListenableFuture<RpcResult<AddLspOutput>> addLsp(final AddLspInput input) {
70         return Futures.transform(manager.addLsp(input),
71             output -> SuccessfulRpcResult.create(new AddLspOutputBuilder(output).build()),
72             MoreExecutors.directExecutor());
73     }
74
75     @VisibleForTesting
76     ListenableFuture<RpcResult<RemoveLspOutput>> removeLsp(final RemoveLspInput input) {
77         return Futures.transform(manager.removeLsp(input),
78             output -> SuccessfulRpcResult.create(new RemoveLspOutputBuilder(output).build()),
79             MoreExecutors.directExecutor());
80     }
81
82     private ListenableFuture<RpcResult<TriggerSyncOutput>> triggerSync(final TriggerSyncInput input) {
83         return Futures.transform(manager.triggerSync(input),
84             output -> SuccessfulRpcResult.create(new TriggerSyncOutputBuilder(output).build()),
85             MoreExecutors.directExecutor());
86     }
87
88     @VisibleForTesting
89     ListenableFuture<RpcResult<UpdateLspOutput>> updateLsp(final UpdateLspInput input) {
90         return Futures.transform(manager.updateLsp(input),
91             output -> SuccessfulRpcResult.create(new UpdateLspOutputBuilder(output).build()),
92             MoreExecutors.directExecutor());
93     }
94
95     @VisibleForTesting
96     ListenableFuture<RpcResult<EnsureLspOperationalOutput>> ensureLspOperational(
97             final EnsureLspOperationalInput input) {
98         return Futures.transform(manager.ensureLspOperational(input),
99             output -> SuccessfulRpcResult.create(new EnsureLspOperationalOutputBuilder(output).build()),
100             MoreExecutors.directExecutor());
101     }
102
103     @VisibleForTesting
104     ListenableFuture<RpcResult<TearDownSessionOutput>> tearDownSession(final TearDownSessionInput input) {
105         return Futures.transform(manager.tearDownSession(input),
106             output -> SuccessfulRpcResult.create(new TearDownSessionOutputBuilder().build()),
107             MoreExecutors.directExecutor());
108     }
109 }