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