Move PCEPSessionNegotiator 24/104024/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 15 Jan 2023 11:50:15 +0000 (12:50 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 15 Jan 2023 14:12:20 +0000 (15:12 +0100)
This class is purely-internal negotiation intermediary, which has
a strong relationship wit AbstractPCEPSessionNegotiatorFactory. Rename
it to BootstrapSessionNegotiator and make it a nested class.

JIRA: BGPCEP-962
Change-Id: I0556c43040bfabf0be691b2e70911caf97ebec91
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/AbstractPCEPSessionNegotiatorFactory.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/PCEPSessionNegotiator.java [deleted file]

index 1a07ee291cba2ba0e29367f51e657c37f8b5ff8c..392de1ca122383c585b00db8d622ee3028b4d0da 100644 (file)
@@ -7,12 +7,19 @@
  */
 package org.opendaylight.protocol.pcep.impl;
 
+import com.google.common.primitives.UnsignedBytes;
 import io.netty.channel.Channel;
+import io.netty.channel.ChannelFutureListener;
 import io.netty.util.concurrent.Promise;
+import java.net.InetSocketAddress;
+import java.util.Comparator;
+import java.util.concurrent.ExecutionException;
 import org.opendaylight.protocol.pcep.PCEPSession;
 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactoryDependencies;
 import org.opendaylight.protocol.pcep.SessionNegotiator;
+import org.opendaylight.protocol.pcep.impl.PCEPPeerRegistry.SessionReference;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
 import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -45,10 +52,87 @@ public abstract class AbstractPCEPSessionNegotiatorFactory implements PCEPSessio
             final Channel channel, final Promise<PCEPSession> promise) {
 
         LOG.debug("Instantiating bootstrap negotiator for channel {}", channel);
-        return new PCEPSessionNegotiator(channel, promise, dependencies, this);
+        return new BootstrapSessionNegotiator(channel, promise, dependencies);
     }
 
-    public PCEPPeerRegistry getSessionRegistry() {
-        return sessionRegistry;
+    private final class BootstrapSessionNegotiator extends AbstractSessionNegotiator {
+        private static final Comparator<byte[]> COMPARATOR = UnsignedBytes.lexicographicalComparator();
+
+        private final PCEPSessionNegotiatorFactoryDependencies nfd;
+
+        BootstrapSessionNegotiator(final Channel channel, final Promise<PCEPSession> promise,
+                final PCEPSessionNegotiatorFactoryDependencies dependencies) {
+            super(promise, channel);
+            nfd = dependencies;
+        }
+
+        @Override
+        @SuppressWarnings("checkstyle:IllegalCatch")
+        //similar to bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AbstractBGPSessionNegotiator.java
+        protected void startNegotiation() throws ExecutionException {
+            final Object lock = this;
+
+            LOG.debug("Bootstrap negotiation for channel {} started", channel);
+
+            /*
+             * We have a chance to see if there's a client session already
+             * registered for this client.
+             */
+            final byte[] clientAddress = ((InetSocketAddress) channel.remoteAddress()).getAddress().getAddress();
+
+            synchronized (lock) {
+                if (sessionRegistry.getSessionReference(clientAddress).isPresent()) {
+                    final byte[] serverAddress =
+                        ((InetSocketAddress) channel.localAddress()).getAddress().getAddress();
+                    if (COMPARATOR.compare(serverAddress, clientAddress) > 0) {
+                        sessionRegistry.removeSessionReference(clientAddress).ifPresent(sessionRef -> {
+                            try {
+                                sessionRef.close();
+                            } catch (final Exception e) {
+                                LOG.error("Unexpected failure to close old session", e);
+                            }
+                        });
+                    } else {
+                        negotiationFailed(new IllegalStateException("A conflicting session for address "
+                                + ((InetSocketAddress) channel.remoteAddress()).getAddress() + " found."));
+                        return;
+                    }
+                }
+
+                final Uint8 sessionId = sessionRegistry.nextSession(clientAddress);
+                final AbstractPCEPSessionNegotiator n = createNegotiator(nfd, promise, channel, sessionId);
+
+                sessionRegistry.putSessionReference(clientAddress, new SessionReference() {
+                    @Override
+                    public void close() throws ExecutionException {
+                        try {
+                            sessionRegistry.releaseSession(clientAddress, sessionId);
+                        } finally {
+                            channel.close();
+                        }
+                    }
+
+                    @Override
+                    public Uint8 getSessionId() {
+                        return sessionId;
+                    }
+                });
+
+                channel.closeFuture().addListener((ChannelFutureListener) future -> {
+                    synchronized (lock) {
+                        sessionRegistry.removeSessionReference(clientAddress);
+                    }
+                });
+
+                LOG.info("Replacing bootstrap negotiator for channel {}", channel);
+                channel.pipeline().replace(this, "negotiator", n);
+                n.startNegotiation();
+            }
+        }
+
+        @Override
+        protected void handleMessage(final Message msg) {
+            throw new IllegalStateException("Bootstrap negotiator should have been replaced");
+        }
     }
 }
diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/PCEPSessionNegotiator.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/PCEPSessionNegotiator.java
deleted file mode 100644 (file)
index 9ca55c8..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.pcep.impl;
-
-import com.google.common.primitives.UnsignedBytes;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelFutureListener;
-import io.netty.util.concurrent.Promise;
-import java.net.InetSocketAddress;
-import java.util.Comparator;
-import java.util.Optional;
-import java.util.concurrent.ExecutionException;
-import org.opendaylight.protocol.pcep.PCEPSession;
-import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactoryDependencies;
-import org.opendaylight.protocol.pcep.impl.PCEPPeerRegistry.SessionReference;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
-import org.opendaylight.yangtools.yang.common.Uint8;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class PCEPSessionNegotiator extends AbstractSessionNegotiator {
-    private static final Logger LOG = LoggerFactory.getLogger(PCEPSessionNegotiator.class);
-
-    private static final Comparator<byte[]> COMPARATOR = UnsignedBytes.lexicographicalComparator();
-    private final AbstractPCEPSessionNegotiatorFactory negFactory;
-    private final PCEPSessionNegotiatorFactoryDependencies nfd;
-
-    public PCEPSessionNegotiator(final Channel channel, final Promise<PCEPSession> promise,
-            final PCEPSessionNegotiatorFactoryDependencies dependencies,
-            final AbstractPCEPSessionNegotiatorFactory negFactory) {
-        super(promise, channel);
-        nfd = dependencies;
-        this.negFactory = negFactory;
-    }
-
-    @Override
-    @SuppressWarnings("checkstyle:IllegalCatch")
-    //similar to bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AbstractBGPSessionNegotiator.java
-    protected void startNegotiation() throws ExecutionException {
-        final Object lock = this;
-
-        LOG.debug("Bootstrap negotiation for channel {} started", channel);
-
-        /*
-         * We have a chance to see if there's a client session already
-         * registered for this client.
-         */
-        final byte[] clientAddress = ((InetSocketAddress) channel.remoteAddress()).getAddress().getAddress();
-        final PCEPPeerRegistry sessionReg = negFactory.getSessionRegistry();
-
-        synchronized (lock) {
-            if (sessionReg.getSessionReference(clientAddress).isPresent()) {
-                final byte[] serverAddress =
-                    ((InetSocketAddress) 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) channel.remoteAddress()).getAddress() + " found."));
-                    return;
-                }
-            }
-
-            final Uint8 sessionId = sessionReg.nextSession(clientAddress);
-            final AbstractPCEPSessionNegotiator n = negFactory.createNegotiator(nfd, promise, 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 Uint8 getSessionId() {
-                    return sessionId;
-                }
-            });
-
-            channel.closeFuture().addListener((ChannelFutureListener) future -> {
-                synchronized (lock) {
-                    sessionReg.removeSessionReference(clientAddress);
-                }
-            });
-
-            LOG.info("Replacing bootstrap negotiator for channel {}", channel);
-            channel.pipeline().replace(this, "negotiator", n);
-            n.startNegotiation();
-        }
-    }
-
-    @Override
-    protected void handleMessage(final Message msg) {
-        throw new IllegalStateException("Bootstrap negotiator should have been replaced");
-    }
-}