2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.bgpcep.pcep.topology.provider;
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;
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;
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.
69 * @param <S> identifier type of requests
70 * @param <L> identifier type for LSPs
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;
77 private MessageContext(final WriteTransaction trans) {
78 this.trans = Preconditions.checkNotNull(trans);
81 void resolveRequest(final PCEPRequest req) {
82 this.requests.add(req);
85 private void notifyRequests() {
86 for (final PCEPRequest r : this.requests) {
87 r.done(OperationResults.SUCCESS);
92 protected static final MessageHeader MESSAGE_HEADER = new MessageHeader() {
93 private final ProtocolVersion version = new ProtocolVersion((short) 1);
96 public Class<? extends DataContainer> getImplementedInterface() {
97 return MessageHeader.class;
101 public ProtocolVersion getVersion() {
106 private static final Logger LOG = LoggerFactory.getLogger(AbstractTopologySessionListener.class);
108 protected static final String MISSING_XML_TAG = "Mandatory XML tags are missing.";
111 private final Map<S, PCEPRequest> requests = new HashMap<>();
114 private final Map<String, ReportedLsp> lspData = new HashMap<>();
117 protected final Map<L, String> lsps = new HashMap<>();
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;
127 private ListenerStateRuntimeRegistration registration;
128 private final SessionListenerState listenerState;
130 protected AbstractTopologySessionListener(final ServerSessionManager serverSessionManager) {
131 this.serverSessionManager = Preconditions.checkNotNull(serverSessionManager);
132 this.listenerState = new SessionListenerState();
136 public final synchronized void onSessionUp(final PCEPSession session) {
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.
143 final InetAddress peerAddress = session.getRemoteAddress();
145 this.syncOptimization = new SyncOptimization(session);
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
150 this.onSessionDown(session, new RuntimeException("Unable to fetch topology node state for PCEP session with " + session.getRemoteAddress()));
154 if (this.session != null || this.nodeState != null) {
155 this.onSessionDown(session, new IllegalStateException("Session is already up with " + session.getRemoteAddress()));
159 this.session = session;
160 this.nodeState = state;
162 LOG.trace("Peer {} resolved to topology node {}", peerAddress, state.getNodeId());
164 // Our augmentation in the topology node
165 final PathComputationClientBuilder pccBuilder = new PathComputationClientBuilder();
167 onSessionUp(session, pccBuilder);
168 this.synced = isSynchronized();
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;
176 loadLspData(initialNodeState, this.lspData, this.lsps, isIncrementalSynchro());
177 pccBuilder.setReportedLsp(initialNodeState.getAugmentation(Node1.class).getPathComputationClient().getReportedLsp());
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."));
186 LOG.info("Session with {} attached to topology node {}", session.getRemoteAddress(), state.getNodeId());
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();
193 final ReadWriteTransaction trans = state.rwTransaction();
194 trans.put(LogicalDatastoreType.OPERATIONAL, topologyAugment, ta);
195 LOG.trace("Peer data {} set to {}", topologyAugment, ta);
197 // All set, commit the modifications
198 Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
200 public void onSuccess(final Void result) {
201 LOG.trace("Internal state for session {} updated successfully", AbstractTopologySessionListener.this.session);
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);
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) {
217 this.triggeredResyncInProcess = true;
219 // All set, commit the modifications
220 Futures.addCallback(ctx.trans.submit(), new FutureCallback<Void>() {
222 public void onSuccess(final Void result) {
223 LOG.trace("Internal state for session {} updated successfully", AbstractTopologySessionListener.this.session);
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);
234 protected boolean isTriggeredSyncInProcess() {
235 return this.triggeredResyncInProcess;
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.
244 private synchronized void tearDown(final PCEPSession session) {
245 Preconditions.checkNotNull(session);
246 this.serverSessionManager.releaseNodeState(this.nodeState, session, isLspDbPersisted());
247 this.nodeState = null;
249 this.syncOptimization = null;
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()) {
257 // Done is done, nothing to do
258 LOG.trace("Request {} was done when session went down.", e.getKey());
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);
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);
274 this.requests.clear();
278 public final synchronized void onSessionDown(final PCEPSession session, final Exception e) {
279 LOG.warn("Session {} went down unexpectedly", session, e);
284 public final synchronized void onSessionTerminated(final PCEPSession session, final PCEPTerminationReason reason) {
285 LOG.info("Session {} terminated by peer with reason {}", session, reason);
290 public final synchronized void onMessage(final PCEPSession session, final Message message) {
291 final MessageContext ctx = new MessageContext(this.nodeState.beginTransaction());
293 if (onMessage(ctx, message)) {
294 LOG.info("Unhandled message {} on session {}", message, session);
295 //cancel not supported, submit empty transaction
300 Futures.addCallback(ctx.trans.submit(), new FutureCallback<Void>() {
302 public void onSuccess(final Void result) {
303 LOG.trace("Internal state for session {} updated successfully", session);
304 ctx.notifyRequests();
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);
317 public void close() {
319 if (this.session != null) {
320 this.session.close(TerminationReason.UNKNOWN);
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;
330 LOG.trace("PCEP session {} was not registered.", this.session);
334 protected final synchronized PCEPRequest removeRequest(final S id) {
335 final PCEPRequest ret = this.requests.remove(id);
337 this.listenerState.processRequestStats(ret.getElapsedMillis());
339 LOG.trace("Removed request {} object {}", id, ret);
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);
355 f.addListener((FutureListener<Void>) future -> {
356 if (!future.isSuccess()) {
357 synchronized (AbstractTopologySessionListener.this) {
358 AbstractTopologySessionListener.this.requests.remove(requestId);
360 req.done(OperationResults.UNSENT);
361 LOG.info("Failed to send request {}, instruction cancelled", requestId, future.cause());
364 LOG.trace("Request {} sent to peer (object {})", requestId, req);
368 return req.getFuture();
371 private void setupTimeoutHandler(final S requestId, final PCEPRequest req, final int timeout) {
372 final Timer timer = req.getTimer();
373 timer.schedule(new TimerTask() {
376 synchronized (AbstractTopologySessionListener.this) {
377 AbstractTopologySessionListener.this.requests.remove(requestId);
380 LOG.info("Request {} timed-out waiting for response", requestId);
382 }, TimeUnit.SECONDS.toMillis(timeout));
383 LOG.trace("Set up response timeout handler for request {}", requestId);
387 * Update an LSP in the data store
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
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) {
400 if (lspName == null) {
401 name = this.lsps.get(id);
403 LOG.error("PLSPID {} seen for the first time, not reporting the LSP", id);
410 LOG.debug("Saved LSP {} with name {}", id, name);
411 this.lsps.put(id, name);
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);
424 rlb.setPath(updatedPaths);
426 rlb.setKey(new ReportedLspKey(name));
429 // If this is an unsolicited update. We need to make sure we retain the metadata already present
431 this.nodeState.setLspMetadata(name, rlb.getMetadata());
433 rlb.setMetadata(this.nodeState.getLspMetadata(name));
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);
440 this.lspData.put(name, rl);
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);
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);
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());
473 if (reportedLspId.getValue() == 0) {
474 // if lsp-id also 0, remove all paths
475 LOG.debug("Removing all paths.");
476 updatedPaths.clear();
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);
484 LOG.debug("Setting new paths {} to lsp {}", updatedPaths, name);
489 * Indicate that the peer has completed state synchronization.
491 * @param ctx Message context
493 protected final synchronized void stateSynchronizationAchieved(final MessageContext ctx) {
495 LOG.debug("State synchronization achieved while synchronized, not updating state");
499 // Update synchronization flag
501 if(this.triggeredResyncInProcess) {
502 this.triggeredResyncInProcess = false;
504 updatePccNode(ctx, new PathComputationClientBuilder().setStateSync(PccSyncState.Synchronized).build());
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);
511 protected final synchronized void updatePccNode(final MessageContext ctx, final PathComputationClient pcc) {
512 ctx.trans.merge(LogicalDatastoreType.OPERATIONAL, this.pccIdentifier, pcc);
515 protected final InstanceIdentifier<ReportedLsp> lspIdentifier(final String name) {
516 return this.pccIdentifier.child(ReportedLsp.class, new ReportedLspKey(name));
520 * Remove LSP from the database.
522 * @param ctx Message Context
523 * @param id Revision-specific LSP identifier
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);
532 protected abstract void onSessionUp(PCEPSession session, PathComputationClientBuilder pccBuilder);
535 * Perform revision-specific message processing when a message arrives.
537 * @param ctx Message processing context
538 * @param message Protocol message
539 * @return True if the message type is not handle.
541 protected abstract boolean onMessage(MessageContext ctx, Message message);
543 protected final String lookupLspName(final L id) {
544 Preconditions.checkNotNull(id, "ID parameter null.");
545 return this.lsps.get(id);
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.
552 * @param id InstanceIdentifier of the node
553 * @return null if the node does not exists, or operational data
555 protected final synchronized <T extends DataObject> ListenableFuture<Optional<T>> readOperationalData(final InstanceIdentifier<T> id) {
556 if (this.nodeState == null) {
559 return this.nodeState.readOperationalData(id);
562 protected abstract Object validateReportedLsp(final Optional<ReportedLsp> rep, final LspId input);
564 protected abstract void loadLspData(final Node node, final Map<String, ReportedLsp> lspData, final Map<L, String> lsps, final boolean incrementalSynchro);
566 protected final boolean isLspDbPersisted() {
567 if (this.syncOptimization != null) {
568 return this.syncOptimization.isSyncAvoidanceEnabled();
573 protected final boolean isLspDbRetreived() {
574 if (this.syncOptimization != null) {
575 return this.syncOptimization.isDbVersionPresent();
581 * Is Incremental synchronization if LSP-DB-VERSION are included,
582 * LSP-DB-VERSION TLV values doesnt match, and LSP-SYNC-CAPABILITY is enabled
585 protected final boolean isIncrementalSynchro() {
586 if (this.syncOptimization != null) {
587 return this.syncOptimization.isSyncAvoidanceEnabled() && this.syncOptimization.isDeltaSyncEnabled();
592 protected final boolean isTriggeredInitialSynchro() {
593 if (this.syncOptimization != null) {
594 return this.syncOptimization.isTriggeredInitSyncEnabled();
599 protected final boolean isTriggeredReSyncEnabled() {
600 if (this.syncOptimization != null) {
601 return this.syncOptimization.isTriggeredReSyncEnabled();
606 protected final boolean isSynchronized() {
607 if (this.syncOptimization != null) {
608 return this.syncOptimization.doesLspDbMatch();
613 protected SessionListenerState getSessionListenerState() {
614 return this.listenerState;
618 public Integer getDelegatedLspsCount() {
619 return this.lsps.size();
623 public Boolean getSynchronized() {
628 public StatefulMessages getStatefulMessages() {
629 return this.listenerState.getStatefulMessages();
633 public synchronized void resetStats() {
634 this.listenerState.resetStats(this.session);
638 public ReplyTime getReplyTime() {
639 return this.listenerState.getReplyTime();
643 public PeerCapabilities getPeerCapabilities() {
644 return this.listenerState.getPeerCapabilities();
648 public void tearDownSession() {
653 public synchronized SessionState getSessionState() {
654 return this.listenerState.getSessionState(this.session);
658 public synchronized String getPeerId() {
659 return this.session.getPeerPref().getIpAddress();