Bug-5612: ODL(PCEP) infinitely waits for the response from peer for addlsp
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / PCEPRequest.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.bgpcep.pcep.topology.provider;
9
10 import com.google.common.base.Stopwatch;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.util.Timer;
14 import java.util.concurrent.TimeUnit;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.lsp.metadata.Metadata;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 final class PCEPRequest {
21     static enum State {
22         UNSENT,
23         UNACKED,
24         DONE,
25     }
26
27     private static final Logger LOG = LoggerFactory.getLogger(PCEPRequest.class);
28     private final SettableFuture<OperationResult> future;
29     private final Metadata metadata;
30     private volatile State state;
31     private final Stopwatch stopwatch;
32     private final Timer timer;
33
34     PCEPRequest(final Metadata metadata) {
35         this.future = SettableFuture.create();
36         this.metadata = metadata;
37         this.state = State.UNSENT;
38         this.stopwatch = Stopwatch.createStarted();
39         this.timer = new Timer();
40     }
41
42     protected ListenableFuture<OperationResult> getFuture() {
43         return future;
44     }
45
46     public Metadata getMetadata() {
47         return metadata;
48     }
49
50     public State getState() {
51         return state;
52     }
53
54     public Timer getTimer() {
55         return timer;
56     }
57
58     public synchronized void done(final OperationResult result) {
59         if (state != State.DONE) {
60             LOG.debug("Request went from {} to {}", state, State.DONE);
61             state = State.DONE;
62             timer.cancel();
63             future.set(result);
64         }
65     }
66
67     public synchronized void done() {
68         OperationResult result = null;
69         switch (state) {
70         case UNSENT:
71             result = OperationResults.UNSENT;
72             break;
73         case UNACKED:
74             result = OperationResults.NOACK;
75             break;
76         case DONE:
77             return;
78         }
79         done(result);
80     }
81
82     public synchronized void sent() {
83         if (state == State.UNSENT) {
84             LOG.debug("Request went from {} to {}", state, State.UNACKED);
85             state = State.UNACKED;
86         }
87     }
88
89     public long getElapsedMillis() {
90         return this.stopwatch.elapsed(TimeUnit.MILLISECONDS);
91     }
92 }