Improve cache interactions
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / connection / ResponseExpectedRpcListener.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.connection;
9
10 import java.util.concurrent.TimeoutException;
11
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import com.google.common.base.Preconditions;
17 import com.google.common.cache.Cache;
18
19 final class ResponseExpectedRpcListener<T extends OfHeader> extends AbstractRpcListener<T> {
20     private static final Logger LOG = LoggerFactory.getLogger(ResponseExpectedRpcListener.class);
21     private final Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
22     private final RpcResponseKey key;
23
24     ResponseExpectedRpcListener(final String failureInfo,
25             final Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache, final RpcResponseKey key) {
26         super(failureInfo);
27         this.cache = Preconditions.checkNotNull(cache);
28         this.key = Preconditions.checkNotNull(key);
29     }
30
31     public void discard() {
32         LOG.warn("Request for {} did not receive a response", key);
33         failedRpc(new TimeoutException("Request timed out"));
34     }
35
36     @SuppressWarnings("unchecked")
37     public void completed(final OfHeader message) {
38         successfulRpc((T)message);
39     }
40
41     @Override
42     protected void operationSuccessful() {
43         LOG.debug("Request for {} sent successfully", key);
44         cache.put(key, this);
45     }
46 }