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.bgpcep.pcep.topology.provider;
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 javax.annotation.concurrent.GuardedBy;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.OperationResult;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.lsp.metadata.Metadata;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 final class PCEPRequest {
23 private static final Logger LOG = LoggerFactory.getLogger(PCEPRequest.class);
25 private static final long MINIMUM_ELAPSED_TIME = 1;
33 private final SettableFuture<OperationResult> future;
34 private final Metadata metadata;
35 private volatile State state;
37 private final Stopwatch stopwatch;
38 private final Timer timer;
40 PCEPRequest(final Metadata metadata) {
41 this.future = SettableFuture.create();
42 this.metadata = metadata;
43 this.state = State.UNSENT;
44 this.stopwatch = Stopwatch.createStarted();
45 this.timer = new Timer();
48 protected ListenableFuture<OperationResult> getFuture() {
52 public Metadata getMetadata() {
56 public State getState() {
64 synchronized void done(final OperationResult result) {
65 if (this.state != State.DONE) {
66 LOG.debug("Request went from {} to {}", this.state, State.DONE);
67 this.state = State.DONE;
69 this.future.set(result);
73 synchronized void done() {
74 OperationResult result;
77 result = OperationResults.UNSENT;
80 result = OperationResults.NOACK;
90 synchronized void sent() {
91 if (this.state == State.UNSENT) {
92 LOG.debug("Request went from {} to {}", this.state, State.UNACKED);
93 this.state = State.UNACKED;
97 synchronized long getElapsedMillis() {
98 final long elapsedNanos = this.stopwatch.elapsed().toNanos();
99 final long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(elapsedNanos);
100 if (elapsedMillis == 0 && elapsedNanos > 0) {
101 return MINIMUM_ELAPSED_TIME;
103 return elapsedMillis;