BUG-54 : switched channel pipeline to be protocol specific.
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionNegotiator.java
1 /*
2  * Copyright (c) 2013 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.bgp.rib.impl;
9
10 import io.netty.channel.Channel;
11 import io.netty.util.Timeout;
12 import io.netty.util.Timer;
13 import io.netty.util.TimerTask;
14 import io.netty.util.concurrent.Promise;
15
16 import java.util.concurrent.TimeUnit;
17
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPError;
20 import org.opendaylight.protocol.bgp.parser.BGPMessage;
21 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
22 import org.opendaylight.protocol.bgp.parser.message.BGPKeepAliveMessage;
23 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
24 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
26 import org.opendaylight.protocol.framework.AbstractSessionNegotiator;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.Preconditions;
31
32 public final class BGPSessionNegotiator extends AbstractSessionNegotiator<BGPMessage, BGPSessionImpl> {
33         // 4 minutes recommended in http://tools.ietf.org/html/rfc4271#section-8.2.2
34         private static final int INITIAL_HOLDTIMER = 4;
35
36         private enum State {
37                 /**
38                  * Negotiation has not started yet.
39                  */
40                 Idle,
41                 /**
42                  * We have sent our Open message, and are waiting for the peer's Open message.
43                  */
44                 OpenSent,
45                 /**
46                  * We have received the peer's Open message, which is acceptable, and we're waiting the acknowledgement of our
47                  * Open message.
48                  */
49                 OpenConfirm,
50                 /**
51                  * The negotiation finished.
52                  */
53                 Finished,
54         }
55
56         private static final Logger logger = LoggerFactory.getLogger(BGPSessionNegotiator.class);
57         private final BGPSessionListener listener;
58         private final Timer timer;
59         private final BGPSessionPreferences localPref;
60         private BGPOpenMessage remotePref;
61         private State state = State.Idle;
62         private final short keepAlive = 15;
63
64         public BGPSessionNegotiator(final Timer timer, final Promise<BGPSessionImpl> promise, final Channel channel,
65                         final BGPSessionPreferences initialPrefs, final BGPSessionListener listener) {
66                 super(promise, channel);
67                 this.listener = Preconditions.checkNotNull(listener);
68                 this.localPref = Preconditions.checkNotNull(initialPrefs);
69                 this.timer = Preconditions.checkNotNull(timer);
70         }
71
72         @Override
73         protected void startNegotiation() {
74                 Preconditions.checkState(this.state == State.Idle);
75                 this.channel.writeAndFlush(new BGPOpenMessage(this.localPref.getMyAs(), (short) this.localPref.getHoldTime(), this.localPref.getBgpId(), this.localPref.getParams()));
76                 this.state = State.OpenSent;
77
78                 final Object lock = this;
79                 this.timer.newTimeout(new TimerTask() {
80                         @Override
81                         public void run(final Timeout timeout) throws Exception {
82                                 synchronized (lock) {
83                                         if (BGPSessionNegotiator.this.state != State.Finished) {
84                                                 negotiationFailed(new BGPDocumentedException("HoldTimer expired", BGPError.FSM_ERROR));
85                                                 BGPSessionNegotiator.this.state = State.Finished;
86                                         }
87                                 }
88                         }
89                 }, INITIAL_HOLDTIMER, TimeUnit.MINUTES);
90         }
91
92         @Override
93         protected synchronized void handleMessage(final BGPMessage msg) {
94                 logger.debug("Channel {} handling message in state {}", this.channel, this.state);
95
96                 switch (this.state) {
97                 case Finished:
98                 case Idle:
99                         throw new IllegalStateException("Unexpected state " + this.state);
100                 case OpenConfirm:
101                         if (msg instanceof BGPKeepAliveMessage) {
102                                 final BGPKeepAliveMessage ka = (BGPKeepAliveMessage) msg;
103
104                                 // FIXME: we miss some stuff over here
105
106                                 negotiationSuccessful(new BGPSessionImpl(this.timer, this.listener, this.channel, this.keepAlive, this.remotePref));
107                                 this.state = State.Finished;
108                                 return;
109                         } else if (msg instanceof BGPNotificationMessage) {
110                                 final BGPNotificationMessage ntf = (BGPNotificationMessage) msg;
111                                 negotiationFailed(new BGPDocumentedException("Peer refusal", ntf.getError()));
112                                 this.state = State.Finished;
113                                 return;
114                         }
115
116                         break;
117                 case OpenSent:
118                         if (msg instanceof BGPOpenMessage) {
119                                 final BGPOpenMessage open = (BGPOpenMessage) msg;
120
121                                 // TODO: validate the open message
122
123                                 this.remotePref = open;
124                                 this.channel.writeAndFlush(new BGPKeepAliveMessage());
125                                 this.state = State.OpenConfirm;
126                                 logger.debug("Channel {} moved to OpenConfirm state with remote proposal {}", this.channel, this.remotePref);
127                                 return;
128                         }
129                         break;
130                 }
131
132                 // Catch-all for unexpected message
133                 logger.warn("Channel {} state {} unexpected message {}", this.channel, this.state, msg);
134                 this.channel.writeAndFlush(new BGPNotificationMessage(BGPError.FSM_ERROR));
135                 negotiationFailed(new BGPDocumentedException("Unexpected message", BGPError.FSM_ERROR));
136                 this.state = State.Finished;
137         }
138 }