2 * Copyright (c) 2014 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.protocol.pcep.impl;
10 import com.google.common.primitives.UnsignedBytes;
11 import io.netty.channel.Channel;
12 import io.netty.channel.ChannelFutureListener;
13 import io.netty.util.concurrent.Promise;
14 import java.net.InetSocketAddress;
15 import java.util.Comparator;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactoryDependencies;
19 import org.opendaylight.protocol.pcep.impl.PCEPPeerRegistry.SessionReference;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class PCEPSessionNegotiator extends AbstractSessionNegotiator {
26 private static final Logger LOG = LoggerFactory.getLogger(PCEPSessionNegotiator.class);
28 private static final Comparator<byte[]> COMPARATOR = UnsignedBytes.lexicographicalComparator();
29 private final AbstractPCEPSessionNegotiatorFactory negFactory;
30 private final PCEPSessionNegotiatorFactoryDependencies nfd;
32 public PCEPSessionNegotiator(final Channel channel, final Promise<PCEPSessionImpl> promise,
33 final PCEPSessionNegotiatorFactoryDependencies dependencies,
34 final AbstractPCEPSessionNegotiatorFactory negFactory) {
35 super(promise, channel);
36 this.nfd = dependencies;
37 this.negFactory = negFactory;
41 @SuppressWarnings("checkstyle:IllegalCatch")
42 //similar to bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AbstractBGPSessionNegotiator.java
43 protected void startNegotiation() throws ExecutionException {
44 final Object lock = this;
46 LOG.debug("Bootstrap negotiation for channel {} started", this.channel);
49 * We have a chance to see if there's a client session already
50 * registered for this client.
52 final byte[] clientAddress = ((InetSocketAddress) this.channel.remoteAddress()).getAddress().getAddress();
53 final PCEPPeerRegistry sessionReg = this.negFactory.getSessionRegistry();
56 if (sessionReg.getSessionReference(clientAddress).isPresent()) {
57 final byte[] serverAddress =
58 ((InetSocketAddress) this.channel.localAddress()).getAddress().getAddress();
59 if (COMPARATOR.compare(serverAddress, clientAddress) > 0) {
60 final Optional<SessionReference> sessionRefMaybe = sessionReg.removeSessionReference(clientAddress);
62 if (sessionRefMaybe.isPresent()) {
63 sessionRefMaybe.get().close();
65 } catch (final Exception e) {
66 LOG.error("Unexpected failure to close old session", e);
69 negotiationFailed(new IllegalStateException("A conflicting session for address "
70 + ((InetSocketAddress) this.channel.remoteAddress()).getAddress() + " found."));
75 final Short sessionId = sessionReg.nextSession(clientAddress);
76 final AbstractPCEPSessionNegotiator n = this.negFactory
77 .createNegotiator(this.nfd, this.promise, this.channel, sessionId);
79 sessionReg.putSessionReference(clientAddress, new SessionReference() {
81 public void close() throws ExecutionException {
83 sessionReg.releaseSession(clientAddress, sessionId);
85 PCEPSessionNegotiator.this.channel.close();
90 public Short getSessionId() {
95 this.channel.closeFuture().addListener((ChannelFutureListener) future -> {
97 sessionReg.removeSessionReference(clientAddress);
101 LOG.info("Replacing bootstrap negotiator for channel {}", this.channel);
102 this.channel.pipeline().replace(this, "negotiator", n);
103 n.startNegotiation();
108 protected void handleMessage(final Message msg) {
109 throw new IllegalStateException("Bootstrap negotiator should have been replaced");