Fix sonar complains
[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.ChannelFutureListener;
14 import io.netty.util.concurrent.Promise;
15 import java.net.InetSocketAddress;
16 import java.util.Comparator;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
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     private final PCEPPeerProposal peerProposal;
35
36     public PCEPSessionNegotiator(final Channel channel, final Promise<PCEPSessionImpl> promise, final PCEPSessionListenerFactory factory,
37         final AbstractPCEPSessionNegotiatorFactory negFactory, final PCEPPeerProposal peerProposal) {
38         super(promise, channel);
39         this.factory = factory;
40         this.negFactory = negFactory;
41         this.peerProposal = peerProposal;
42     }
43
44     @Override
45     protected void startNegotiation() throws ExecutionException {
46         final Object lock = this;
47
48         LOG.debug("Bootstrap negotiation for channel {} started", this.channel);
49
50         /*
51          * We have a chance to see if there's a client session already
52          * registered for this client.
53          */
54         final byte[] clientAddress = ((InetSocketAddress) this.channel.remoteAddress()).getAddress().getAddress();
55         final PCEPPeerRegistry sessionReg = this.negFactory.getSessionRegistry();
56
57         synchronized (lock) {
58             if (sessionReg.getSessionReference(clientAddress).isPresent()) {
59                 final byte[] serverAddress = ((InetSocketAddress) this.channel.localAddress()).getAddress().getAddress();
60                 if (COMPARATOR.compare(serverAddress, clientAddress) > 0) {
61                     final Optional<SessionReference> sessionRefMaybe = sessionReg.removeSessionReference(clientAddress);
62                     try {
63                         if (sessionRefMaybe.isPresent()) {
64                             sessionRefMaybe.get().close();
65                         }
66                     } catch (final Exception e) {
67                         LOG.error("Unexpected failure to close old session", e);
68                     }
69                 } else {
70                     negotiationFailed(new IllegalStateException("A conflicting session for address "
71                             + ((InetSocketAddress) this.channel.remoteAddress()).getAddress() + " found."));
72                     return;
73                 }
74             }
75
76             final Short sessionId = sessionReg.nextSession(clientAddress);
77             final AbstractPCEPSessionNegotiator n = this.negFactory.createNegotiator(this.promise, this.factory.getSessionListener(),
78                     this.channel, sessionId, this.peerProposal);
79
80             sessionReg.putSessionReference(clientAddress, new SessionReference() {
81                 @Override
82                 public void close() throws ExecutionException {
83                     try {
84                         sessionReg.releaseSession(clientAddress, sessionId);
85                     } finally {
86                         PCEPSessionNegotiator.this.channel.close();
87                     }
88                 }
89
90                 @Override
91                 public Short getSessionId() {
92                     return sessionId;
93                 }
94             });
95
96             this.channel.closeFuture().addListener((ChannelFutureListener) future -> {
97                 synchronized (lock) {
98                     sessionReg.removeSessionReference(clientAddress);
99                 }
100             });
101
102             LOG.info("Replacing bootstrap negotiator for channel {}", this.channel);
103             this.channel.pipeline().replace(this, "negotiator", n);
104             n.startNegotiation();
105         }
106     }
107
108     @Override
109     protected void handleMessage(final Message msg) {
110         throw new IllegalStateException("Bootstrap negotiator should have been replaced");
111     }
112 }