BUG-48: split the programming and immediate parts
[bgpcep.git] / pcep / 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 io.netty.util.concurrent.EventExecutor;
11 import io.netty.util.concurrent.FutureListener;
12 import io.netty.util.concurrent.Promise;
13
14 import java.util.concurrent.Future;
15
16 import org.opendaylight.bgpcep.programming.spi.SuccessfulRpcResult;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspOutputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.NetworkTopologyPcepService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspOutputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspOutputBuilder;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29
30 import com.google.common.base.Preconditions;
31
32 final class TopologyRPCs implements NetworkTopologyPcepService {
33         private final ServerSessionManager manager;
34         private final EventExecutor exec;
35
36         private abstract class ExecutionResultAdaptor<T> implements FutureListener<OperationResult> {
37                 protected final Promise<RpcResult<T>> promise = exec.newPromise();
38
39                 protected abstract RpcResult<T> convertResult(OperationResult result);
40
41                 @Override
42                 public final void operationComplete(final io.netty.util.concurrent.Future<OperationResult> future) {
43                         if (future.isSuccess()) {
44                                 promise.setSuccess(convertResult(future.getNow()));
45                         } else {
46                                 promise.setFailure(future.cause());
47                         }
48                 }
49         }
50
51         TopologyRPCs(final EventExecutor exec, final ServerSessionManager manager) {
52                 this.manager = Preconditions.checkNotNull(manager);
53                 this.exec = Preconditions.checkNotNull(exec);
54         }
55
56         @Override
57         public Future<RpcResult<AddLspOutput>> addLsp(final AddLspInput input) {
58                 final ExecutionResultAdaptor<AddLspOutput> adaptor = new ExecutionResultAdaptor<AddLspOutput>() {
59                         @Override
60                         protected RpcResult<AddLspOutput> convertResult(final OperationResult result) {
61                                 return SuccessfulRpcResult.create(new AddLspOutputBuilder(result).build());
62                         }
63                 };
64
65                 manager.realAddLsp(input).addListener(adaptor);
66                 return adaptor.promise;
67         }
68
69         @Override
70         public Future<RpcResult<RemoveLspOutput>> removeLsp(final RemoveLspInput input) {
71                 final ExecutionResultAdaptor<RemoveLspOutput> adaptor = new ExecutionResultAdaptor<RemoveLspOutput>() {
72                         @Override
73                         protected RpcResult<RemoveLspOutput> convertResult(final OperationResult result) {
74                                 return SuccessfulRpcResult.create(new RemoveLspOutputBuilder(result).build());
75                         }
76                 };
77
78                 manager.realRemoveLsp(input).addListener(adaptor);
79                 return adaptor.promise;
80         }
81
82         @Override
83         public Future<RpcResult<UpdateLspOutput>> updateLsp(final UpdateLspInput input) {
84                 final ExecutionResultAdaptor<UpdateLspOutput> adaptor = new ExecutionResultAdaptor<UpdateLspOutput>() {
85                         @Override
86                         protected RpcResult<UpdateLspOutput> convertResult(final OperationResult result) {
87                                 return SuccessfulRpcResult.create(new UpdateLspOutputBuilder(result).build());
88                         }
89                 };
90
91                 manager.realUpdateLsp(input).addListener(adaptor);
92                 return adaptor.promise;
93         }
94 }