clean pcep/impl
[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.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;
23
24 public class PCEPSessionNegotiator extends AbstractSessionNegotiator {
25
26     private static final Logger LOG = LoggerFactory.getLogger(PCEPSessionNegotiator.class);
27
28     private static final Comparator<byte[]> COMPARATOR = UnsignedBytes.lexicographicalComparator();
29     private final AbstractPCEPSessionNegotiatorFactory negFactory;
30     private final PCEPSessionNegotiatorFactoryDependencies nfd;
31
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;
38     }
39
40     @Override
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;
45
46         LOG.debug("Bootstrap negotiation for channel {} started", this.channel);
47
48         /*
49          * We have a chance to see if there's a client session already
50          * registered for this client.
51          */
52         final byte[] clientAddress = ((InetSocketAddress) this.channel.remoteAddress()).getAddress().getAddress();
53         final PCEPPeerRegistry sessionReg = this.negFactory.getSessionRegistry();
54
55         synchronized (lock) {
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);
61                     try {
62                         if (sessionRefMaybe.isPresent()) {
63                             sessionRefMaybe.get().close();
64                         }
65                     } catch (final Exception e) {
66                         LOG.error("Unexpected failure to close old session", e);
67                     }
68                 } else {
69                     negotiationFailed(new IllegalStateException("A conflicting session for address "
70                             + ((InetSocketAddress) this.channel.remoteAddress()).getAddress() + " found."));
71                     return;
72                 }
73             }
74
75             final Short sessionId = sessionReg.nextSession(clientAddress);
76             final AbstractPCEPSessionNegotiator n = this.negFactory
77                     .createNegotiator(this.nfd, this.promise, this.channel, sessionId);
78
79             sessionReg.putSessionReference(clientAddress, new SessionReference() {
80                 @Override
81                 public void close() throws ExecutionException {
82                     try {
83                         sessionReg.releaseSession(clientAddress, sessionId);
84                     } finally {
85                         PCEPSessionNegotiator.this.channel.close();
86                     }
87                 }
88
89                 @Override
90                 public Short getSessionId() {
91                     return sessionId;
92                 }
93             });
94
95             this.channel.closeFuture().addListener((ChannelFutureListener) future -> {
96                 synchronized (lock) {
97                     sessionReg.removeSessionReference(clientAddress);
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     @Override
108     protected void handleMessage(final Message msg) {
109         throw new IllegalStateException("Bootstrap negotiator should have been replaced");
110     }
111 }