2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
13 import io.netty.channel.Channel;
14 import io.netty.util.concurrent.Promise;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
20 import org.opendaylight.protocol.framework.AbstractSessionNegotiator;
21 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.SessionCase;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Abstract PCEP session negotiator. Takes care of basic handshake without implementing a specific policy. Policies need
38 * to be provided by a specific subclass.
40 public abstract class AbstractPCEPSessionNegotiator extends AbstractSessionNegotiator<Message, PCEPSessionImpl> {
42 * Unified KeepWait and OpenWait timer expiration, in seconds.
44 public static final int FAIL_TIMER_VALUE = 60;
47 * PCEP session negotiation state transitions are described in RFC5440. Simplification the two timers (KeepWait and
48 * OpenWait) are merged into a FailTimer, as they are mutually exclusive, have the same timeout value and their
49 * action is to terminate negotiation. This timer is restarted between state transitions and runs in all states
50 * except Idle and Finished.
55 * Negotiation has not begun. It will be activated once we are asked to provide our initial proposal, at which
56 * point we move into OpenWait state.
60 * Waiting for the peer's OPEN message.
64 * Waiting for the peer's KEEPALIVE message.
68 * Negotiation has completed.
73 private static final Logger LOG = LoggerFactory.getLogger(AbstractPCEPSessionNegotiator.class);
74 private static final Keepalive KEEPALIVE = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
76 private volatile boolean localOK, openRetry, remoteOK;
77 private volatile State state = State.Idle;
78 private Future<?> failTimer;
79 private Open localPrefs;
80 private Open remotePrefs;
82 protected AbstractPCEPSessionNegotiator(final Promise<PCEPSessionImpl> promise, final Channel channel) {
83 super(promise, channel);
87 * Get the initial session parameters proposal.
89 * @return Session parameters proposal.
91 protected abstract Open getInitialProposal();
94 * Get the revised session parameters proposal based on the feedback the peer has provided to us.
96 * @param suggestion Peer-provided suggested session parameters
97 * @return Session parameters proposal, or null if peers session parameters preclude us from suggesting anything
99 protected abstract Open getRevisedProposal(Open suggestion);
102 * Check whether a peer-provided session parameters proposal is acceptable.
104 * @param proposal peer-proposed session parameters
105 * @return true if the proposal is acceptable, false otherwise
107 protected abstract boolean isProposalAcceptable(Open proposal);
110 * Given a peer-provided session parameters proposal which we found unacceptable, provide a counter-proposal. The
111 * requirement is that the isProposalAcceptable() method has to return true when presented with this proposal.
113 * @param proposal unacceptable peer proposal
114 * @return our counter-proposal, or null if there is no way to negotiate an acceptable proposal
116 protected abstract Open getCounterProposal(Open proposal);
119 * Create the protocol session.
121 * @param channel Underlying channel.
122 * @param sessionId Assigned session ID.
123 * @param localPrefs Session preferences proposed by us and accepted by the peer.
124 * @param remotePrefs Session preferences proposed by the peer and accepted by us.
125 * @return New protocol session.
127 protected abstract PCEPSessionImpl createSession(Channel channel, Open localPrefs, Open remotePrefs);
130 * Sends PCEP Error Message with one PCEPError.
134 private void sendErrorMessage(final PCEPErrors value) {
136 this.sendMessage(Util.createErrorMessage(value, null));
139 private void scheduleFailTimer() {
140 this.failTimer = this.channel.eventLoop().schedule(new Runnable() {
143 switch (AbstractPCEPSessionNegotiator.this.state) {
148 sendErrorMessage(PCEPErrors.NO_MSG_BEFORE_EXP_KEEPWAIT);
149 negotiationFailed(new TimeoutException("KeepWait timer expired"));
150 AbstractPCEPSessionNegotiator.this.state = State.Finished;
153 sendErrorMessage(PCEPErrors.NO_OPEN_BEFORE_EXP_OPENWAIT);
154 negotiationFailed(new TimeoutException("OpenWait timer expired"));
155 AbstractPCEPSessionNegotiator.this.state = State.Finished;
159 }, FAIL_TIMER_VALUE, TimeUnit.SECONDS);
163 protected final void startNegotiation() {
164 Preconditions.checkState(this.state == State.Idle);
165 this.localPrefs = getInitialProposal();
166 final OpenMessage m = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder().setOpenMessage(
167 new OpenMessageBuilder().setOpen(this.localPrefs).build()).build();
169 this.state = State.OpenWait;
172 LOG.info("PCEP session with {} started, sent proposal {}", this.channel, this.localPrefs);
176 protected final void handleMessage(final Message msg) {
177 this.failTimer.cancel(false);
179 LOG.debug("Channel {} handling message {} in state {}", this.channel, msg, this.state);
181 switch (this.state) {
184 throw new IllegalStateException("Unexpected handleMessage in state " + this.state);
186 if (msg instanceof Keepalive) {
189 LOG.info("PCEP peer {} completed negotiation", this.channel);
190 negotiationSuccessful(createSession(this.channel, this.localPrefs, this.remotePrefs));
191 this.state = State.Finished;
194 this.state = State.OpenWait;
195 LOG.debug("Channel {} moved to OpenWait state with localOK=1", this.channel);
199 } else if (msg instanceof Pcerr) {
200 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessage err = ((Pcerr) msg).getPcerrMessage();
201 if (err.getErrorType() == null) {
202 final ErrorObject obj = err.getErrors().get(0).getErrorObject();
203 LOG.warn("Unexpected error received from PCC: type {} value {}", obj.getType(), obj.getValue());
204 negotiationFailed(new IllegalStateException("Unexpected error received from PCC."));
205 this.state = State.Idle;
208 this.localPrefs = getRevisedProposal(((SessionCase) err.getErrorType()).getSession().getOpen());
209 if (this.localPrefs == null) {
210 sendErrorMessage(PCEPErrors.PCERR_NON_ACC_SESSION_CHAR);
211 negotiationFailed(new IllegalStateException("Peer suggested unacceptable retry proposal"));
212 this.state = State.Finished;
215 this.sendMessage(new OpenBuilder().setOpenMessage(new OpenMessageBuilder().setOpen(this.localPrefs).build()).build());
216 if (!this.remoteOK) {
217 this.state = State.OpenWait;
225 if (msg instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open) {
226 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage o = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open) msg).getOpenMessage();
227 final Open open = o.getOpen();
228 if (isProposalAcceptable(open)) {
229 this.sendMessage(KEEPALIVE);
230 this.remotePrefs = open;
231 this.remoteOK = true;
233 negotiationSuccessful(createSession(this.channel, this.localPrefs, this.remotePrefs));
234 LOG.info("PCEP peer {} completed negotiation", this.channel);
235 this.state = State.Finished;
238 this.state = State.KeepWait;
239 LOG.debug("Channel {} moved to KeepWait state with remoteOK=1", this.channel);
244 if (this.openRetry) {
245 sendErrorMessage(PCEPErrors.SECOND_OPEN_MSG);
246 negotiationFailed(new IllegalStateException("OPEN renegotiation failed"));
247 this.state = State.Finished;
251 final Open newPrefs = getCounterProposal(open);
252 if (newPrefs == null) {
253 sendErrorMessage(PCEPErrors.NON_ACC_NON_NEG_SESSION_CHAR);
254 negotiationFailed(new IllegalStateException("Peer sent unacceptable session parameters"));
255 this.state = State.Finished;
259 this.sendMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR, newPrefs));
261 this.openRetry = true;
262 this.state = this.localOK ? State.OpenWait : State.KeepWait;
270 LOG.warn("Channel {} in state {} received unexpected message {}", this.channel, this.state, msg);
271 sendErrorMessage(PCEPErrors.NON_OR_INVALID_OPEN_MSG);
272 negotiationFailed(new Exception("Illegal message encountered"));
273 this.state = State.Finished;