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 protected void startNegotiation() throws ExecutionException {
42 final Object lock = this;
44 LOG.debug("Bootstrap negotiation for channel {} started", this.channel);
47 * We have a chance to see if there's a client session already
48 * registered for this client.
50 final byte[] clientAddress = ((InetSocketAddress) this.channel.remoteAddress()).getAddress().getAddress();
51 final PCEPPeerRegistry sessionReg = this.negFactory.getSessionRegistry();
54 if (sessionReg.getSessionReference(clientAddress).isPresent()) {
55 final byte[] serverAddress =
56 ((InetSocketAddress) this.channel.localAddress()).getAddress().getAddress();
57 if (COMPARATOR.compare(serverAddress, clientAddress) > 0) {
58 final Optional<SessionReference> sessionRefMaybe = sessionReg.removeSessionReference(clientAddress);
60 if (sessionRefMaybe.isPresent()) {
61 sessionRefMaybe.get().close();
63 } catch (final Exception e) {
64 LOG.error("Unexpected failure to close old session", e);
67 negotiationFailed(new IllegalStateException("A conflicting session for address "
68 + ((InetSocketAddress) this.channel.remoteAddress()).getAddress() + " found."));
73 final Short sessionId = sessionReg.nextSession(clientAddress);
74 final AbstractPCEPSessionNegotiator n = this.negFactory
75 .createNegotiator(this.nfd, this.promise, this.channel, sessionId);
77 sessionReg.putSessionReference(clientAddress, new SessionReference() {
79 public void close() throws ExecutionException {
81 sessionReg.releaseSession(clientAddress, sessionId);
83 PCEPSessionNegotiator.this.channel.close();
88 public Short getSessionId() {
93 this.channel.closeFuture().addListener((ChannelFutureListener) future -> {
95 sessionReg.removeSessionReference(clientAddress);
99 LOG.info("Replacing bootstrap negotiator for channel {}", this.channel);
100 this.channel.pipeline().replace(this, "negotiator", n);
101 n.startNegotiation();
106 protected void handleMessage(final Message msg) {
107 throw new IllegalStateException("Bootstrap negotiator should have been replaced");