Use sendMessage() from AbstractSessionNegotiator
[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.List;
17 import java.util.concurrent.TimeUnit;
18
19 import javax.annotation.concurrent.GuardedBy;
20
21 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
22 import org.opendaylight.protocol.bgp.parser.BGPError;
23 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
25 import org.opendaylight.protocol.framework.AbstractSessionNegotiator;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
33 import org.opendaylight.yangtools.yang.binding.Notification;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.annotations.VisibleForTesting;
38 import com.google.common.base.Preconditions;
39
40 public final class BGPSessionNegotiator extends AbstractSessionNegotiator<Notification, BGPSessionImpl> {
41         // 4 minutes recommended in http://tools.ietf.org/html/rfc4271#section-8.2.2
42         protected static final int INITIAL_HOLDTIMER = 4;
43
44         @VisibleForTesting
45         public enum State {
46                 /**
47                  * Negotiation has not started yet.
48                  */
49                 Idle,
50                 /**
51                  * We have sent our Open message, and are waiting for the peer's Open message.
52                  */
53                 OpenSent,
54                 /**
55                  * We have received the peer's Open message, which is acceptable, and we're waiting the acknowledgement of our
56                  * Open message.
57                  */
58                 OpenConfirm,
59                 /**
60                  * The negotiation finished.
61                  */
62                 Finished,
63         }
64
65         private static final Logger LOG = LoggerFactory.getLogger(BGPSessionNegotiator.class);
66         private final BGPSessionListener listener;
67         private final Timer timer;
68         private final BGPSessionPreferences localPref;
69
70         @GuardedBy("this")
71         private Open remotePref;
72
73         @GuardedBy("this")
74         private State state = State.Idle;
75
76         @GuardedBy("this")
77         private BGPSessionImpl session;
78
79         public BGPSessionNegotiator(final Timer timer, final Promise<BGPSessionImpl> promise, final Channel channel,
80                         final BGPSessionPreferences initialPrefs, final BGPSessionListener listener) {
81                 super(promise, channel);
82                 this.listener = Preconditions.checkNotNull(listener);
83                 this.localPref = Preconditions.checkNotNull(initialPrefs);
84                 this.timer = Preconditions.checkNotNull(timer);
85         }
86
87         @Override
88         protected void startNegotiation() {
89                 Preconditions.checkState(this.state == State.Idle);
90                 this.sendMessage(new OpenBuilder().setMyAsNumber(this.localPref.getMyAs().getValue().intValue()).setHoldTimer(
91                                 this.localPref.getHoldTime()).setBgpIdentifier(this.localPref.getBgpId()).setBgpParameters(this.localPref.getParams()).build());
92                 this.state = State.OpenSent;
93
94                 final Object lock = this;
95                 this.timer.newTimeout(new TimerTask() {
96                         @Override
97                         public void run(final Timeout timeout) throws Exception {
98                                 synchronized (lock) {
99                                         if (BGPSessionNegotiator.this.state != State.Finished) {
100                                                 BGPSessionNegotiator.this.sendMessage(new NotifyBuilder().setErrorCode(BGPError.HOLD_TIMER_EXPIRED.getCode()).setErrorSubcode(
101                                                                 BGPError.HOLD_TIMER_EXPIRED.getSubcode()).build());
102                                                 negotiationFailed(new BGPDocumentedException("HoldTimer expired", BGPError.FSM_ERROR));
103                                                 BGPSessionNegotiator.this.state = State.Finished;
104                                         }
105                                 }
106                         }
107                 }, INITIAL_HOLDTIMER, TimeUnit.MINUTES);
108         }
109
110         @Override
111         protected synchronized void handleMessage(final Notification msg) {
112                 LOG.debug("Channel {} handling message in state {}", this.channel, this.state);
113
114                 switch (this.state) {
115                 case Finished:
116                 case Idle:
117                         final Notify fsmError = new NotifyBuilder().setErrorCode(BGPError.FSM_ERROR.getCode()).setErrorSubcode(
118                                         BGPError.FSM_ERROR.getSubcode()).build();
119                         this.sendMessage(fsmError);
120                 case OpenConfirm:
121                         if (msg instanceof Keepalive) {
122                                 negotiationSuccessful(this.session);
123                         } else if (msg instanceof Notify) {
124                                 final Notify ntf = (Notify) msg;
125                                 negotiationFailed(new BGPDocumentedException("Peer refusal", BGPError.forValue(ntf.getErrorCode(), ntf.getErrorSubcode())));
126                         }
127                         this.state = State.Finished;
128                         return;
129                 case OpenSent:
130                         if (msg instanceof Open) {
131                                 final Open openObj = (Open) msg;
132                                 handleOpen(openObj);
133                                 return;
134                         }
135                         break;
136                 }
137
138                 // Catch-all for unexpected message
139                 LOG.warn("Channel {} state {} unexpected message {}", this.channel, this.state, msg);
140                 this.sendMessage(new NotifyBuilder().setErrorCode(BGPError.FSM_ERROR.getCode()).setErrorSubcode(BGPError.FSM_ERROR.getSubcode()).build());
141                 negotiationFailed(new BGPDocumentedException("Unexpected message", BGPError.FSM_ERROR));
142                 this.state = State.Finished;
143         }
144
145         private void handleOpen(final Open openObj) {
146                 final List<BgpParameters> prefs = openObj.getBgpParameters();
147                 if (prefs != null && !prefs.isEmpty()) {
148                         if (!prefs.containsAll(this.localPref.getParams())) {
149                                 LOG.info("Open message unacceptable. Check the configuration of BGP speaker.");
150                         }
151                         this.remotePref = openObj;
152                         this.sendMessage(new KeepaliveBuilder().build());
153                         this.session = new BGPSessionImpl(this.timer, this.listener, this.channel, this.remotePref);
154                         this.state = State.OpenConfirm;
155                         LOG.debug("Channel {} moved to OpenConfirm state with remote proposal {}", this.channel, this.remotePref);
156                         return;
157                 }
158                 final Notify ntf = new NotifyBuilder().setErrorCode(BGPError.UNSPECIFIC_OPEN_ERROR.getCode()).setErrorSubcode(
159                                 BGPError.UNSPECIFIC_OPEN_ERROR.getSubcode()).build();
160                 this.sendMessage(ntf);
161                 negotiationFailed(new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.", BGPError.forValue(
162                                 ntf.getErrorCode(), ntf.getErrorSubcode())));
163                 this.state = State.Finished;
164         }
165
166         public synchronized State getState() {
167                 return this.state;
168         }
169 }