Fix Jdk8 compatibility
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / RpcResultConvertor.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.binding.DataObject;
18 import org.opendaylight.yangtools.yang.common.RpcError;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
21
22 /**
23  * Created by Martin Bobak <mbobak@cisco.com> on 26.3.2015.
24  */
25 public class RpcResultConvertor<T extends DataObject> {
26
27     private final RequestContext<T> requestContext;
28     private final DeviceContext deviceContext;
29
30     public RpcResultConvertor(final RequestContext<T> requestContext, final DeviceContext deviceContext) {
31         this.requestContext = requestContext;
32         this.deviceContext = deviceContext;
33     }
34
35     public <F> void processResultFromOfJava(final Future<RpcResult<F>> futureResultFromOfLib) {
36         try {
37             final RpcResult<F> rpcResult = futureResultFromOfLib.get(requestContext.getWaitTimeout(), TimeUnit.MILLISECONDS);
38             if (!rpcResult.isSuccessful()) {
39                 requestContext.getFuture().set(
40                         RpcResultBuilder.<T> failed().withRpcErrors(rpcResult.getErrors()).build());
41                 RequestContextUtil.closeRequstContext(requestContext);
42             } else {
43                 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
44             }
45         } catch (InterruptedException | ExecutionException | TimeoutException e) {
46             requestContext.getFuture().set(
47                     RpcResultBuilder
48                             .<T> failed()
49                             .withError(RpcError.ErrorType.APPLICATION, "",
50                                     "Flow modification on device wasn't successfull.").build());
51             RequestContextUtil.closeRequstContext(requestContext);
52         } catch (final Exception e) {
53             requestContext.getFuture().set(
54                     RpcResultBuilder.<T> failed()
55                             .withError(RpcError.ErrorType.APPLICATION, "", "Flow translation to OF JAVA failed.")
56                             .build());
57             RequestContextUtil.closeRequstContext(requestContext);
58         }
59     }
60 }