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