46004e0faddec1c4e452ade8ac0f52000afa3e0d
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPSessionNegotiator.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl;
9
10 import com.google.common.base.Optional;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelFutureListener;
15 import io.netty.util.concurrent.Promise;
16 import java.net.InetSocketAddress;
17 import java.util.Comparator;
18 import java.util.concurrent.ExecutionException;
19 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
20 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
21 import org.opendaylight.protocol.pcep.impl.PCEPPeerRegistry.SessionReference;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class PCEPSessionNegotiator extends AbstractSessionNegotiator {
27
28     private static final Logger LOG = LoggerFactory.getLogger(PCEPSessionNegotiator.class);
29
30     private static final Comparator<byte[]> COMPARATOR = UnsignedBytes.lexicographicalComparator();
31
32     private final PCEPSessionListenerFactory factory;
33
34     private final AbstractPCEPSessionNegotiatorFactory negFactory;
35     private final PCEPPeerProposal peerProposal;
36
37     public PCEPSessionNegotiator(final Channel channel, final Promise<PCEPSessionImpl> promise, final PCEPSessionListenerFactory factory,
38         final AbstractPCEPSessionNegotiatorFactory negFactory, final PCEPPeerProposal peerProposal) {
39         super(promise, channel);
40         this.factory = factory;
41         this.negFactory = negFactory;
42         this.peerProposal = peerProposal;
43     }
44
45     @Override
46     protected void startNegotiation() throws ExecutionException {
47         final Object lock = this;
48
49         LOG.debug("Bootstrap negotiation for channel {} started", this.channel);
50
51         /*
52          * We have a chance to see if there's a client session already
53          * registered for this client.
54          */
55         final byte[] clientAddress = ((InetSocketAddress) this.channel.remoteAddress()).getAddress().getAddress();
56         final PCEPPeerRegistry sessionReg = this.negFactory.getSessionRegistry();
57
58         synchronized (lock) {
59             if (sessionReg.getSessionReference(clientAddress).isPresent()) {
60                 final byte[] serverAddress = ((InetSocketAddress) this.channel.localAddress()).getAddress().getAddress();
61                 if (COMPARATOR.compare(serverAddress, clientAddress) > 0) {
62                     final Optional<SessionReference> sessionRefMaybe = sessionReg.removeSessionReference(clientAddress);
63                     try {
64                         if (sessionRefMaybe.isPresent()) {
65                             sessionRefMaybe.get().close();
66                         }
67                     } catch (final Exception e) {
68                         LOG.error("Unexpected failure to close old session", e);
69                     }
70                 } else {
71                     negotiationFailed(new IllegalStateException("A conflicting session for address "
72                             + ((InetSocketAddress) this.channel.remoteAddress()).getAddress() + " found."));
73                     return;
74                 }
75             }
76
77             final Short sessionId = sessionReg.nextSession(clientAddress);
78             final AbstractPCEPSessionNegotiator n = this.negFactory.createNegotiator(this.promise, this.factory.getSessionListener(),
79                     this.channel, sessionId, this.peerProposal);
80
81             sessionReg.putSessionReference(clientAddress, new SessionReference() {
82                 @Override
83                 public void close() throws ExecutionException {
84                     try {
85                         sessionReg.releaseSession(clientAddress, sessionId);
86                     } finally {
87                         PCEPSessionNegotiator.this.channel.close();
88                     }
89                 }
90
91                 @Override
92                 public Short getSessionId() {
93                     return sessionId;
94                 }
95             });
96
97             this.channel.closeFuture().addListener((ChannelFutureListener) future -> {
98                 synchronized (lock) {
99                     sessionReg.removeSessionReference(clientAddress);
100                 }
101             });
102
103             LOG.info("Replacing bootstrap negotiator for channel {}", this.channel);
104             this.channel.pipeline().replace(this, "negotiator", n);
105             n.startNegotiation();
106         }
107     }
108
109     @Override
110     protected void handleMessage(final Message msg) {
111         throw new IllegalStateException("Bootstrap negotiator should have been replaced");
112     }
113 }