BUG-9079 Make PCEP session recoverable from exception
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / TopologyProgramming.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.base.Preconditions;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.opendaylight.bgpcep.pcep.topology.spi.AbstractInstructionExecutor;
16 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
17 import org.opendaylight.bgpcep.programming.spi.SuccessfulRpcResult;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.NetworkTopologyPcepProgrammingService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitAddLspInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitAddLspOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitAddLspOutputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitEnsureLspOperationalInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitEnsureLspOperationalOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitEnsureLspOperationalOutputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitRemoveLspInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitRemoveLspOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitRemoveLspOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitTriggerSyncInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitTriggerSyncOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitTriggerSyncOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitUpdateLspInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitUpdateLspOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitUpdateLspOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37
38 /**
39  *
40  */
41 final class TopologyProgramming implements NetworkTopologyPcepProgrammingService {
42     private final InstructionScheduler scheduler;
43     private final ServerSessionManager manager;
44
45     TopologyProgramming(final InstructionScheduler scheduler, final ServerSessionManager manager) {
46         this.scheduler = requireNonNull(scheduler);
47         this.manager = requireNonNull(manager);
48     }
49
50     @Override
51     public ListenableFuture<RpcResult<SubmitAddLspOutput>> submitAddLsp(final SubmitAddLspInput input) {
52         Preconditions.checkArgument(input.getNode() != null);
53         Preconditions.checkArgument(input.getName() != null);
54
55         final SubmitAddLspOutputBuilder b = new SubmitAddLspOutputBuilder();
56         b.setResult(AbstractInstructionExecutor.schedule(this.scheduler, new AbstractInstructionExecutor(input) {
57             @Override
58             protected ListenableFuture<OperationResult> invokeOperation() {
59                 return TopologyProgramming.this.manager.addLsp(input);
60             }
61         }));
62
63         final RpcResult<SubmitAddLspOutput> res = SuccessfulRpcResult.create(b.build());
64         return Futures.immediateFuture(res);
65     }
66
67     @Override
68     public ListenableFuture<RpcResult<SubmitRemoveLspOutput>> submitRemoveLsp(final SubmitRemoveLspInput input) {
69         Preconditions.checkArgument(input.getNode() != null);
70         Preconditions.checkArgument(input.getName() != null);
71
72         final SubmitRemoveLspOutputBuilder b = new SubmitRemoveLspOutputBuilder();
73         b.setResult(AbstractInstructionExecutor.schedule(this.scheduler, new AbstractInstructionExecutor(input) {
74             @Override
75             protected ListenableFuture<OperationResult> invokeOperation() {
76                 return TopologyProgramming.this.manager.removeLsp(input);
77             }
78         }));
79
80         final RpcResult<SubmitRemoveLspOutput> res = SuccessfulRpcResult.create(b.build());
81         return Futures.immediateFuture(res);
82     }
83
84     @Override
85     public ListenableFuture<RpcResult<SubmitUpdateLspOutput>> submitUpdateLsp(final SubmitUpdateLspInput input) {
86         Preconditions.checkArgument(input.getNode() != null);
87         Preconditions.checkArgument(input.getName() != null);
88
89         final SubmitUpdateLspOutputBuilder b = new SubmitUpdateLspOutputBuilder();
90         b.setResult(AbstractInstructionExecutor.schedule(this.scheduler, new AbstractInstructionExecutor(input) {
91             @Override
92             protected ListenableFuture<OperationResult> invokeOperation() {
93                 return TopologyProgramming.this.manager.updateLsp(input);
94             }
95         }));
96
97         final RpcResult<SubmitUpdateLspOutput> res = SuccessfulRpcResult.create(b.build());
98         return Futures.immediateFuture(res);
99     }
100
101     @Override
102     public ListenableFuture<RpcResult<SubmitEnsureLspOperationalOutput>> submitEnsureLspOperational(
103             final SubmitEnsureLspOperationalInput input) {
104         Preconditions.checkArgument(input.getNode() != null);
105         Preconditions.checkArgument(input.getName() != null);
106         Preconditions.checkArgument(input.getArguments() != null);
107
108         // FIXME: can we validate this early?
109         // Preconditions.checkArgument(input.getArguments().getOperational() != null);
110
111         final SubmitEnsureLspOperationalOutputBuilder b = new SubmitEnsureLspOperationalOutputBuilder();
112         b.setResult(AbstractInstructionExecutor.schedule(this.scheduler, new AbstractInstructionExecutor(input) {
113             @Override
114             protected ListenableFuture<OperationResult> invokeOperation() {
115                 EnsureLspOperationalInputBuilder ensureLspOperationalInputBuilder = new EnsureLspOperationalInputBuilder();
116                 ensureLspOperationalInputBuilder.fieldsFrom(input);
117                 return TopologyProgramming.this.manager.ensureLspOperational(ensureLspOperationalInputBuilder.build());
118             }
119         }));
120
121         final RpcResult<SubmitEnsureLspOperationalOutput> res = SuccessfulRpcResult.create(b.build());
122         return Futures.immediateFuture(res);
123     }
124
125
126     @Override
127     public ListenableFuture<RpcResult<SubmitTriggerSyncOutput>> submitTriggerSync(final SubmitTriggerSyncInput input) {
128         Preconditions.checkArgument(input.getNode() != null);
129
130         final SubmitTriggerSyncOutputBuilder b = new SubmitTriggerSyncOutputBuilder();
131         b.setResult(AbstractInstructionExecutor.schedule(this.scheduler, new AbstractInstructionExecutor(input) {
132             @Override
133             protected ListenableFuture<OperationResult> invokeOperation() {
134                 return TopologyProgramming.this.manager.triggerSync(input);
135             }
136         }));
137
138         final RpcResult<SubmitTriggerSyncOutput> res = SuccessfulRpcResult.create(b.build());
139         return Futures.immediateFuture(res);
140     }
141 }