f0f9c91438ed1e7abcb43913087deaf7f04901c4
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / SalEchoServiceImpl.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 package org.opendaylight.openflowplugin.impl.services.sal;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.concurrent.Future;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.impl.services.EchoService;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SalEchoService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SendEchoOutputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26
27 public final class SalEchoServiceImpl implements SalEchoService {
28
29     private final EchoService echoService;
30
31     public SalEchoServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
32         echoService = new EchoService(requestContextStack, deviceContext);
33     }
34
35     @Override
36     public Future<RpcResult<SendEchoOutput>> sendEcho(final SendEchoInput sendEchoInput) {
37         final EchoInputBuilder echoInputBld = new EchoInputBuilder()
38                 .setData(sendEchoInput.getData());
39         return transform(echoService.handleServiceCall(echoInputBld));
40     }
41
42     private Future<RpcResult<SendEchoOutput>>
43             transform(final ListenableFuture<RpcResult<EchoOutput>> rpcResultListenableFuture) {
44         return Futures.transform(rpcResultListenableFuture, input -> {
45             Preconditions.checkNotNull(input, "echoOutput value is never expected to be NULL");
46             final RpcResult<SendEchoOutput> rpcOutput;
47             if (input.isSuccessful()) {
48                 final SendEchoOutput sendEchoOutput = new SendEchoOutputBuilder()
49                         .setData(input.getResult().getData())
50                         .build();
51                 rpcOutput = RpcResultBuilder.success(sendEchoOutput).build();
52             } else {
53                 rpcOutput = RpcResultBuilder.<SendEchoOutput>failed()
54                         .withRpcErrors(input.getErrors())
55                         .build();
56             }
57             return rpcOutput;
58         }, MoreExecutors.directExecutor());
59     }
60 }