use of org.opendaylight.protocol.pcep.impl.PCEPPeerRegistry.SessionReference in project bgpcep by opendaylight.
the class PCEPSessionNegotiator method startNegotiation.
@Override
protected void startNegotiation() throws ExecutionException {
final Object lock = this;
LOG.debug("Bootstrap negotiation for channel {} started", this.channel);
/*
* We have a chance to see if there's a client session already
* registered for this client.
*/
final byte[] clientAddress = ((InetSocketAddress) this.channel.remoteAddress()).getAddress().getAddress();
final PCEPPeerRegistry sessionReg = this.negFactory.getSessionRegistry();
synchronized (lock) {
if (sessionReg.getSessionReference(clientAddress).isPresent()) {
final byte[] serverAddress = ((InetSocketAddress) this.channel.localAddress()).getAddress().getAddress();
if (COMPARATOR.compare(serverAddress, clientAddress) > 0) {
final Optional<SessionReference> sessionRefMaybe = sessionReg.removeSessionReference(clientAddress);
try {
if (sessionRefMaybe.isPresent()) {
sessionRefMaybe.get().close();
}
} catch (final Exception e) {
LOG.error("Unexpected failure to close old session", e);
}
} else {
negotiationFailed(new IllegalStateException("A conflicting session for address " + ((InetSocketAddress) this.channel.remoteAddress()).getAddress() + " found."));
return;
}
}
final Short sessionId = sessionReg.nextSession(clientAddress);
final AbstractPCEPSessionNegotiator n = this.negFactory.createNegotiator(this.nfd, this.promise, this.channel, sessionId);
sessionReg.putSessionReference(clientAddress, new SessionReference() {
@Override
public void close() throws ExecutionException {
try {
sessionReg.releaseSession(clientAddress, sessionId);
} finally {
PCEPSessionNegotiator.this.channel.close();
}
}
@Override
public Short getSessionId() {
return sessionId;
}
});
this.channel.closeFuture().addListener((ChannelFutureListener) future -> {
synchronized (lock) {
sessionReg.removeSessionReference(clientAddress);
}
});
LOG.info("Replacing bootstrap negotiator for channel {}", this.channel);
this.channel.pipeline().replace(this, "negotiator", n);
n.startNegotiation();
}
}
Aggregations