implementation of NodeConfigService
[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 com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.common.RpcError;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22
23 /**
24  * Created by Martin Bobak <mbobak@cisco.com> on 26.3.2015.
25  */
26 public class RpcResultConvertor<T extends DataObject> {
27
28     private final RequestContext requestContext;
29
30     public RpcResultConvertor(RequestContext requestContext) {
31         this.requestContext = requestContext;
32     }
33
34     public void processResultFromOfJava(final Future<RpcResult<Void>> futureResultFromOfLib,
35                                         final long waitTime) {
36         try {
37             final RpcResult<Void> rpcResult = futureResultFromOfLib.get(waitTime, TimeUnit.MILLISECONDS);
38             if (!rpcResult.isSuccessful()) {
39                 requestContext.getFuture().set(RpcResultBuilder.<T>failed().withRpcErrors(rpcResult.getErrors()).build());
40                 requestContext.close();
41             }
42         } catch (InterruptedException | ExecutionException | TimeoutException e) {
43             requestContext.getFuture().set(RpcResultBuilder
44                     .<T>failed()
45                     .withError(RpcError.ErrorType.APPLICATION, "",
46                             "Flow modification on device wasn't successfull.").build());
47             requestContext.close();
48         } catch (final Exception e) {
49             requestContext.getFuture().set(RpcResultBuilder.<T>failed()
50                     .withError(RpcError.ErrorType.APPLICATION, "", "Flow translation to OF JAVA failed.").build());
51             requestContext.close();
52         }
53     }
54
55 }