Bug-731: Fixed few major Sonar warnings
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / Stateful07TopologySessionListener.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 com.google.common.base.Charsets;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.AsyncFunction;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.net.InetAddress;
18 import java.nio.ByteBuffer;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.concurrent.atomic.AtomicLong;
23 import org.opendaylight.protocol.pcep.PCEPSession;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.PcinitiateBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.pcinitiate.message.PcinitiateMessageBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.pcinitiate.message.pcinitiate.message.Requests;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.pcinitiate.message.pcinitiate.message.RequestsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments2;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments3;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.OperationalStatus;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Path1;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Path1Builder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PcrptMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PcupdBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PlspId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SrpIdNumber;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.StatefulTlv1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.StatefulTlv1Builder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcerr.pcerr.message.error.type.StatefulCase;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcerr.pcerr.message.error.type.stateful._case.stateful.Srps;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.Reports;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.PcupdMessageBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.UpdatesBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.updates.PathBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathNameBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcerrMessage;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.PccSyncState;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClientBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.StatefulTlvBuilder;
71 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
74
75 final class Stateful07TopologySessionListener extends AbstractTopologySessionListener<SrpIdNumber, PlspId> {
76     private static final Logger LOG = LoggerFactory.getLogger(Stateful07TopologySessionListener.class);
77
78     private final AtomicLong requestId = new AtomicLong(1L);
79
80     /**
81      * @param serverSessionManager
82      */
83     Stateful07TopologySessionListener(final ServerSessionManager serverSessionManager) {
84         super(serverSessionManager);
85     }
86
87     @Override
88     protected void onSessionUp(final PCEPSession session, final PathComputationClientBuilder pccBuilder) {
89         final InetAddress peerAddress = session.getRemoteAddress();
90
91         final Tlvs tlvs = session.getRemoteTlvs();
92         if (tlvs != null && tlvs.getAugmentation(Tlvs1.class) != null) {
93             final Stateful stateful = tlvs.getAugmentation(Tlvs1.class).getStateful();
94             if (stateful != null) {
95                 pccBuilder.setReportedLsp(Collections.<ReportedLsp> emptyList());
96                 pccBuilder.setStateSync(PccSyncState.InitialResync);
97                 pccBuilder.setStatefulTlv(new StatefulTlvBuilder().addAugmentation(StatefulTlv1.class,
98                     new StatefulTlv1Builder(tlvs.getAugmentation(Tlvs1.class)).build()).build());
99             } else {
100                 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
101             }
102         } else {
103             LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
104         }
105     }
106
107     @Override
108     protected boolean onMessage(final MessageContext ctx, final Message message) {
109         if (message instanceof PcerrMessage) {
110             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessage errMsg = ((PcerrMessage) message).getPcerrMessage();
111             if (errMsg.getErrorType() instanceof StatefulCase) {
112                 final StatefulCase stat = (StatefulCase)errMsg.getErrorType();
113                 for (final Srps srps : stat.getStateful().getSrps()) {
114                     final SrpIdNumber id = srps.getSrp().getOperationId();
115                     if (id.getValue() != 0) {
116                         final PCEPRequest req = removeRequest(id);
117                         if (req != null) {
118                             req.done(OperationResults.createFailed(errMsg.getErrors()));
119                         } else {
120                             LOG.warn("Request ID {} not found in outstanding DB", id);
121                         }
122                     }
123                 }
124             } else {
125                 LOG.warn("Unhandled PCErr message {}.", errMsg);
126                 return true;
127             }
128             return false;
129         }
130         if (!(message instanceof PcrptMessage)) {
131             return true;
132         }
133
134         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.PcrptMessage rpt = ((PcrptMessage) message).getPcrptMessage();
135         for (final Reports report : rpt.getReports()) {
136             final Lsp lsp = report.getLsp();
137             final PlspId plspid = lsp.getPlspId();
138
139             if (!lsp.isSync() && (lsp.getPlspId() == null || plspid.getValue() == 0)) {
140                 stateSynchronizationAchieved(ctx);
141                 continue;
142             }
143
144             final ReportedLspBuilder rlb = new ReportedLspBuilder();
145
146             boolean solicited = false;
147
148             final Srp srp = report.getSrp();
149             if (srp != null) {
150                 final SrpIdNumber id = srp.getOperationId();
151                 if (id.getValue() != 0) {
152                     solicited = true;
153
154                     switch (lsp.getOperational()) {
155                     case Active:
156                     case Down:
157                     case Up:
158                         final PCEPRequest req = removeRequest(id);
159                         if (req != null) {
160                             LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.getOperational());
161                             rlb.setMetadata(req.getMetadata());
162                             ctx.resolveRequest(req);
163                         } else {
164                             LOG.warn("Request ID {} not found in outstanding DB", id);
165                         }
166                         break;
167                     case GoingDown:
168                     case GoingUp:
169                         // These are transitive states, so we don't have to do anything, as they will be followed
170                         // up...
171                         break;
172                     }
173                     // if remove flag is set in SRP object, remove the tunnel immediately
174                     if (srp.getAugmentation(Srp1.class) != null) {
175                         final Srp1 initiatedSrp = srp.getAugmentation(Srp1.class);
176                         if (initiatedSrp.isRemove()) {
177                             super.removeLsp(ctx, plspid);
178                             return false;
179                         }
180                     }
181                 }
182             }
183             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs tlvs = report.getLsp().getTlvs();
184             final String name;
185             if (tlvs != null && tlvs.getSymbolicPathName() != null) {
186                 name = Charsets.UTF_8.decode(ByteBuffer.wrap(tlvs.getSymbolicPathName().getPathName().getValue())).toString();
187             } else {
188                 name = null;
189             }
190             LspId lspid = null;
191             if (tlvs != null && tlvs.getLspIdentifiers() != null) {
192                 lspid = tlvs.getLspIdentifiers().getLspId();
193             }
194             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.reported.lsp.PathBuilder pb = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.reported.lsp.PathBuilder();
195             if (report.getPath() != null) {
196                 pb.fieldsFrom(report.getPath());
197             }
198             // LSP is mandatory (if there is none, parser will throw an exception)
199             // this is to ensure a path will be created at any rate
200             pb.addAugmentation(Path1.class, new Path1Builder().setLsp(report.getLsp()).build());
201             pb.setLspId(lspid);
202             rlb.setPath(Collections.singletonList(pb.build()));
203             updateLsp(ctx, plspid, name, rlb, solicited, lsp.isRemove());
204             LOG.debug("LSP {} updated", lsp);
205         }
206         return false;
207     }
208
209     private SrpIdNumber nextRequest() {
210         return new SrpIdNumber(this.requestId.getAndIncrement());
211     }
212
213     @Override
214     public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
215         Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, MISSING_XML_TAG);
216         LOG.trace("AddLspArgs {}", input);
217         // Make sure there is no such LSP
218         final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
219         final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
220
221         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
222             @Override
223             public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
224                 if (rep.isPresent()) {
225                     LOG.debug("Node {} already contains lsp {} at {}", input.getNode(), input.getName(), lsp);
226                     return OperationResults.UNSENT.future();
227                 }
228
229                 // Build the request
230                 final RequestsBuilder rb = new RequestsBuilder();
231                 final Arguments2 args = input.getArguments().getAugmentation(Arguments2.class);
232                 Preconditions.checkArgument(args != null, "Input is missing operational tag.");
233                 final Lsp inputLsp = args.getLsp();
234                 Preconditions.checkArgument(inputLsp != null, "Reported LSP does not contain LSP object.");
235
236                 rb.fieldsFrom(input.getArguments());
237
238                 final TlvsBuilder tlvsBuilder = new TlvsBuilder();
239                 tlvsBuilder.setSymbolicPathName(
240                     new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(input.getName().getBytes(Charsets.UTF_8))).build());
241                 if (inputLsp.getTlvs() != null) {
242                     tlvsBuilder.setVsTlv(inputLsp.getTlvs().getVsTlv());
243                 }
244
245                 rb.setSrp(new SrpBuilder().setOperationId(nextRequest()).setProcessingRule(Boolean.TRUE).build());
246                 rb.setLsp(new LspBuilder().setAdministrative(inputLsp.isAdministrative()).setDelegate(inputLsp.isDelegate()).setPlspId(
247                     new PlspId(0L)).setTlvs(tlvsBuilder.build()).build());
248
249                 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
250                 ib.setRequests(Collections.singletonList(rb.build()));
251
252                 // Send the message
253                 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), rb.getSrp().getOperationId(),
254                     input.getArguments().getMetadata());
255             }
256         });
257     }
258
259     @Override
260     public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
261         Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null, MISSING_XML_TAG);
262         LOG.trace("RemoveLspArgs {}", input);
263         // Make sure the LSP exists, we need it for PLSP-ID
264         final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
265         final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
266
267         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
268             @Override
269             public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
270                 if (!rep.isPresent()) {
271                     LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
272                     return OperationResults.UNSENT.future();
273                 }
274
275                 // it doesn't matter how many lsps there are in the path list, we only need delegate & plspid that is the same in each path
276                 final Path1 ra = rep.get().getPath().get(0).getAugmentation(Path1.class);
277                 Preconditions.checkState(ra != null, "Reported LSP reported null from data-store.");
278                 final Lsp reportedLsp = ra.getLsp();
279                 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
280
281                 // Build the request and send it
282                 final RequestsBuilder rb = new RequestsBuilder();
283                 rb.setSrp(new SrpBuilder().addAugmentation(Srp1.class, new Srp1Builder().setRemove(Boolean.TRUE).build()).setOperationId(nextRequest()).setProcessingRule(Boolean.TRUE).build());
284                 rb.setLsp(new LspBuilder().setRemove(Boolean.FALSE).setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).build());
285
286                 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
287                 ib.setRequests(Collections.singletonList(rb.build()));
288                 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), rb.getSrp().getOperationId(), null);
289             }
290         });
291     }
292
293     @Override
294     public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
295         Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, MISSING_XML_TAG);
296         LOG.trace("UpdateLspArgs {}", input);
297         // Make sure the LSP exists
298         final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
299         final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
300
301         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
302             @Override
303             public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
304                 if (!rep.isPresent()) {
305                     LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
306                     return OperationResults.UNSENT.future();
307                 }
308
309                 // it doesn't matter how many lsps there are in the path list, we only need plspid that is the same in each path
310                 final Path1 ra = rep.get().getPath().get(0).getAugmentation(Path1.class);
311                 Preconditions.checkState(ra != null, "Reported LSP reported null from data-store.");
312                 final Lsp reportedLsp = ra.getLsp();
313                 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
314
315                 // create mandatory objects
316                 final Srp srp = new SrpBuilder().setOperationId(nextRequest()).setProcessingRule(Boolean.TRUE).build();
317
318                 final Lsp inputLsp = input.getArguments().getAugmentation(Arguments3.class).getLsp();
319                 Lsp lsp = null;
320                 if (inputLsp != null) {
321                     lsp = new LspBuilder().setPlspId(reportedLsp.getPlspId()).setDelegate((inputLsp.isDelegate() != null) ? inputLsp.isDelegate() : false).setTlvs(inputLsp.getTlvs()).setAdministrative((inputLsp.isAdministrative() != null) ? inputLsp.isAdministrative() : false).build();
322                 } else {
323                     lsp = new LspBuilder().setPlspId(reportedLsp.getPlspId()).build();
324                 }
325                 Message msg = null;
326                 // the D bit that was reported decides the type of PCE message sent
327                 Preconditions.checkNotNull(reportedLsp.isDelegate());
328                 if (reportedLsp.isDelegate()) {
329                     // we already have delegation, send update
330                     final UpdatesBuilder rb = new UpdatesBuilder();
331                     rb.setSrp(srp);
332                     rb.setLsp(lsp);
333                     final PathBuilder pb = new PathBuilder();
334                     pb.fieldsFrom(input.getArguments());
335                     rb.setPath(pb.build());
336                     final PcupdMessageBuilder ub = new PcupdMessageBuilder(MESSAGE_HEADER);
337                     ub.setUpdates(Collections.singletonList(rb.build()));
338                     msg = new PcupdBuilder().setPcupdMessage(ub.build()).build();
339                 } else {
340                     // we want to revoke delegation, different type of message
341                     // is sent because of specification by Siva
342                     // this message is also sent, when input delegate bit is set to 0
343                     // generating an error in PCC
344                     final List<Requests> reqs = new ArrayList<>();
345                     reqs.add(new RequestsBuilder().setSrp(srp).setLsp(lsp).build());
346                     final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder();
347                     ib.setRequests(reqs);
348                     msg = new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build();
349                 }
350                 return sendMessage(msg, srp.getOperationId(), input.getArguments().getMetadata());
351             }
352         });
353     }
354
355     @Override
356     public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
357         Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, MISSING_XML_TAG);
358         final OperationalStatus op;
359         final Arguments1 aa = input.getArguments().getAugmentation(Arguments1.class);
360         if (aa != null) {
361             op = aa.getOperational();
362         } else {
363             op = null;
364         }
365
366         // Make sure the LSP exists
367         final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
368         LOG.debug("Checking if LSP {} has operational state {}", lsp, op);
369         final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
370
371         return Futures.transform(f, new Function<Optional<ReportedLsp>, OperationResult>() {
372             @Override
373             public OperationResult apply(final Optional<ReportedLsp> rep) {
374                 if (!rep.isPresent()) {
375                     LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
376                     return OperationResults.UNSENT;
377                 }
378
379                 // check if at least one of the paths has the same status as requested
380                 boolean operational = false;
381                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.reported.lsp.Path p : rep.get().getPath()) {
382                     final Path1 p1 = p.getAugmentation(Path1.class);
383                     if (p1 == null) {
384                         LOG.warn("Node {} LSP {} does not contain data", input.getNode(), input.getName());
385                         return OperationResults.UNSENT;
386                     }
387                     final Lsp l = p1.getLsp();
388                     if (l.getOperational().equals(op)) {
389                         operational = true;
390                     }
391                 }
392
393                 return operational ? OperationResults.SUCCESS : OperationResults.UNSENT;
394             }
395         });
396     }
397 }