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