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