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;
27 import javax.annotation.concurrent.GuardedBy;
28 import org.opendaylight.controller.config.yang.pcep.topology.provider.ListenerStateRuntimeMXBean;
29 import org.opendaylight.controller.config.yang.pcep.topology.provider.ListenerStateRuntimeRegistration;
30 import org.opendaylight.controller.config.yang.pcep.topology.provider.PeerCapabilities;
31 import org.opendaylight.controller.config.yang.pcep.topology.provider.ReplyTime;
32 import org.opendaylight.controller.config.yang.pcep.topology.provider.SessionState;
33 import org.opendaylight.controller.config.yang.pcep.topology.provider.StatefulMessages;
34 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
35 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
36 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
37 import org.opendaylight.protocol.pcep.PCEPSession;
38 import org.opendaylight.protocol.pcep.PCEPSessionListener;
39 import org.opendaylight.protocol.pcep.PCEPTerminationReason;
40 import org.opendaylight.protocol.pcep.TerminationReason;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddressBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.MessageHeader;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.LspId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1Builder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.PccSyncState;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.lsp.metadata.Metadata;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClient;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClientBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLspKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.reported.lsp.Path;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
59 import org.opendaylight.yangtools.yang.binding.DataContainer;
60 import org.opendaylight.yangtools.yang.binding.DataObject;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * Base class for PCEP topology providers. It handles the common tasks involved in managing a PCEP server (PCE)
67 * endpoint, and exposing a network topology based on it. It needs to be subclassed to form a fully functional block,
68 * where the subclass provides handling of incoming messages.
70 * @param <S> identifier type of requests
71 * @param <L> identifier type for LSPs
73 public abstract class AbstractTopologySessionListener<S, L> implements PCEPSessionListener, TopologySessionListener, ListenerStateRuntimeMXBean {
74 protected static final class MessageContext {
75 private final Collection<PCEPRequest> requests = new ArrayList<>();
76 private final WriteTransaction trans;
78 private MessageContext(final WriteTransaction trans) {
79 this.trans = Preconditions.checkNotNull(trans);
82 void resolveRequest(final PCEPRequest req) {
83 this.requests.add(req);
86 private void notifyRequests() {
87 for (final PCEPRequest r : this.requests) {
88 r.done(OperationResults.SUCCESS);
93 protected static final MessageHeader MESSAGE_HEADER = new MessageHeader() {
94 private final ProtocolVersion version = new ProtocolVersion((short) 1);
97 public Class<? extends DataContainer> getImplementedInterface() {
98 return MessageHeader.class;
102 public ProtocolVersion getVersion() {
107 private static final Logger LOG = LoggerFactory.getLogger(AbstractTopologySessionListener.class);
109 protected static final String MISSING_XML_TAG = "Mandatory XML tags are missing.";
112 private final Map<S, PCEPRequest> requests = new HashMap<>();
115 private final Map<String, ReportedLsp> lspData = new HashMap<>();
118 protected final Map<L, String> lsps = new HashMap<>();
120 private final ServerSessionManager serverSessionManager;
121 private InstanceIdentifier<PathComputationClient> pccIdentifier;
122 private TopologyNodeState nodeState;
123 private boolean synced = false;
124 private PCEPSession session;
125 private SyncOptimization syncOptimization;
126 private boolean triggeredResyncInProcess;
128 private ListenerStateRuntimeRegistration registration;
129 private final SessionListenerState listenerState;
131 protected AbstractTopologySessionListener(final ServerSessionManager serverSessionManager) {
132 this.serverSessionManager = Preconditions.checkNotNull(serverSessionManager);
133 this.listenerState = new SessionListenerState();
137 public final synchronized void onSessionUp(final PCEPSession session) {
139 * The session went up. Look up the router in Inventory model,
140 * create it if it is not there (marking that fact for later
141 * deletion), and mark it as synchronizing. Also create it in
142 * the topology model, with empty LSP list.
144 final InetAddress peerAddress = session.getRemoteAddress();
146 syncOptimization = new SyncOptimization(session);
148 final TopologyNodeState state = this.serverSessionManager.takeNodeState(peerAddress, this, isLspDbRetreived());
150 this.session = session;
151 this.nodeState = state;
153 LOG.trace("Peer {} resolved to topology node {}", peerAddress, state.getNodeId());
155 // Our augmentation in the topology node
156 final PathComputationClientBuilder pccBuilder = new PathComputationClientBuilder();
158 onSessionUp(session, pccBuilder);
159 this.synced = isSynchronized();
161 pccBuilder.setIpAddress(IpAddressBuilder.getDefaultInstance(peerAddress.getHostAddress()));
162 final InstanceIdentifier<Node1> topologyAugment = state.getNodeId().augmentation(Node1.class);
163 this.pccIdentifier = topologyAugment.child(PathComputationClient.class);
164 final Node initialNodeState = state.getInitialNodeState();
165 final boolean isNodePresent = isLspDbRetreived() && initialNodeState != null;
167 loadLspData(initialNodeState, lspData, lsps, isIncrementalSynchro());
168 pccBuilder.setReportedLsp(initialNodeState.getAugmentation(Node1.class).getPathComputationClient().getReportedLsp());
170 writeNode(pccBuilder, state, topologyAugment);
171 this.listenerState.init(session);
172 if (this.serverSessionManager.getRuntimeRootRegistration().isPresent()) {
173 this.registration = this.serverSessionManager.getRuntimeRootRegistration().get().register(this);
175 LOG.info("Session with {} attached to topology node {}", session.getRemoteAddress(), state.getNodeId());
178 private void writeNode(final PathComputationClientBuilder pccBuilder, final TopologyNodeState state,
179 final InstanceIdentifier<Node1> topologyAugment) {
180 final Node1 ta = new Node1Builder().setPathComputationClient(pccBuilder.build()).build();
182 final ReadWriteTransaction trans = state.rwTransaction();
183 trans.put(LogicalDatastoreType.OPERATIONAL, topologyAugment, ta);
184 LOG.trace("Peer data {} set to {}", topologyAugment, ta);
186 // All set, commit the modifications
187 Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
189 public void onSuccess(final Void result) {
190 LOG.trace("Internal state for session {} updated successfully", session);
194 public void onFailure(final Throwable t) {
195 LOG.error("Failed to update internal state for session {}, terminating it", session, t);
196 session.close(TerminationReason.UNKNOWN);
201 protected void updatePccState(final PccSyncState pccSyncState) {
202 final MessageContext ctx = new MessageContext(this.nodeState.beginTransaction());
203 updatePccNode(ctx, new PathComputationClientBuilder().setStateSync(pccSyncState).build());
204 if (pccSyncState != PccSyncState.Synchronized) {
206 this.triggeredResyncInProcess = true;
208 // All set, commit the modifications
209 Futures.addCallback(ctx.trans.submit(), new FutureCallback<Void>() {
211 public void onSuccess(final Void result) {
212 LOG.trace("Internal state for session {} updated successfully", session);
216 public void onFailure(final Throwable t) {
217 LOG.error("Failed to update internal state for session {}", session, t);
218 session.close(TerminationReason.UNKNOWN);
223 protected boolean isTriggeredSyncInProcess() {
224 return this.triggeredResyncInProcess;
228 private void tearDown(final PCEPSession session) {
229 this.serverSessionManager.releaseNodeState(this.nodeState, session, isLspDbPersisted());
230 this.nodeState = null;
232 this.syncOptimization = null;
235 // Clear all requests we know about
236 for (final Entry<S, PCEPRequest> e : this.requests.entrySet()) {
237 final PCEPRequest r = e.getValue();
238 switch (r.getState()) {
240 // Done is done, nothing to do
243 // Peer has not acked: results in failure
244 LOG.info("Request {} was incomplete when session went down, failing the instruction", e.getKey());
245 r.done(OperationResults.NOACK);
248 // Peer has not been sent to the peer: results in cancellation
249 LOG.debug("Request {} was not sent when session went down, cancelling the instruction", e.getKey());
250 r.done(OperationResults.UNSENT);
256 this.requests.clear();
260 public final synchronized void onSessionDown(final PCEPSession session, final Exception e) {
261 LOG.warn("Session {} went down unexpectedly", session, e);
266 public final synchronized void onSessionTerminated(final PCEPSession session, final PCEPTerminationReason reason) {
267 LOG.info("Session {} terminated by peer with reason {}", session, reason);
272 public final synchronized void onMessage(final PCEPSession session, final Message message) {
273 final MessageContext ctx = new MessageContext(this.nodeState.beginTransaction());
275 if (onMessage(ctx, message)) {
276 LOG.info("Unhandled message {} on session {}", message, session);
280 Futures.addCallback(ctx.trans.submit(), new FutureCallback<Void>() {
282 public void onSuccess(final Void result) {
283 LOG.trace("Internal state for session {} updated successfully", session);
284 ctx.notifyRequests();
288 public void onFailure(final Throwable t) {
289 LOG.error("Failed to update internal state for session {}, closing it", session, t);
290 ctx.notifyRequests();
291 session.close(TerminationReason.UNKNOWN);
297 public void close() {
299 if (this.session != null) {
300 this.session.close(TerminationReason.UNKNOWN);
304 private synchronized void unregister() {
305 if (this.registration != null) {
306 this.registration.close();
307 this.registration = null;
311 protected final synchronized PCEPRequest removeRequest(final S id) {
312 final PCEPRequest ret = this.requests.remove(id);
314 this.listenerState.processRequestStats(ret.getElapsedMillis());
316 LOG.trace("Removed request {} object {}", id, ret);
320 protected final synchronized ListenableFuture<OperationResult> sendMessage(final Message message, final S requestId,
321 final Metadata metadata) {
322 final io.netty.util.concurrent.Future<Void> f = this.session.sendMessage(message);
323 this.listenerState.updateStatefulSentMsg(message);
324 final PCEPRequest req = new PCEPRequest(metadata);
325 this.requests.put(requestId, req);
326 final int rpcTimeout = serverSessionManager.getRpcTimeout();
327 LOG.trace("RPC response timeout value is {} seconds", rpcTimeout);
328 if (rpcTimeout > 0) {
329 setupTimeoutHandler(requestId, req, rpcTimeout);
332 f.addListener(new FutureListener<Void>() {
334 public void operationComplete(final io.netty.util.concurrent.Future<Void> future) {
335 if (!future.isSuccess()) {
336 synchronized (AbstractTopologySessionListener.this) {
337 AbstractTopologySessionListener.this.requests.remove(requestId);
339 req.done(OperationResults.UNSENT);
340 LOG.info("Failed to send request {}, instruction cancelled", requestId, future.cause());
343 LOG.trace("Request {} sent to peer (object {})", requestId, req);
348 return req.getFuture();
351 private void setupTimeoutHandler(final S requestId, final PCEPRequest req, final int timeout) {
352 final Timer timer = req.getTimer();
353 timer.schedule(new TimerTask() {
356 synchronized (AbstractTopologySessionListener.this) {
357 AbstractTopologySessionListener.this.requests.remove(requestId);
360 LOG.info("Request {} timed-out waiting for response", requestId);
362 }, TimeUnit.SECONDS.toMillis(timeout));
363 LOG.trace("Set up response timeout handler for request {}", requestId);
367 * Update an LSP in the data store
369 * @param ctx Message context
370 * @param id Revision-specific LSP identifier
371 * @param lspName LSP name
372 * @param rlb Reported LSP builder
373 * @param solicited True if the update was solicited
374 * @param remove True if this is an LSP path removal
376 protected final synchronized void updateLsp(final MessageContext ctx, final L id, final String lspName,
377 final ReportedLspBuilder rlb, final boolean solicited, final boolean remove) {
380 if (lspName == null) {
381 name = this.lsps.get(id);
383 LOG.error("PLSPID {} seen for the first time, not reporting the LSP", id);
390 LOG.debug("Saved LSP {} with name {}", id, name);
391 this.lsps.put(id, name);
394 final ReportedLsp previous = this.lspData.get(name);
395 // if no previous report about the lsp exist, just proceed
396 if (previous != null) {
397 final List<Path> updatedPaths = makeBeforeBreak(rlb, previous, name, remove);
398 // if all paths or the last path were deleted, delete whole tunnel
399 if (updatedPaths == null || updatedPaths.isEmpty()) {
400 LOG.debug("All paths were removed, removing LSP with {}.", id);
404 rlb.setPath(updatedPaths);
406 rlb.setKey(new ReportedLspKey(name));
409 // If this is an unsolicited update. We need to make sure we retain the metadata already present
411 this.nodeState.setLspMetadata(name, rlb.getMetadata());
413 rlb.setMetadata(this.nodeState.getLspMetadata(name));
416 final ReportedLsp rl = rlb.build();
417 ctx.trans.put(LogicalDatastoreType.OPERATIONAL, this.pccIdentifier.child(ReportedLsp.class, rlb.getKey()), rl);
418 LOG.debug("LSP {} updated to MD-SAL", name);
420 this.lspData.put(name, rl);
423 private List<Path> makeBeforeBreak(final ReportedLspBuilder rlb, final ReportedLsp previous, final String name, final boolean remove) {
424 // just one path should be reported
425 Preconditions.checkState(rlb.getPath().size() == 1);
426 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId reportedLspId = rlb.getPath().get(0).getLspId();
427 final List<Path> updatedPaths;
428 //lspId = 0 and remove = false -> tunnel is down, still exists but no path is signaled
429 //remove existing tunnel's paths now, as explicit path remove will not come
430 if (!remove && reportedLspId.getValue() == 0) {
431 updatedPaths = new ArrayList<>();
432 LOG.debug("Remove previous paths {} to this lsp name {}", previous.getPath(), name);
434 // check previous report for existing paths
435 updatedPaths = new ArrayList<>(previous.getPath());
436 LOG.debug("Found previous paths {} to this lsp name {}", updatedPaths, name);
437 for (final Path path : previous.getPath()) {
438 //we found reported path in previous reports
439 if (path.getLspId().getValue() == 0 || path.getLspId().equals(reportedLspId)) {
440 LOG.debug("Match on lsp-id {}", path.getLspId().getValue() );
441 // path that was reported previously and does have the same lsp-id, path will be updated
442 final boolean r = updatedPaths.remove(path);
443 LOG.trace("Request removed? {}", r);
447 // if the path does not exist in previous report, add it to path list, it's a new ERO
448 // only one path will be added
449 //lspId is 0 means confirmation message that shouldn't be added (because we have no means of deleting it later)
450 LOG.trace("Adding new path {} to {}", rlb.getPath(), updatedPaths);
451 updatedPaths.addAll(rlb.getPath());
453 if (reportedLspId.getValue() == 0) {
454 // if lsp-id also 0, remove all paths
455 LOG.debug("Removing all paths.");
456 updatedPaths.clear();
458 // path is marked to be removed
459 LOG.debug("Removing path {} from {}", rlb.getPath(), updatedPaths);
460 final boolean r = updatedPaths.removeAll(rlb.getPath());
461 LOG.trace("Request removed? {}", r);
464 LOG.debug("Setting new paths {} to lsp {}", updatedPaths, name);
469 * Indicate that the peer has completed state synchronization.
471 * @param ctx Message context
473 protected final synchronized void stateSynchronizationAchieved(final MessageContext ctx) {
475 LOG.debug("State synchronization achieved while synchronized, not updating state");
479 // Update synchronization flag
481 if(this.triggeredResyncInProcess) {
482 this.triggeredResyncInProcess = false;
484 updatePccNode(ctx, new PathComputationClientBuilder().setStateSync(PccSyncState.Synchronized).build());
486 // The node has completed synchronization, cleanup metadata no longer reported back
487 this.nodeState.cleanupExcept(this.lsps.values());
488 LOG.debug("Session {} achieved synchronized state", this.session);
491 protected final synchronized void updatePccNode(final MessageContext ctx, final PathComputationClient pcc) {
492 ctx.trans.merge(LogicalDatastoreType.OPERATIONAL, this.pccIdentifier, pcc);
495 protected final InstanceIdentifier<ReportedLsp> lspIdentifier(final String name) {
496 return this.pccIdentifier.child(ReportedLsp.class, new ReportedLspKey(name));
499 protected final InstanceIdentifier<PathComputationClient> getPccIdentifier() {
500 return this.pccIdentifier;
504 * Remove LSP from the database.
506 * @param ctx Message Context
507 * @param id Revision-specific LSP identifier
509 protected final synchronized void removeLsp(final MessageContext ctx, final L id) {
510 final String name = this.lsps.remove(id);
511 LOG.debug("LSP {} removed", name);
512 ctx.trans.delete(LogicalDatastoreType.OPERATIONAL, lspIdentifier(name));
513 this.lspData.remove(name);
516 protected abstract void onSessionUp(PCEPSession session, PathComputationClientBuilder pccBuilder);
519 * Perform revision-specific message processing when a message arrives.
521 * @param ctx Message processing context
522 * @param message Protocol message
523 * @return True if the message type is not handle.
525 protected abstract boolean onMessage(MessageContext ctx, Message message);
527 protected final String lookupLspName(final L id) {
528 Preconditions.checkNotNull(id, "ID parameter null.");
529 return this.lsps.get(id);
533 * Reads operational data on this node. Doesn't attempt to read the data,
534 * if the node does not exist. In this case returns null.
536 * @param id InstanceIdentifier of the node
537 * @return null if the node does not exists, or operational data
539 protected final synchronized <T extends DataObject> ListenableFuture<Optional<T>> readOperationalData(final InstanceIdentifier<T> id) {
540 if (this.nodeState == null) {
543 return this.nodeState.readOperationalData(id);
546 protected abstract Object validateReportedLsp(final Optional<ReportedLsp> rep, final LspId input);
548 protected abstract void loadLspData(final Node node, final Map<String, ReportedLsp> lspData, final Map<L, String> lsps, final boolean incrementalSynchro);
550 protected final boolean isLspDbPersisted() {
551 if (syncOptimization != null) {
552 return syncOptimization.isSyncAvoidanceEnabled();
557 protected final boolean isLspDbRetreived() {
558 if (syncOptimization != null) {
559 return syncOptimization.isDbVersionPresent();
565 * Is Incremental synchronization if LSP-DB-VERSION are included,
566 * LSP-DB-VERSION TLV values doesnt match, and LSP-SYNC-CAPABILITY is enabled
569 protected final boolean isIncrementalSynchro() {
570 if (syncOptimization != null) {
571 return syncOptimization.isSyncAvoidanceEnabled() && syncOptimization.isDeltaSyncEnabled();
576 protected final boolean isTriggeredInitialSynchro() {
577 if (syncOptimization != null) {
578 return syncOptimization.isTriggeredInitSyncEnabled();
583 protected final boolean isTriggeredReSyncEnabled() {
584 if (syncOptimization != null) {
585 return syncOptimization.isTriggeredReSyncEnabled();
590 protected final boolean isSynchronized() {
591 if (syncOptimization != null) {
592 return syncOptimization.doesLspDbMatch();
597 protected SessionListenerState getSessionListenerState() {
598 return this.listenerState;
602 public Integer getDelegatedLspsCount() {
603 return this.lsps.size();
607 public Boolean getSynchronized() {
612 public StatefulMessages getStatefulMessages() {
613 return this.listenerState.getStatefulMessages();
617 public synchronized void resetStats() {
618 this.listenerState.resetStats(this.session);
622 public ReplyTime getReplyTime() {
623 return this.listenerState.getReplyTime();
627 public PeerCapabilities getPeerCapabilities() {
628 return this.listenerState.getPeerCapabilities();
632 public void tearDownSession() {
637 public synchronized SessionState getSessionState() {
638 return this.listenerState.getSessionState(this.session);
642 public synchronized String getPeerId() {
643 return this.session.getPeerPref().getIpAddress();