statistics services using MultipartRequestInputFactory for generating inputs
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / OFJResult2RequestCtxFuture.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.openflowplugin.impl.services;
10
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.yangtools.yang.common.RpcError;
18 import org.opendaylight.yangtools.yang.common.RpcResult;
19 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Created by Martin Bobak <mbobak@cisco.com> on 26.3.2015.
25  */
26 public class OFJResult2RequestCtxFuture<T> {
27
28     private static final Logger LOG = LoggerFactory.getLogger(OFJResult2RequestCtxFuture.class);
29     private final RequestContext<T> requestContext;
30     private final DeviceContext deviceContext;
31
32     public OFJResult2RequestCtxFuture(final RequestContext<T> requestContext, final DeviceContext deviceContext) {
33         this.requestContext = requestContext;
34         this.deviceContext = deviceContext;
35     }
36
37     public <F> void processResultFromOfJava(final Future<RpcResult<F>> futureResultFromOfLib) {
38         try {
39             final RpcResult<F> rpcResult = futureResultFromOfLib.get(requestContext.getWaitTimeout(), TimeUnit.MILLISECONDS);
40             if (!rpcResult.isSuccessful()) {
41                 requestContext.getFuture().set(
42                         RpcResultBuilder.<T>failed().withRpcErrors(rpcResult.getErrors()).build());
43                 RequestContextUtil.closeRequstContext(requestContext);
44             } else {
45                 LOG.trace("Hooking xid {} to device context.", requestContext.getXid().getValue());
46                 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
47             }
48         } catch (InterruptedException | ExecutionException | TimeoutException e) {
49             requestContext.getFuture().set(
50                     RpcResultBuilder
51                             .<T>failed()
52                             .withError(RpcError.ErrorType.APPLICATION, "",
53                                     "Flow modification on device wasn't successfull.").build());
54             RequestContextUtil.closeRequstContext(requestContext);
55         } catch (final Exception e) {
56             requestContext.getFuture().set(
57                     RpcResultBuilder.<T>failed()
58                             .withError(RpcError.ErrorType.APPLICATION, "", "Flow translation to OF JAVA failed.")
59                             .build());
60             RequestContextUtil.closeRequstContext(requestContext);
61         }
62     }
63 }