Code Clean Up
[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     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 this.future;
44     }
45
46     public Metadata getMetadata() {
47         return this.metadata;
48     }
49
50     public State getState() {
51         return this.state;
52     }
53
54     Timer getTimer() {
55         return this.timer;
56     }
57
58     synchronized void done(final OperationResult result) {
59         if (this.state != State.DONE) {
60             LOG.debug("Request went from {} to {}", this.state, State.DONE);
61             this.state = State.DONE;
62             this.timer.cancel();
63             this.future.set(result);
64         }
65     }
66
67     synchronized void done() {
68         OperationResult result;
69         switch (this.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         default:
79             return;
80         }
81         done(result);
82     }
83
84     synchronized void sent() {
85         if (this.state == State.UNSENT) {
86             LOG.debug("Request went from {} to {}", this.state, State.UNACKED);
87             this.state = State.UNACKED;
88         }
89     }
90
91     long getElapsedMillis() {
92         return this.stopwatch.elapsed(TimeUnit.MILLISECONDS);
93     }
94 }