84f8024f25050570db6aa741e9e484b85a7ba6aa
[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.collect.ImmutableClassToInstanceMap;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
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.binding.Rpc;
49 import org.opendaylight.yangtools.yang.common.RpcResult;
50
51 final class TopologyRPCs {
52     private final ServerSessionManager manager;
53
54     TopologyRPCs(final ServerSessionManager manager) {
55         this.manager = requireNonNull(manager);
56     }
57
58     Registration register(final RpcProviderService rpcProviderService,
59             final KeyedInstanceIdentifier<Topology, TopologyKey> path) {
60         return rpcProviderService.registerRpcImplementations(ImmutableClassToInstanceMap.<Rpc<?, ?>>builder()
61             .put(AddLsp.class, this::addLsp)
62             .put(RemoveLsp.class, this::removeLsp)
63             .put(TriggerSync.class, this::triggerSync)
64             .put(UpdateLsp.class, this::updateLsp)
65             .put(EnsureLspOperational.class, this::ensureLspOperational)
66             .put(TearDownSession.class, this::tearDownSession)
67             .build(), Set.of(path));
68     }
69
70     @VisibleForTesting
71     ListenableFuture<RpcResult<AddLspOutput>> addLsp(final AddLspInput input) {
72         return Futures.transform(manager.addLsp(input),
73             output -> SuccessfulRpcResult.create(new AddLspOutputBuilder(output).build()),
74             MoreExecutors.directExecutor());
75     }
76
77     @VisibleForTesting
78     ListenableFuture<RpcResult<RemoveLspOutput>> removeLsp(final RemoveLspInput input) {
79         return Futures.transform(manager.removeLsp(input),
80             output -> SuccessfulRpcResult.create(new RemoveLspOutputBuilder(output).build()),
81             MoreExecutors.directExecutor());
82     }
83
84     private ListenableFuture<RpcResult<TriggerSyncOutput>> triggerSync(final TriggerSyncInput input) {
85         return Futures.transform(manager.triggerSync(input),
86             output -> SuccessfulRpcResult.create(new TriggerSyncOutputBuilder(output).build()),
87             MoreExecutors.directExecutor());
88     }
89
90     @VisibleForTesting
91     ListenableFuture<RpcResult<UpdateLspOutput>> updateLsp(final UpdateLspInput input) {
92         return Futures.transform(manager.updateLsp(input),
93             output -> SuccessfulRpcResult.create(new UpdateLspOutputBuilder(output).build()),
94             MoreExecutors.directExecutor());
95     }
96
97     @VisibleForTesting
98     ListenableFuture<RpcResult<EnsureLspOperationalOutput>> ensureLspOperational(
99             final EnsureLspOperationalInput input) {
100         return Futures.transform(manager.ensureLspOperational(input),
101             output -> SuccessfulRpcResult.create(new EnsureLspOperationalOutputBuilder(output).build()),
102             MoreExecutors.directExecutor());
103     }
104
105     @VisibleForTesting
106     ListenableFuture<RpcResult<TearDownSessionOutput>> tearDownSession(final TearDownSessionInput input) {
107         return Futures.transform(manager.tearDownSession(input),
108             output -> SuccessfulRpcResult.create(new TearDownSessionOutputBuilder().build()),
109             MoreExecutors.directExecutor());
110     }
111 }