Bug-2081: PCEP statistics
[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.concurrent.TimeUnit;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.lsp.metadata.Metadata;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 final class PCEPRequest {
20     static enum State {
21         UNSENT,
22         UNACKED,
23         DONE,
24     }
25
26     private static final Logger LOG = LoggerFactory.getLogger(PCEPRequest.class);
27     private final SettableFuture<OperationResult> future;
28     private final Metadata metadata;
29     private volatile State state;
30     private final Stopwatch stopwatch;
31
32     PCEPRequest(final Metadata metadata) {
33         this.future = SettableFuture.create();
34         this.metadata = metadata;
35         this.state = State.UNSENT;
36         this.stopwatch = new Stopwatch().start();
37     }
38
39     protected ListenableFuture<OperationResult> getFuture() {
40         return future;
41     }
42
43     public Metadata getMetadata() {
44         return metadata;
45     }
46
47     public State getState() {
48         return state;
49     }
50
51     public synchronized void done(final OperationResult result) {
52         if (state != State.DONE) {
53             LOG.debug("Request went from {} to {}", state, State.DONE);
54             state = State.DONE;
55             future.set(result);
56         }
57     }
58
59     public synchronized void sent() {
60         if (state == State.UNSENT) {
61             LOG.debug("Request went from {} to {}", state, State.UNACKED);
62             state = State.UNACKED;
63         }
64     }
65
66     public long getElapsedMillis() {
67         return this.stopwatch.elapsed(TimeUnit.MILLISECONDS);
68     }
69 }