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.rev130715.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);
277 //cancel not supported, submit empty transaction
282 Futures.addCallback(ctx.trans.submit(), new FutureCallback<Void>() {
284 public void onSuccess(final Void result) {
285 LOG.trace("Internal state for session {} updated successfully", session);
286 ctx.notifyRequests();
290 public void onFailure(final Throwable t) {
291 LOG.error("Failed to update internal state for session {}, closing it", session, t);
292 ctx.notifyRequests();
293 session.close(TerminationReason.UNKNOWN);
299 public void close() {
301 if (this.session != null) {
302 this.session.close(TerminationReason.UNKNOWN);
306 private synchronized void unregister() {
307 if (this.registration != null) {
308 this.registration.close();
309 this.registration = null;
313 protected final synchronized PCEPRequest removeRequest(final S id) {
314 final PCEPRequest ret = this.requests.remove(id);
316 this.listenerState.processRequestStats(ret.getElapsedMillis());
318 LOG.trace("Removed request {} object {}", id, ret);
322 protected final synchronized ListenableFuture<OperationResult> sendMessage(final Message message, final S requestId,
323 final Metadata metadata) {
324 final io.netty.util.concurrent.Future<Void> f = this.session.sendMessage(message);
325 this.listenerState.updateStatefulSentMsg(message);
326 final PCEPRequest req = new PCEPRequest(metadata);
327 this.requests.put(requestId, req);
328 final int rpcTimeout = serverSessionManager.getRpcTimeout();
329 LOG.trace("RPC response timeout value is {} seconds", rpcTimeout);
330 if (rpcTimeout > 0) {
331 setupTimeoutHandler(requestId, req, rpcTimeout);
334 f.addListener(new FutureListener<Void>() {
336 public void operationComplete(final io.netty.util.concurrent.Future<Void> future) {
337 if (!future.isSuccess()) {
338 synchronized (AbstractTopologySessionListener.this) {
339 AbstractTopologySessionListener.this.requests.remove(requestId);
341 req.done(OperationResults.UNSENT);
342 LOG.info("Failed to send request {}, instruction cancelled", requestId, future.cause());
345 LOG.trace("Request {} sent to peer (object {})", requestId, req);
350 return req.getFuture();
353 private void setupTimeoutHandler(final S requestId, final PCEPRequest req, final int timeout) {
354 final Timer timer = req.getTimer();
355 timer.schedule(new TimerTask() {
358 synchronized (AbstractTopologySessionListener.this) {
359 AbstractTopologySessionListener.this.requests.remove(requestId);
362 LOG.info("Request {} timed-out waiting for response", requestId);
364 }, TimeUnit.SECONDS.toMillis(timeout));
365 LOG.trace("Set up response timeout handler for request {}", requestId);
369 * Update an LSP in the data store
371 * @param ctx Message context
372 * @param id Revision-specific LSP identifier
373 * @param lspName LSP name
374 * @param rlb Reported LSP builder
375 * @param solicited True if the update was solicited
376 * @param remove True if this is an LSP path removal
378 protected final synchronized void updateLsp(final MessageContext ctx, final L id, final String lspName,
379 final ReportedLspBuilder rlb, final boolean solicited, final boolean remove) {
382 if (lspName == null) {
383 name = this.lsps.get(id);
385 LOG.error("PLSPID {} seen for the first time, not reporting the LSP", id);
392 LOG.debug("Saved LSP {} with name {}", id, name);
393 this.lsps.put(id, name);
396 final ReportedLsp previous = this.lspData.get(name);
397 // if no previous report about the lsp exist, just proceed
398 if (previous != null) {
399 final List<Path> updatedPaths = makeBeforeBreak(rlb, previous, name, remove);
400 // if all paths or the last path were deleted, delete whole tunnel
401 if (updatedPaths == null || updatedPaths.isEmpty()) {
402 LOG.debug("All paths were removed, removing LSP with {}.", id);
406 rlb.setPath(updatedPaths);
408 rlb.setKey(new ReportedLspKey(name));
411 // If this is an unsolicited update. We need to make sure we retain the metadata already present
413 this.nodeState.setLspMetadata(name, rlb.getMetadata());
415 rlb.setMetadata(this.nodeState.getLspMetadata(name));
418 final ReportedLsp rl = rlb.build();
419 ctx.trans.put(LogicalDatastoreType.OPERATIONAL, this.pccIdentifier.child(ReportedLsp.class, rlb.getKey()), rl);
420 LOG.debug("LSP {} updated to MD-SAL", name);
422 this.lspData.put(name, rl);
425 private List<Path> makeBeforeBreak(final ReportedLspBuilder rlb, final ReportedLsp previous, final String name, final boolean remove) {
426 // just one path should be reported
427 Preconditions.checkState(rlb.getPath().size() == 1);
428 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId reportedLspId = rlb.getPath().get(0).getLspId();
429 final List<Path> updatedPaths;
430 //lspId = 0 and remove = false -> tunnel is down, still exists but no path is signaled
431 //remove existing tunnel's paths now, as explicit path remove will not come
432 if (!remove && reportedLspId.getValue() == 0) {
433 updatedPaths = new ArrayList<>();
434 LOG.debug("Remove previous paths {} to this lsp name {}", previous.getPath(), name);
436 // check previous report for existing paths
437 updatedPaths = new ArrayList<>(previous.getPath());
438 LOG.debug("Found previous paths {} to this lsp name {}", updatedPaths, name);
439 for (final Path path : previous.getPath()) {
440 //we found reported path in previous reports
441 if (path.getLspId().getValue() == 0 || path.getLspId().equals(reportedLspId)) {
442 LOG.debug("Match on lsp-id {}", path.getLspId().getValue() );
443 // path that was reported previously and does have the same lsp-id, path will be updated
444 final boolean r = updatedPaths.remove(path);
445 LOG.trace("Request removed? {}", r);
449 // if the path does not exist in previous report, add it to path list, it's a new ERO
450 // only one path will be added
451 //lspId is 0 means confirmation message that shouldn't be added (because we have no means of deleting it later)
452 LOG.trace("Adding new path {} to {}", rlb.getPath(), updatedPaths);
453 updatedPaths.addAll(rlb.getPath());
455 if (reportedLspId.getValue() == 0) {
456 // if lsp-id also 0, remove all paths
457 LOG.debug("Removing all paths.");
458 updatedPaths.clear();
460 // path is marked to be removed
461 LOG.debug("Removing path {} from {}", rlb.getPath(), updatedPaths);
462 final boolean r = updatedPaths.removeAll(rlb.getPath());
463 LOG.trace("Request removed? {}", r);
466 LOG.debug("Setting new paths {} to lsp {}", updatedPaths, name);
471 * Indicate that the peer has completed state synchronization.
473 * @param ctx Message context
475 protected final synchronized void stateSynchronizationAchieved(final MessageContext ctx) {
477 LOG.debug("State synchronization achieved while synchronized, not updating state");
481 // Update synchronization flag
483 if(this.triggeredResyncInProcess) {
484 this.triggeredResyncInProcess = false;
486 updatePccNode(ctx, new PathComputationClientBuilder().setStateSync(PccSyncState.Synchronized).build());
488 // The node has completed synchronization, cleanup metadata no longer reported back
489 this.nodeState.cleanupExcept(this.lsps.values());
490 LOG.debug("Session {} achieved synchronized state", this.session);
493 protected final synchronized void updatePccNode(final MessageContext ctx, final PathComputationClient pcc) {
494 ctx.trans.merge(LogicalDatastoreType.OPERATIONAL, this.pccIdentifier, pcc);
497 protected final InstanceIdentifier<ReportedLsp> lspIdentifier(final String name) {
498 return this.pccIdentifier.child(ReportedLsp.class, new ReportedLspKey(name));
502 * Remove LSP from the database.
504 * @param ctx Message Context
505 * @param id Revision-specific LSP identifier
507 protected final synchronized void removeLsp(final MessageContext ctx, final L id) {
508 final String name = this.lsps.remove(id);
509 LOG.debug("LSP {} removed", name);
510 ctx.trans.delete(LogicalDatastoreType.OPERATIONAL, lspIdentifier(name));
511 this.lspData.remove(name);
514 protected abstract void onSessionUp(PCEPSession session, PathComputationClientBuilder pccBuilder);
517 * Perform revision-specific message processing when a message arrives.
519 * @param ctx Message processing context
520 * @param message Protocol message
521 * @return True if the message type is not handle.
523 protected abstract boolean onMessage(MessageContext ctx, Message message);
525 protected final String lookupLspName(final L id) {
526 Preconditions.checkNotNull(id, "ID parameter null.");
527 return this.lsps.get(id);
531 * Reads operational data on this node. Doesn't attempt to read the data,
532 * if the node does not exist. In this case returns null.
534 * @param id InstanceIdentifier of the node
535 * @return null if the node does not exists, or operational data
537 protected final synchronized <T extends DataObject> ListenableFuture<Optional<T>> readOperationalData(final InstanceIdentifier<T> id) {
538 if (this.nodeState == null) {
541 return this.nodeState.readOperationalData(id);
544 protected abstract Object validateReportedLsp(final Optional<ReportedLsp> rep, final LspId input);
546 protected abstract void loadLspData(final Node node, final Map<String, ReportedLsp> lspData, final Map<L, String> lsps, final boolean incrementalSynchro);
548 protected final boolean isLspDbPersisted() {
549 if (syncOptimization != null) {
550 return syncOptimization.isSyncAvoidanceEnabled();
555 protected final boolean isLspDbRetreived() {
556 if (syncOptimization != null) {
557 return syncOptimization.isDbVersionPresent();
563 * Is Incremental synchronization if LSP-DB-VERSION are included,
564 * LSP-DB-VERSION TLV values doesnt match, and LSP-SYNC-CAPABILITY is enabled
567 protected final boolean isIncrementalSynchro() {
568 if (syncOptimization != null) {
569 return syncOptimization.isSyncAvoidanceEnabled() && syncOptimization.isDeltaSyncEnabled();
574 protected final boolean isTriggeredInitialSynchro() {
575 if (syncOptimization != null) {
576 return syncOptimization.isTriggeredInitSyncEnabled();
581 protected final boolean isTriggeredReSyncEnabled() {
582 if (syncOptimization != null) {
583 return syncOptimization.isTriggeredReSyncEnabled();
588 protected final boolean isSynchronized() {
589 if (syncOptimization != null) {
590 return syncOptimization.doesLspDbMatch();
595 protected SessionListenerState getSessionListenerState() {
596 return this.listenerState;
600 public Integer getDelegatedLspsCount() {
601 return this.lsps.size();
605 public Boolean getSynchronized() {
610 public StatefulMessages getStatefulMessages() {
611 return this.listenerState.getStatefulMessages();
615 public synchronized void resetStats() {
616 this.listenerState.resetStats(this.session);
620 public ReplyTime getReplyTime() {
621 return this.listenerState.getReplyTime();
625 public PeerCapabilities getPeerCapabilities() {
626 return this.listenerState.getPeerCapabilities();
630 public void tearDownSession() {
635 public synchronized SessionState getSessionState() {
636 return this.listenerState.getSessionState(this.session);
640 public synchronized String getPeerId() {
641 return this.session.getPeerPref().getIpAddress();