Replaced slice() with readSlice() where appropriate.
[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.framework.AbstractSessionNegotiator;
20 import org.opendaylight.protocol.framework.SessionListenerFactory;
21 import org.opendaylight.protocol.pcep.PCEPSessionListener;
22 import org.opendaylight.protocol.pcep.impl.PCEPPeerRegistry.SessionReference;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class PCEPSessionNegotiator extends AbstractSessionNegotiator<Message, PCEPSessionImpl> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(PCEPSessionNegotiator.class);
30
31     private static final Comparator<byte[]> COMPARATOR = UnsignedBytes.lexicographicalComparator();
32
33     private final Channel channel;
34
35     private final Promise<PCEPSessionImpl> promise;
36
37     private final SessionListenerFactory<PCEPSessionListener> factory;
38
39     private final AbstractPCEPSessionNegotiatorFactory negFactory;
40
41     public PCEPSessionNegotiator(final Channel channel, final Promise<PCEPSessionImpl> promise, final SessionListenerFactory<PCEPSessionListener> factory,
42         final AbstractPCEPSessionNegotiatorFactory negFactory) {
43         super(promise, channel);
44         this.channel = channel;
45         this.promise = promise;
46         this.factory = factory;
47         this.negFactory = negFactory;
48     }
49
50     @Override
51     protected void startNegotiation() throws ExecutionException {
52         final Object lock = this;
53
54         LOG.debug("Bootstrap negotiation for channel {} started", this.channel);
55
56         /*
57          * We have a chance to see if there's a client session already
58          * registered for this client.
59          */
60         final byte[] clientAddress = ((InetSocketAddress) this.channel.remoteAddress()).getAddress().getAddress();
61         final PCEPPeerRegistry sessionReg = this.negFactory.getSessionRegistry();
62
63         synchronized (lock) {
64             if (sessionReg.getSessionReference(clientAddress).isPresent()) {
65                 final byte[] serverAddress = ((InetSocketAddress) this.channel.localAddress()).getAddress().getAddress();
66                 if (COMPARATOR.compare(serverAddress, clientAddress) > 0) {
67                     final Optional<SessionReference> sessionRefMaybe = sessionReg.removeSessionReference(clientAddress);
68                     try {
69                         if (sessionRefMaybe.isPresent()) {
70                             sessionRefMaybe.get().close();
71                         }
72                     } catch (final Exception e) {
73                         LOG.error("Unexpected failure to close old session", e);
74                     }
75                 } else {
76                     negotiationFailed(new IllegalStateException("A conflicting session for address "
77                             + ((InetSocketAddress) this.channel.remoteAddress()).getAddress() + " found."));
78                     return;
79                 }
80             }
81
82             final Short sessionId = sessionReg.nextSession(clientAddress);
83             final AbstractPCEPSessionNegotiator n = this.negFactory.createNegotiator(this.promise, this.factory.getSessionListener(), this.channel, sessionId);
84
85             sessionReg.putSessionReference(clientAddress, new SessionReference() {
86                 @Override
87                 public void close() throws ExecutionException {
88                     try {
89                         sessionReg.releaseSession(clientAddress, sessionId);
90                     } finally {
91                         PCEPSessionNegotiator.this.channel.close();
92                     }
93                 }
94
95                 @Override
96                 public Short getSessionId() {
97                     return sessionId;
98                 }
99             });
100
101             this.channel.closeFuture().addListener(new ChannelFutureListener() {
102                 @Override
103                 public void operationComplete(final ChannelFuture future) {
104                     synchronized (lock) {
105                         sessionReg.removeSessionReference(clientAddress);
106                     }
107                 }
108             });
109
110             LOG.info("Replacing bootstrap negotiator for channel {}", this.channel);
111             this.channel.pipeline().replace(this, "negotiator", n);
112             n.startNegotiation();
113         }
114     }
115
116     @Override
117     protected void handleMessage(final Message msg) {
118         throw new IllegalStateException("Bootstrap negotiator should have been replaced");
119     }
120 }