Search in sources :

Example 1 with SessionReference

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();
    }
}
Also used : UnsignedBytes(com.google.common.primitives.UnsignedBytes) Logger(org.slf4j.Logger) Promise(io.netty.util.concurrent.Promise) LoggerFactory(org.slf4j.LoggerFactory) Message(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ExecutionException(java.util.concurrent.ExecutionException) PCEPSessionNegotiatorFactoryDependencies(org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactoryDependencies) SessionReference(org.opendaylight.protocol.pcep.impl.PCEPPeerRegistry.SessionReference) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Optional(java.util.Optional) Comparator(java.util.Comparator) InetSocketAddress(java.net.InetSocketAddress) SessionReference(org.opendaylight.protocol.pcep.impl.PCEPPeerRegistry.SessionReference) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

UnsignedBytes (com.google.common.primitives.UnsignedBytes)1 Channel (io.netty.channel.Channel)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 Promise (io.netty.util.concurrent.Promise)1 InetSocketAddress (java.net.InetSocketAddress)1 Comparator (java.util.Comparator)1 Optional (java.util.Optional)1 ExecutionException (java.util.concurrent.ExecutionException)1 PCEPSessionNegotiatorFactoryDependencies (org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactoryDependencies)1 SessionReference (org.opendaylight.protocol.pcep.impl.PCEPPeerRegistry.SessionReference)1 Message (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1