BUG-48: more implementation
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / ServerSessionManager.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.GlobalEventExecutor;
13 import io.netty.util.concurrent.Promise;
14
15 import java.net.InetAddress;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21
22 import javax.annotation.concurrent.GuardedBy;
23
24 import org.opendaylight.bgpcep.programming.spi.ExecutionResult;
25 import org.opendaylight.bgpcep.programming.spi.InstructionExecutor;
26 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
27 import org.opendaylight.bgpcep.programming.spi.SuccessfulRpcResult;
28 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
29 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
30 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
31 import org.opendaylight.protocol.framework.SessionListenerFactory;
32 import org.opendaylight.protocol.pcep.PCEPSession;
33 import org.opendaylight.protocol.pcep.PCEPSessionListener;
34 import org.opendaylight.protocol.pcep.PCEPTerminationReason;
35 import org.opendaylight.protocol.pcep.TerminationReason;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcinitiateBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcupdBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.MessageHeader;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcrptMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PlspId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SrpIdNumber;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SrpObject.Flags;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.TlvsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcinitiate.message.PcinitiateMessageBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcinitiate.message.pcinitiate.message.RequestsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcinitiate.message.pcinitiate.message.requests.LspBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcinitiate.message.pcinitiate.message.requests.SrpBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrpt.message.pcrpt.message.Reports;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrpt.message.pcrpt.message.reports.Lsp;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrpt.message.pcrpt.message.reports.Srp;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.PcupdMessageBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.UpdatesBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.updates.PathBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.stateful.capability.tlv.Stateful;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.symbolic.path.name.tlv.SymbolicPathName;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.symbolic.path.name.tlv.SymbolicPathNameBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionStatus;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.status.changed.Details;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.submit.instruction.output.result.FailureBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.submit.instruction.output.result.failure.Failure;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspInput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspOutput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspOutputBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.NetworkTopologyPcepService;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1Builder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.PccSyncState;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspInput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspOutput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspOutputBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspInput;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspOutput;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspOutputBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClientBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsps;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLspsKey;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.StatefulTlvBuilder;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
89 import org.opendaylight.yangtools.yang.binding.DataContainer;
90 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
91 import org.opendaylight.yangtools.yang.common.RpcResult;
92 import org.slf4j.Logger;
93 import org.slf4j.LoggerFactory;
94
95 import com.google.common.base.Preconditions;
96 import com.google.common.collect.ImmutableList;
97
98 /**
99  *
100  */
101 final class ServerSessionManager implements SessionListenerFactory<PCEPSessionListener>, NetworkTopologyPcepService {
102         private static String createNodeId(final InetAddress addr) {
103                 return "pcc://" + addr.getHostAddress();
104         }
105
106         private final class SessionListener implements PCEPSessionListener {
107                 private final Map<SrpIdNumber, Promise<ExecutionResult<Details>>> waitingRequests = new HashMap<>();
108                 private final Map<SrpIdNumber, Promise<ExecutionResult<Details>>> sendingRequests = new HashMap<>();
109                 private final Map<PlspId, SymbolicPathName> lsps = new HashMap<>();
110                 private PathComputationClientBuilder pccBuilder;
111                 private InstanceIdentifier<Node1> topologyAugment;
112                 private InstanceIdentifier<Node> topologyNode;
113                 private Node1Builder topologyAugmentBuilder;
114                 private boolean ownsTopology = false;
115                 private boolean synced = false;
116                 private PCEPSession session;
117                 private long requestId = 1;
118                 private NodeId nodeId;
119
120                 final Node topologyNode(final DataModificationTransaction trans, final InetAddress address) {
121                         final String pccId = createNodeId(address);
122                         final Topology topo = (Topology)trans.readOperationalData(topology);
123
124                         for (final Node n : topo.getNode()) {
125                                 LOG.debug("Matching topology node {} to id {}", n, pccId);
126                                 if (n.getNodeId().getValue().equals(pccId)) {
127                                         return n;
128                                 }
129                         }
130
131                         /*
132                          * We failed to find a matching node. Let's create a dynamic one
133                          * and note that we are the owner (so we clean it up afterwards).
134                          */
135                         final NodeId id = new NodeId(pccId);
136                         final NodeKey nk = new NodeKey(id);
137                         final InstanceIdentifier<Node> nti = InstanceIdentifier.builder(ServerSessionManager.this.topology).node(Node.class, nk).toInstance();
138
139                         final Node ret = new NodeBuilder().setKey(nk).setNodeId(id).build();
140
141                         trans.putRuntimeData(nti, ret);
142                         this.ownsTopology = true;
143                         this.topologyNode = nti;
144                         this.nodeId = id;
145                         return ret;
146                 }
147
148                 @Override
149                 public synchronized void onSessionUp(final PCEPSession session) {
150                         /*
151                          * The session went up. Look up the router in Inventory model,
152                          * create it if it is not there (marking that fact for later
153                          * deletion), and mark it as synchronizing. Also create it in
154                          * the topology model, with empty LSP list.
155                          */
156                         final InetAddress peerAddress = session.getRemoteAddress();
157                         final DataModificationTransaction trans = ServerSessionManager.this.dataProvider.beginTransaction();
158
159                         final Node topoNode = topologyNode(trans, peerAddress);
160                         LOG.debug("Peer {} resolved to topology node {}", peerAddress, topoNode);
161
162                         // Our augmentation in the topology node
163                         this.pccBuilder = new PathComputationClientBuilder();
164
165                         final Tlvs tlvs = session.getRemoteTlvs();
166                         final Stateful stateful = tlvs.getStateful();
167                         if (stateful != null) {
168                                 this.pccBuilder.setStatefulTlv(new StatefulTlvBuilder(tlvs).build());
169                                 this.pccBuilder.setStateSync(PccSyncState.InitialResync);
170                         }
171
172                         topologyAugmentBuilder = new Node1Builder().setPathComputationClient(pccBuilder.build());
173                         topologyAugment = InstanceIdentifier.builder(topologyNode).node(Node1.class).toInstance();
174                         trans.putRuntimeData(topologyAugment, topologyAugmentBuilder.build());
175
176                         // All set, commit the modifications
177                         final Future<RpcResult<TransactionStatus>> s = trans.commit();
178
179                         /*
180                          * FIXME: once this Future is listenable, attach to it so we can
181                          *        do cleanup if the commit fails. For now we force a commit.
182                          */
183                         try {
184                                 s.get();
185                         } catch (InterruptedException | ExecutionException e) {
186                                 LOG.error("Failed to update internal state for session {}, terminating it", session, e);
187                                 session.close(TerminationReason.Unknown);
188                                 return;
189                         }
190
191                         nodes.put(nodeId, this);
192                         this.session = session;
193                         LOG.info("Session with {} attached to topology node {}", session.getRemoteAddress(), topoNode.getNodeId());
194                 }
195
196                 @GuardedBy("this")
197                 private void tearDown(final PCEPSession session) {
198                         this.session = null;
199                         nodes.remove(nodeId);
200
201                         final DataModificationTransaction trans = ServerSessionManager.this.dataProvider.beginTransaction();
202
203                         // The session went down. Undo all the Topology changes we have done.
204                         trans.removeRuntimeData(topologyAugment);
205                         if (ownsTopology) {
206                                 trans.removeRuntimeData(topologyNode);
207                         }
208
209                         /*
210                          * FIXME: once this Future is listenable, attach to it so we can
211                          *        do cleanup if the commit fails. For now we force a commit.
212                          */
213                         final Future<RpcResult<TransactionStatus>> s = trans.commit();
214                         try {
215                                 s.get();
216                         } catch (InterruptedException | ExecutionException e) {
217                                 LOG.error("Failed to cleanup internal state for session {}", session, e);
218                         }
219
220                         // Clear all requests which have not been sent to the peer: they result in cancellation
221                         for (Entry<SrpIdNumber, Promise<ExecutionResult<Details>>> e : sendingRequests.entrySet()) {
222                                 LOG.debug("Request {} was not sent when session went down, cancelling the instruction", e.getKey());
223                                 e.getValue().setSuccess(new ExecutionResult<Details>(InstructionStatus.Cancelled, null));
224                         }
225                         sendingRequests.clear();
226
227                         // CLear all requests which have not been acked by the peer: they result in failure
228                         for (Entry<SrpIdNumber, Promise<ExecutionResult<Details>>> e : waitingRequests.entrySet()) {
229                                 LOG.info("Request {} was incomplete when session went down, failing the instruction", e.getKey());
230                                 e.getValue().setSuccess(new ExecutionResult<Details>(InstructionStatus.Failed, null));
231                         }
232                         waitingRequests.clear();
233                 }
234
235                 @Override
236                 public synchronized void onSessionDown(final PCEPSession session, final Exception e) {
237                         LOG.warn("Session {} went down unexpectedly", e);
238                         tearDown(session);
239                 }
240
241                 @Override
242                 public synchronized void onSessionTerminated(final PCEPSession session, final PCEPTerminationReason reason) {
243                         LOG.info("Session {} terminated by peer with reason {}", session, reason);
244                         tearDown(session);
245                 }
246
247                 private InstanceIdentifier<ReportedLsps> lspIdentifier(final SymbolicPathName name) {
248                         return InstanceIdentifier.builder(topologyAugment).
249                                         node(ReportedLsps.class, new ReportedLspsKey(name.getPathName())).toInstance();
250                 }
251
252                 @Override
253                 public synchronized void onMessage(final PCEPSession session, final Message message) {
254                         if (!(message instanceof PcrptMessage)) {
255                                 LOG.info("Unhandled message {} on session {}", message, session);
256                                 session.sendMessage(unhandledMessageError);
257                         }
258
259                         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrpt.message.PcrptMessage rpt = ((PcrptMessage) message).getPcrptMessage();
260
261                         final DataModificationTransaction trans = ServerSessionManager.this.dataProvider.beginTransaction();
262
263                         for (final Reports r : rpt.getReports()) {
264                                 final Lsp lsp = r.getLsp();
265
266                                 if (lsp.isSync() && !this.synced) {
267                                         // Update synchronization flag
268                                         synced = true;
269                                         topologyAugmentBuilder.setPathComputationClient(pccBuilder.setStateSync(PccSyncState.Synchronized).build());
270                                         trans.putRuntimeData(topologyAugment, topologyAugmentBuilder.build());
271                                         LOG.debug("Session {} achieved synchronized state", session);
272                                 }
273
274                                 final Srp srp = r.getSrp();
275                                 if (srp != null) {
276                                         final SrpIdNumber id = srp.getOperationId();
277                                         if (id.getValue() != 0) {
278                                                 switch (lsp.getOperational()) {
279                                                 case Active:
280                                                 case Down:
281                                                 case Up:
282                                                         final Promise<ExecutionResult<Details>> p = waitingRequests.remove(id);
283                                                         if (p != null) {
284                                                                 LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.getOperational());
285                                                                 p.setSuccess(new ExecutionResult<Details>(InstructionStatus.Successful, null));
286                                                         } else {
287                                                                 LOG.warn("Request ID {} not found in outstanding DB", id);
288                                                         }
289                                                         break;
290                                                 case GoingDown:
291                                                 case GoingUp:
292                                                         // These are transitive states, so
293                                                         break;
294                                                 }
295                                         }
296                                 }
297
298                                 final PlspId id = lsp.getPlspId();
299                                 if (lsp.isRemove()) {
300                                         final SymbolicPathName name = this.lsps.remove(id);
301                                         if (name != null) {
302                                                 trans.removeRuntimeData(lspIdentifier(name));
303                                         }
304
305                                         LOG.debug("LSP {} removed", lsp);
306                                 } else {
307                                         if (!this.lsps.containsKey(id)) {
308                                                 LOG.debug("PLSPID {} not known yet, looking for a symbolic name", id);
309
310                                                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.Tlvs tlvs = r.getLsp().getTlvs();
311                                                 final SymbolicPathName name = tlvs.getSymbolicPathName();
312                                                 if (name == null) {
313                                                         LOG.error("PLSPID {} seen for the first time, not reporting the LSP");
314                                                         // TODO: what should we do here?
315                                                         continue;
316                                                 }
317                                         }
318
319                                         final SymbolicPathName name = this.lsps.get(id);
320                                         trans.putRuntimeData(lspIdentifier(name), lsp);
321
322                                         LOG.debug("LSP {} updated");
323                                 }
324                         }
325
326                         /*
327                          * FIXME: once this Future is listenable, attach to it so we can
328                          *        do cleanup if the commit fails. For now we force a commit.
329                          */
330                         final Future<RpcResult<TransactionStatus>> s = trans.commit();
331                         try {
332                                 s.get();
333                         } catch (InterruptedException | ExecutionException e) {
334                                 LOG.error("Failed to update internal state for session {}, closing it", session, e);
335                                 session.close(TerminationReason.Unknown);
336                         }
337                 }
338
339                 private synchronized SrpIdNumber nextRequest() {
340                         return new SrpIdNumber(requestId++);
341                 }
342
343                 private synchronized void messageSendingComplete(final SrpIdNumber requestId, final io.netty.util.concurrent.Future<Void> future) {
344                         final Promise<ExecutionResult<Details>> promise = sendingRequests.remove(requestId);
345
346                         if (future.isSuccess()) {
347                                 waitingRequests.put(requestId, promise);
348                         } else {
349                                 LOG.info("Failed to send request {}, instruction cancelled", requestId, future.cause());
350                                 promise.setSuccess(new ExecutionResult<Details>(InstructionStatus.Cancelled, null));
351                         }
352                 }
353
354                 private synchronized io.netty.util.concurrent.Future<ExecutionResult<Details>> sendMessage(final Message message, final SrpIdNumber requestId) {
355                         final io.netty.util.concurrent.Future<Void> f = session.sendMessage(message);
356                         final Promise<ExecutionResult<Details>> ret = exec.newPromise();
357
358                         sendingRequests.put(requestId, ret);
359
360                         f.addListener(new FutureListener<Void>() {
361                                 @Override
362                                 public void operationComplete(final io.netty.util.concurrent.Future<Void> future) {
363                                         messageSendingComplete(requestId, future);
364                                 }
365                         });
366
367                         return ret;
368                 }
369         }
370
371         private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
372         private static final Pcerr unhandledMessageError = new PcerrBuilder().setPcerrMessage(
373                         new PcerrMessageBuilder().setErrorType(null).build()).build();
374         private static final MessageHeader messageHeader = new MessageHeader() {
375                 private final ProtocolVersion version = new ProtocolVersion((short)1);
376
377                 @Override
378                 public Class<? extends DataContainer> getImplementedInterface() {
379                         return MessageHeader.class;
380                 }
381
382                 @Override
383                 public ProtocolVersion getVersion() {
384                         return version;
385                 }
386         };
387         private static final EventExecutor exec = GlobalEventExecutor.INSTANCE;
388         private final Map<NodeId, SessionListener> nodes = new HashMap<>();
389         private final InstanceIdentifier<Topology> topology;
390         private final DataProviderService dataProvider;
391         private final InstructionScheduler scheduler;
392
393         public ServerSessionManager(final InstructionScheduler scheduler, final DataProviderService dataProvider,
394                         final InstanceIdentifier<Topology> topology) {
395                 this.dataProvider = Preconditions.checkNotNull(dataProvider);
396                 this.topology = Preconditions.checkNotNull(topology);
397                 this.scheduler = Preconditions.checkNotNull(scheduler);
398         }
399
400         @Override
401         public PCEPSessionListener getSessionListener() {
402                 return new SessionListener();
403         }
404
405         private synchronized io.netty.util.concurrent.Future<ExecutionResult<Details>> realAddLsp(final AddLspInput input) {
406                 // Get the listener corresponding to the node
407                 final SessionListener l = nodes.get(input.getNode());
408                 if (l == null) {
409                         LOG.debug("Session for node {} not found", input.getNode());
410                         return exec.newSucceededFuture(new ExecutionResult<Details>(InstructionStatus.Cancelled, null));
411                 }
412
413                 final InstanceIdentifier<ReportedLsps> lsp = InstanceIdentifier.builder(l.topologyAugment).node(ReportedLsps.class, new ReportedLspsKey(input.getName())).toInstance();
414                 if (dataProvider.readOperationalData(lsp) != null) {
415                         LOG.debug("Node {} already contains lsp {} at {}", input.getNode(), input.getName(), lsp);
416                         return exec.newSucceededFuture(new ExecutionResult<Details>(InstructionStatus.Cancelled, null));
417                 }
418
419                 final RequestsBuilder rb = new RequestsBuilder(input.getArguments());
420                 rb.setSrp(new SrpBuilder().setOperationId(l.nextRequest()).setProcessingRule(Boolean.TRUE).build());
421                 rb.setLsp(
422                                 new LspBuilder().setAdministrative(input.getArguments().isAdministrative()).setTlvs(
423                                                 new TlvsBuilder().setSymbolicPathName(
424                                                                 new SymbolicPathNameBuilder().setPathName(input.getName()).build()).build()).build());
425
426                 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(messageHeader);
427                 ib.setRequests(ImmutableList.of(rb.build()));
428
429                 return l.sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), rb.getSrp().getOperationId());
430         }
431
432         private synchronized io.netty.util.concurrent.Future<ExecutionResult<Details>> realRemoveLsp(final RemoveLspInput input) {
433                 // Get the listener corresponding to the node
434                 final SessionListener l = nodes.get(input.getNode());
435                 if (l == null) {
436                         LOG.debug("Session for node {} not found", input.getNode());
437                         return exec.newSucceededFuture(new ExecutionResult<Details>(InstructionStatus.Cancelled, null));
438                 }
439
440                 final InstanceIdentifier<ReportedLsps> lsp = InstanceIdentifier.builder(l.topologyAugment).node(ReportedLsps.class, new ReportedLspsKey(input.getName())).toInstance();
441                 final ReportedLsps rep = (ReportedLsps) dataProvider.readOperationalData(lsp);
442                 if (rep == null) {
443                         LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
444                         return exec.newSucceededFuture(new ExecutionResult<Details>(InstructionStatus.Cancelled, null));
445                 }
446
447                 final RequestsBuilder rb = new RequestsBuilder();
448                 rb.setSrp(new SrpBuilder().setOperationId(l.nextRequest()).setProcessingRule(Boolean.TRUE).setFlags(new Flags(Boolean.TRUE)).build());
449                 rb.setLsp(new LspBuilder().setRemove(Boolean.TRUE).setPlspId(rep.getLsp().getPlspId()).setDelegate(Boolean.TRUE).build());
450
451                 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(messageHeader);
452                 ib.setRequests(ImmutableList.of(rb.build()));
453                 return l.sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), rb.getSrp().getOperationId());
454         }
455
456         private synchronized io.netty.util.concurrent.Future<ExecutionResult<Details>> realUpdateLsp(final UpdateLspInput input) {
457                 // Get the listener corresponding to the node
458                 final SessionListener l = nodes.get(input.getNode());
459                 if (l == null) {
460                         LOG.debug("Session for node {} not found", input.getNode());
461                         return exec.newSucceededFuture(new ExecutionResult<Details>(InstructionStatus.Cancelled, null));
462                 }
463
464                 final InstanceIdentifier<ReportedLsps> lsp = InstanceIdentifier.builder(l.topologyAugment).node(ReportedLsps.class, new ReportedLspsKey(input.getName())).toInstance();
465                 final ReportedLsps rep = (ReportedLsps) dataProvider.readOperationalData(lsp);
466                 if (rep == null) {
467                         LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
468                         return exec.newSucceededFuture(new ExecutionResult<Details>(InstructionStatus.Cancelled, null));
469                 }
470
471                 final UpdatesBuilder rb = new UpdatesBuilder();
472                 rb.setSrp(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.updates.SrpBuilder().setOperationId(l.nextRequest()).setProcessingRule(Boolean.TRUE).build());
473                 rb.setLsp(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.updates.LspBuilder().
474                                 setPlspId(rep.getLsp().getPlspId()).setDelegate(Boolean.TRUE).build());
475                 rb.setPath(new PathBuilder(input.getArguments()).build());
476
477                 final PcupdMessageBuilder ub = new PcupdMessageBuilder(messageHeader);
478                 ub.setUpdates(ImmutableList.of(rb.build()));
479                 return l.sendMessage(new PcupdBuilder().setPcupdMessage(ub.build()).build(), rb.getSrp().getOperationId());
480         }
481
482         @Override
483         public Future<RpcResult<AddLspOutput>> addLsp(final AddLspInput input) {
484                 Preconditions.checkArgument(input.getNode() != null);
485                 Preconditions.checkArgument(input.getName() != null);
486
487                 final InstructionExecutor e = new InstructionExecutor() {
488                         @Override
489                         public io.netty.util.concurrent.Future<ExecutionResult<Details>> execute() {
490                                 return realAddLsp(input);
491                         }
492                 };
493
494                 final Failure f = this.scheduler.submitInstruction(input, e);
495                 final AddLspOutputBuilder b = new AddLspOutputBuilder();
496                 if (f != null) {
497                         b.setResult(new FailureBuilder().setFailure(f).build());
498                 }
499
500                 final RpcResult<AddLspOutput> res = SuccessfulRpcResult.create(b.build());
501                 return exec.newSucceededFuture(res);
502         }
503
504         @Override
505         public Future<RpcResult<RemoveLspOutput>> removeLsp(final RemoveLspInput input) {
506                 Preconditions.checkArgument(input.getNode() != null);
507                 Preconditions.checkArgument(input.getName() != null);
508
509                 final InstructionExecutor e = new InstructionExecutor() {
510                         @Override
511                         public io.netty.util.concurrent.Future<ExecutionResult<Details>> execute() {
512                                 return realRemoveLsp(input);
513                         }
514                 };
515
516                 final Failure f = this.scheduler.submitInstruction(input, e);
517                 final RemoveLspOutputBuilder b = new RemoveLspOutputBuilder();
518                 if (f != null) {
519                         b.setResult(new FailureBuilder().setFailure(f).build());
520                 }
521
522                 final RpcResult<RemoveLspOutput> res = SuccessfulRpcResult.create(b.build());
523                 return exec.newSucceededFuture(res);
524         }
525
526         @Override
527         public Future<RpcResult<UpdateLspOutput>> updateLsp(final UpdateLspInput input) {
528                 Preconditions.checkArgument(input.getNode() != null);
529                 Preconditions.checkArgument(input.getName() != null);
530
531                 final InstructionExecutor e = new InstructionExecutor() {
532                         @Override
533                         public io.netty.util.concurrent.Future<ExecutionResult<Details>> execute() {
534                                 return realUpdateLsp(input);
535                         }
536                 };
537
538                 final Failure f = this.scheduler.submitInstruction(input, e);
539                 final UpdateLspOutputBuilder b = new UpdateLspOutputBuilder();
540                 if (f != null) {
541                         b.setResult(new FailureBuilder().setFailure(f).build());
542                 }
543
544                 final RpcResult<UpdateLspOutput> res = SuccessfulRpcResult.create(b.build());
545                 return exec.newSucceededFuture(res);
546         }
547 }