Code Clean Up
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / AbstractTopologySessionListener.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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import io.netty.util.concurrent.FutureListener;
16 import java.net.InetAddress;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Timer;
24 import java.util.TimerTask;
25 import java.util.concurrent.TimeUnit;
26 import javax.annotation.concurrent.GuardedBy;
27 import org.opendaylight.controller.config.yang.pcep.topology.provider.ListenerStateRuntimeMXBean;
28 import org.opendaylight.controller.config.yang.pcep.topology.provider.ListenerStateRuntimeRegistration;
29 import org.opendaylight.controller.config.yang.pcep.topology.provider.PeerCapabilities;
30 import org.opendaylight.controller.config.yang.pcep.topology.provider.ReplyTime;
31 import org.opendaylight.controller.config.yang.pcep.topology.provider.SessionState;
32 import org.opendaylight.controller.config.yang.pcep.topology.provider.StatefulMessages;
33 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
34 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.protocol.pcep.PCEPSession;
37 import org.opendaylight.protocol.pcep.PCEPSessionListener;
38 import org.opendaylight.protocol.pcep.PCEPTerminationReason;
39 import org.opendaylight.protocol.pcep.TerminationReason;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.MessageHeader;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
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.topology.pcep.rev131024.LspId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1Builder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.PccSyncState;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.lsp.metadata.Metadata;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClient;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClientBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLspKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.reported.lsp.Path;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
58 import org.opendaylight.yangtools.yang.binding.DataContainer;
59 import org.opendaylight.yangtools.yang.binding.DataObject;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 /**
65  * Base class for PCEP topology providers. It handles the common tasks involved in managing a PCEP server (PCE)
66  * endpoint, and exposing a network topology based on it. It needs to be subclassed to form a fully functional block,
67  * where the subclass provides handling of incoming messages.
68  *
69  * @param <S> identifier type of requests
70  * @param <L> identifier type for LSPs
71  */
72 public abstract class AbstractTopologySessionListener<S, L> implements PCEPSessionListener, TopologySessionListener, ListenerStateRuntimeMXBean {
73     protected static final class MessageContext {
74         private final Collection<PCEPRequest> requests = new ArrayList<>();
75         private final WriteTransaction trans;
76
77         private MessageContext(final WriteTransaction trans) {
78             this.trans = Preconditions.checkNotNull(trans);
79         }
80
81         void resolveRequest(final PCEPRequest req) {
82             this.requests.add(req);
83         }
84
85         private void notifyRequests() {
86             for (final PCEPRequest r : this.requests) {
87                 r.done(OperationResults.SUCCESS);
88             }
89         }
90     }
91
92     protected static final MessageHeader MESSAGE_HEADER = new MessageHeader() {
93         private final ProtocolVersion version = new ProtocolVersion((short) 1);
94
95         @Override
96         public Class<? extends DataContainer> getImplementedInterface() {
97             return MessageHeader.class;
98         }
99
100         @Override
101         public ProtocolVersion getVersion() {
102             return this.version;
103         }
104     };
105
106     private static final Logger LOG = LoggerFactory.getLogger(AbstractTopologySessionListener.class);
107
108     protected static final String MISSING_XML_TAG = "Mandatory XML tags are missing.";
109
110     @GuardedBy("this")
111     private final Map<S, PCEPRequest> requests = new HashMap<>();
112
113     @GuardedBy("this")
114     private final Map<String, ReportedLsp> lspData = new HashMap<>();
115
116     @GuardedBy("this")
117     protected final Map<L, String> lsps = new HashMap<>();
118
119     private final ServerSessionManager serverSessionManager;
120     private InstanceIdentifier<PathComputationClient> pccIdentifier;
121     private TopologyNodeState nodeState;
122     private boolean synced = false;
123     private PCEPSession session;
124     private SyncOptimization syncOptimization;
125     private boolean triggeredResyncInProcess;
126
127     private ListenerStateRuntimeRegistration registration;
128     private final SessionListenerState listenerState;
129
130     protected AbstractTopologySessionListener(final ServerSessionManager serverSessionManager) {
131         this.serverSessionManager = Preconditions.checkNotNull(serverSessionManager);
132         this.listenerState = new SessionListenerState();
133     }
134
135     @Override
136     public final synchronized void onSessionUp(final PCEPSession session) {
137         /*
138          * The session went up. Look up the router in Inventory model,
139          * create it if it is not there (marking that fact for later
140          * deletion), and mark it as synchronizing. Also create it in
141          * the topology model, with empty LSP list.
142          */
143         final InetAddress peerAddress = session.getRemoteAddress();
144
145         this.syncOptimization = new SyncOptimization(session);
146
147         final TopologyNodeState state = this.serverSessionManager.takeNodeState(peerAddress, this, isLspDbRetreived());
148         // takeNodeState(..) may fail when the server session manager is being restarted due to configuration change
149         if (state == null) {
150             this.onSessionDown(session, new RuntimeException("Unable to fetch topology node state for PCEP session with " + session.getRemoteAddress()));
151             return;
152         }
153
154         if (this.session != null || this.nodeState != null) {
155             this.onSessionDown(session, new IllegalStateException("Session is already up with " + session.getRemoteAddress()));
156             return;
157         }
158
159         this.session = session;
160         this.nodeState = state;
161
162         LOG.trace("Peer {} resolved to topology node {}", peerAddress, state.getNodeId());
163
164         // Our augmentation in the topology node
165         final PathComputationClientBuilder pccBuilder = new PathComputationClientBuilder();
166
167         onSessionUp(session, pccBuilder);
168         this.synced = isSynchronized();
169
170         pccBuilder.setIpAddress(IpAddressBuilder.getDefaultInstance(peerAddress.getHostAddress()));
171         final InstanceIdentifier<Node1> topologyAugment = state.getNodeId().augmentation(Node1.class);
172         this.pccIdentifier = topologyAugment.child(PathComputationClient.class);
173         final Node initialNodeState = state.getInitialNodeState();
174         final boolean isNodePresent = isLspDbRetreived() && initialNodeState != null;
175         if (isNodePresent) {
176             loadLspData(initialNodeState, this.lspData, this.lsps, isIncrementalSynchro());
177             pccBuilder.setReportedLsp(initialNodeState.getAugmentation(Node1.class).getPathComputationClient().getReportedLsp());
178         }
179         writeNode(pccBuilder, state, topologyAugment);
180         this.listenerState.init(session);
181         this.registration = this.serverSessionManager.registerRuntimeRootRegistration(this);
182         if (this.registration == null) {
183             this.onSessionDown(session, new RuntimeException("PCEP Session with " + session.getRemoteAddress() + " fails to register."));
184             return;
185         }
186         LOG.info("Session with {} attached to topology node {}", session.getRemoteAddress(), state.getNodeId());
187     }
188
189     private void writeNode(final PathComputationClientBuilder pccBuilder, final TopologyNodeState state,
190             final InstanceIdentifier<Node1> topologyAugment) {
191         final Node1 ta = new Node1Builder().setPathComputationClient(pccBuilder.build()).build();
192
193         final ReadWriteTransaction trans = state.rwTransaction();
194         trans.put(LogicalDatastoreType.OPERATIONAL, topologyAugment, ta);
195         LOG.trace("Peer data {} set to {}", topologyAugment, ta);
196
197         // All set, commit the modifications
198         Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
199             @Override
200             public void onSuccess(final Void result) {
201                 LOG.trace("Internal state for session {} updated successfully", AbstractTopologySessionListener.this.session);
202             }
203
204             @Override
205             public void onFailure(final Throwable t) {
206                 LOG.error("Failed to update internal state for session {}, terminating it", AbstractTopologySessionListener.this.session, t);
207                 AbstractTopologySessionListener.this.session.close(TerminationReason.UNKNOWN);
208             }
209         });
210     }
211
212     protected void updatePccState(final PccSyncState pccSyncState) {
213         final MessageContext ctx = new MessageContext(this.nodeState.beginTransaction());
214         updatePccNode(ctx, new PathComputationClientBuilder().setStateSync(pccSyncState).build());
215         if (pccSyncState != PccSyncState.Synchronized) {
216             this.synced = false;
217             this.triggeredResyncInProcess = true;
218         }
219         // All set, commit the modifications
220         Futures.addCallback(ctx.trans.submit(), new FutureCallback<Void>() {
221             @Override
222             public void onSuccess(final Void result) {
223                 LOG.trace("Internal state for session {} updated successfully", AbstractTopologySessionListener.this.session);
224             }
225
226             @Override
227             public void onFailure(final Throwable t) {
228                 LOG.error("Failed to update internal state for session {}", AbstractTopologySessionListener.this.session, t);
229                 AbstractTopologySessionListener.this.session.close(TerminationReason.UNKNOWN);
230             }
231         });
232     }
233
234     protected boolean isTriggeredSyncInProcess() {
235         return this.triggeredResyncInProcess;
236     }
237
238     /**
239      * Tear down the given PCEP session. It's OK to call this method even after the session
240      * is already down. It always clear up the current session status.
241      * @param session
242      */
243     @GuardedBy("this")
244     private synchronized void tearDown(final PCEPSession session) {
245         Preconditions.checkNotNull(session);
246         this.serverSessionManager.releaseNodeState(this.nodeState, session, isLspDbPersisted());
247         this.nodeState = null;
248         this.session = null;
249         this.syncOptimization = null;
250         unregister();
251
252         // Clear all requests we know about
253         for (final Entry<S, PCEPRequest> e : this.requests.entrySet()) {
254             final PCEPRequest r = e.getValue();
255             switch (r.getState()) {
256             case DONE:
257                 // Done is done, nothing to do
258                 LOG.trace("Request {} was done when session went down.", e.getKey());
259                 break;
260             case UNACKED:
261                 // Peer has not acked: results in failure
262                 LOG.info("Request {} was incomplete when session went down, failing the instruction", e.getKey());
263                 r.done(OperationResults.NOACK);
264                 break;
265             case UNSENT:
266                 // Peer has not been sent to the peer: results in cancellation
267                 LOG.debug("Request {} was not sent when session went down, cancelling the instruction", e.getKey());
268                 r.done(OperationResults.UNSENT);
269                 break;
270             default:
271                 break;
272             }
273         }
274         this.requests.clear();
275     }
276
277     @Override
278     public final synchronized void onSessionDown(final PCEPSession session, final Exception e) {
279         LOG.warn("Session {} went down unexpectedly", session, e);
280         tearDown(session);
281     }
282
283     @Override
284     public final synchronized void onSessionTerminated(final PCEPSession session, final PCEPTerminationReason reason) {
285         LOG.info("Session {} terminated by peer with reason {}", session, reason);
286         tearDown(session);
287     }
288
289     @Override
290     public final synchronized void onMessage(final PCEPSession session, final Message message) {
291         final MessageContext ctx = new MessageContext(this.nodeState.beginTransaction());
292
293         if (onMessage(ctx, message)) {
294             LOG.info("Unhandled message {} on session {}", message, session);
295             //cancel not supported, submit empty transaction
296             ctx.trans.submit();
297             return;
298         }
299
300         Futures.addCallback(ctx.trans.submit(), new FutureCallback<Void>() {
301             @Override
302             public void onSuccess(final Void result) {
303                 LOG.trace("Internal state for session {} updated successfully", session);
304                 ctx.notifyRequests();
305             }
306
307             @Override
308             public void onFailure(final Throwable t) {
309                 LOG.error("Failed to update internal state for session {}, closing it", session, t);
310                 ctx.notifyRequests();
311                 session.close(TerminationReason.UNKNOWN);
312             }
313         });
314     }
315
316     @Override
317     public void close() {
318         unregister();
319         if (this.session != null) {
320             this.session.close(TerminationReason.UNKNOWN);
321         }
322     }
323
324     private synchronized void unregister() {
325         if (this.registration != null) {
326             this.registration.close();
327             LOG.trace("PCEP session {} unregistered successfully.", this.session);
328             this.registration = null;
329         } else {
330             LOG.trace("PCEP session {} was not registered.", this.session);
331         }
332     }
333
334     protected final synchronized PCEPRequest removeRequest(final S id) {
335         final PCEPRequest ret = this.requests.remove(id);
336         if (ret != null) {
337             this.listenerState.processRequestStats(ret.getElapsedMillis());
338         }
339         LOG.trace("Removed request {} object {}", id, ret);
340         return ret;
341     }
342
343     protected final synchronized ListenableFuture<OperationResult> sendMessage(final Message message, final S requestId,
344         final Metadata metadata) {
345         final io.netty.util.concurrent.Future<Void> f = this.session.sendMessage(message);
346         this.listenerState.updateStatefulSentMsg(message);
347         final PCEPRequest req = new PCEPRequest(metadata);
348         this.requests.put(requestId, req);
349         final int rpcTimeout = this.serverSessionManager.getRpcTimeout();
350         LOG.trace("RPC response timeout value is {} seconds", rpcTimeout);
351         if (rpcTimeout > 0) {
352             setupTimeoutHandler(requestId, req, rpcTimeout);
353         }
354
355         f.addListener((FutureListener<Void>) future -> {
356             if (!future.isSuccess()) {
357                 synchronized (AbstractTopologySessionListener.this) {
358                     AbstractTopologySessionListener.this.requests.remove(requestId);
359                 }
360                 req.done(OperationResults.UNSENT);
361                 LOG.info("Failed to send request {}, instruction cancelled", requestId, future.cause());
362             } else {
363                 req.sent();
364                 LOG.trace("Request {} sent to peer (object {})", requestId, req);
365             }
366         });
367
368         return req.getFuture();
369     }
370
371     private void setupTimeoutHandler(final S requestId, final PCEPRequest req, final int timeout) {
372         final Timer timer = req.getTimer();
373         timer.schedule(new TimerTask() {
374             @Override
375             public void run() {
376                 synchronized (AbstractTopologySessionListener.this) {
377                     AbstractTopologySessionListener.this.requests.remove(requestId);
378                 }
379                 req.done();
380                 LOG.info("Request {} timed-out waiting for response", requestId);
381             }
382         }, TimeUnit.SECONDS.toMillis(timeout));
383         LOG.trace("Set up response timeout handler for request {}", requestId);
384     }
385
386     /**
387      * Update an LSP in the data store
388      *
389      * @param ctx Message context
390      * @param id Revision-specific LSP identifier
391      * @param lspName LSP name
392      * @param rlb Reported LSP builder
393      * @param solicited True if the update was solicited
394      * @param remove True if this is an LSP path removal
395      */
396     protected final synchronized void updateLsp(final MessageContext ctx, final L id, final String lspName,
397         final ReportedLspBuilder rlb, final boolean solicited, final boolean remove) {
398
399         final String name;
400         if (lspName == null) {
401             name = this.lsps.get(id);
402             if (name == null) {
403                 LOG.error("PLSPID {} seen for the first time, not reporting the LSP", id);
404                 return;
405             }
406         } else {
407             name = lspName;
408         }
409
410         LOG.debug("Saved LSP {} with name {}", id, name);
411         this.lsps.put(id, name);
412
413
414         final ReportedLsp previous = this.lspData.get(name);
415         // if no previous report about the lsp exist, just proceed
416         if (previous != null) {
417             final List<Path> updatedPaths = makeBeforeBreak(rlb, previous, name, remove);
418             // if all paths or the last path were deleted, delete whole tunnel
419             if (updatedPaths == null || updatedPaths.isEmpty()) {
420                 LOG.debug("All paths were removed, removing LSP with {}.", id);
421                 removeLsp(ctx, id);
422                 return;
423             }
424             rlb.setPath(updatedPaths);
425         }
426         rlb.setKey(new ReportedLspKey(name));
427         rlb.setName(name);
428
429         // If this is an unsolicited update. We need to make sure we retain the metadata already present
430         if (solicited) {
431             this.nodeState.setLspMetadata(name, rlb.getMetadata());
432         } else {
433             rlb.setMetadata(this.nodeState.getLspMetadata(name));
434         }
435
436         final ReportedLsp rl = rlb.build();
437         ctx.trans.put(LogicalDatastoreType.OPERATIONAL, this.pccIdentifier.child(ReportedLsp.class, rlb.getKey()), rl);
438         LOG.debug("LSP {} updated to MD-SAL", name);
439
440         this.lspData.put(name, rl);
441     }
442
443     private List<Path> makeBeforeBreak(final ReportedLspBuilder rlb, final ReportedLsp previous, final String name, final boolean remove) {
444         // just one path should be reported
445         Preconditions.checkState(rlb.getPath().size() == 1);
446         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId reportedLspId = rlb.getPath().get(0).getLspId();
447         final List<Path> updatedPaths;
448         //lspId = 0 and remove = false -> tunnel is down, still exists but no path is signaled
449         //remove existing tunnel's paths now, as explicit path remove will not come
450         if (!remove && reportedLspId.getValue() == 0) {
451             updatedPaths = new ArrayList<>();
452             LOG.debug("Remove previous paths {} to this lsp name {}", previous.getPath(), name);
453         } else {
454             // check previous report for existing paths
455             updatedPaths = new ArrayList<>(previous.getPath());
456             LOG.debug("Found previous paths {} to this lsp name {}", updatedPaths, name);
457             for (final Path path : previous.getPath()) {
458                 //we found reported path in previous reports
459                 if (path.getLspId().getValue() == 0 || path.getLspId().equals(reportedLspId)) {
460                     LOG.debug("Match on lsp-id {}", path.getLspId().getValue() );
461                     // path that was reported previously and does have the same lsp-id, path will be updated
462                     final boolean r = updatedPaths.remove(path);
463                     LOG.trace("Request removed? {}", r);
464                 }
465             }
466         }
467         // if the path does not exist in previous report, add it to path list, it's a new ERO
468         // only one path will be added
469         //lspId is 0 means confirmation message that shouldn't be added (because we have no means of deleting it later)
470         LOG.trace("Adding new path {} to {}", rlb.getPath(), updatedPaths);
471         updatedPaths.addAll(rlb.getPath());
472         if (remove) {
473             if (reportedLspId.getValue() == 0) {
474                 // if lsp-id also 0, remove all paths
475                 LOG.debug("Removing all paths.");
476                 updatedPaths.clear();
477             } else {
478                 // path is marked to be removed
479                 LOG.debug("Removing path {} from {}", rlb.getPath(), updatedPaths);
480                 final boolean r = updatedPaths.removeAll(rlb.getPath());
481                 LOG.trace("Request removed? {}", r);
482             }
483         }
484         LOG.debug("Setting new paths {} to lsp {}", updatedPaths, name);
485         return updatedPaths;
486     }
487
488     /**
489      * Indicate that the peer has completed state synchronization.
490      *
491      * @param ctx Message context
492      */
493     protected final synchronized void stateSynchronizationAchieved(final MessageContext ctx) {
494         if (this.synced) {
495             LOG.debug("State synchronization achieved while synchronized, not updating state");
496             return;
497         }
498
499         // Update synchronization flag
500         this.synced = true;
501         if(this.triggeredResyncInProcess) {
502             this.triggeredResyncInProcess = false;
503         }
504         updatePccNode(ctx, new PathComputationClientBuilder().setStateSync(PccSyncState.Synchronized).build());
505
506         // The node has completed synchronization, cleanup metadata no longer reported back
507         this.nodeState.cleanupExcept(this.lsps.values());
508         LOG.debug("Session {} achieved synchronized state", this.session);
509     }
510
511     protected final synchronized void updatePccNode(final MessageContext ctx, final PathComputationClient pcc) {
512         ctx.trans.merge(LogicalDatastoreType.OPERATIONAL, this.pccIdentifier, pcc);
513     }
514
515     protected final InstanceIdentifier<ReportedLsp> lspIdentifier(final String name) {
516         return this.pccIdentifier.child(ReportedLsp.class, new ReportedLspKey(name));
517     }
518
519     /**
520      * Remove LSP from the database.
521      *
522      * @param ctx Message Context
523      * @param id Revision-specific LSP identifier
524      */
525     protected final synchronized void removeLsp(final MessageContext ctx, final L id) {
526         final String name = this.lsps.remove(id);
527         LOG.debug("LSP {} removed", name);
528         ctx.trans.delete(LogicalDatastoreType.OPERATIONAL, lspIdentifier(name));
529         this.lspData.remove(name);
530     }
531
532     protected abstract void onSessionUp(PCEPSession session, PathComputationClientBuilder pccBuilder);
533
534     /**
535      * Perform revision-specific message processing when a message arrives.
536      *
537      * @param ctx Message processing context
538      * @param message Protocol message
539      * @return True if the message type is not handle.
540      */
541     protected abstract boolean onMessage(MessageContext ctx, Message message);
542
543     protected final String lookupLspName(final L id) {
544         Preconditions.checkNotNull(id, "ID parameter null.");
545         return this.lsps.get(id);
546     }
547
548     /**
549      * Reads operational data on this node. Doesn't attempt to read the data,
550      * if the node does not exist. In this case returns null.
551      *
552      * @param id InstanceIdentifier of the node
553      * @return null if the node does not exists, or operational data
554      */
555     protected final synchronized <T extends DataObject> ListenableFuture<Optional<T>> readOperationalData(final InstanceIdentifier<T> id) {
556         if (this.nodeState == null) {
557             return null;
558         }
559         return this.nodeState.readOperationalData(id);
560     }
561
562     protected abstract Object validateReportedLsp(final Optional<ReportedLsp> rep, final LspId input);
563
564     protected abstract void loadLspData(final Node node, final Map<String, ReportedLsp> lspData, final Map<L, String> lsps, final boolean incrementalSynchro);
565
566     protected final boolean isLspDbPersisted() {
567         if (this.syncOptimization != null) {
568             return this.syncOptimization.isSyncAvoidanceEnabled();
569         }
570         return false;
571     }
572
573     protected final boolean isLspDbRetreived() {
574         if (this.syncOptimization != null) {
575             return this.syncOptimization.isDbVersionPresent();
576         }
577         return false;
578     }
579
580     /**
581      * Is Incremental synchronization if LSP-DB-VERSION are included,
582      * LSP-DB-VERSION TLV values doesnt match, and  LSP-SYNC-CAPABILITY is enabled
583      * @return
584      */
585     protected final boolean isIncrementalSynchro() {
586         if (this.syncOptimization != null) {
587             return this.syncOptimization.isSyncAvoidanceEnabled() && this.syncOptimization.isDeltaSyncEnabled();
588         }
589         return false;
590     }
591
592     protected final boolean isTriggeredInitialSynchro() {
593         if (this.syncOptimization != null) {
594             return this.syncOptimization.isTriggeredInitSyncEnabled();
595         }
596         return false;
597     }
598
599     protected final boolean isTriggeredReSyncEnabled() {
600         if (this.syncOptimization != null) {
601             return this.syncOptimization.isTriggeredReSyncEnabled();
602         }
603         return false;
604     }
605
606     protected final boolean isSynchronized() {
607         if (this.syncOptimization != null) {
608             return this.syncOptimization.doesLspDbMatch();
609         }
610         return false;
611     }
612
613     protected SessionListenerState getSessionListenerState() {
614         return this.listenerState;
615     }
616
617     @Override
618     public Integer getDelegatedLspsCount() {
619         return this.lsps.size();
620     }
621
622     @Override
623     public Boolean getSynchronized() {
624         return this.synced;
625     }
626
627     @Override
628     public StatefulMessages getStatefulMessages() {
629         return this.listenerState.getStatefulMessages();
630     }
631
632     @Override
633     public synchronized void resetStats() {
634         this.listenerState.resetStats(this.session);
635     }
636
637     @Override
638     public ReplyTime getReplyTime() {
639         return this.listenerState.getReplyTime();
640     }
641
642     @Override
643     public PeerCapabilities getPeerCapabilities() {
644         return this.listenerState.getPeerCapabilities();
645     }
646
647     @Override
648     public void tearDownSession() {
649         this.close();
650     }
651
652     @Override
653     public synchronized SessionState getSessionState() {
654         return this.listenerState.getSessionState(this.session);
655     }
656
657     @Override
658     public synchronized String getPeerId() {
659         return this.session.getPeerPref().getIpAddress();
660     }
661 }