Returning of future from request context - NodeConfigService.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / NodeConfigServiceImpl.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;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.SettableFuture;
12 import java.util.concurrent.Future;
13 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;
25 import org.opendaylight.yangtools.yang.common.RpcError;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28
29 public class NodeConfigServiceImpl extends CommonService implements NodeConfigService {
30
31
32     public NodeConfigServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
33         super(requestContextStack, deviceContext);
34     }
35
36     @Override
37     public Future<RpcResult<SetConfigOutput>> setConfig(final SetConfigInput input) {
38         final RequestContext<SetConfigOutput> requestContext = createRequestContext();
39         if (requestContext == null) {
40             return failedFuture();
41         }
42
43         SetConfigInputBuilder builder = new SetConfigInputBuilder();
44         SwitchConfigFlag flag = SwitchConfigFlag.valueOf(input.getFlag());
45
46         final Xid xid = requestContext.getXid();
47         builder.setXid(xid.getValue());
48         builder.setFlags(flag);
49         builder.setMissSendLen(input.getMissSearchLength());
50         builder.setVersion(getVersion());
51         final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
52
53         final SettableFuture<RpcResult<SetConfigOutput>> settableFuture = SettableFuture.create();
54         final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput setConfigInput = builder.build();
55         outboundQueue.commitEntry(xid.getValue(), setConfigInput, new FutureCallback<OfHeader>() {
56
57             RpcResultBuilder<SetConfigOutput> rpcResultBuilder;
58             @Override
59             public void onSuccess(final OfHeader ofHeader) {
60                 rpcResultBuilder =  RpcResultBuilder.<SetConfigOutput>success();
61                 requestContext.setResult(rpcResultBuilder.build());
62                 RequestContextUtil.closeRequstContext(requestContext);
63
64                 getMessageSpy().spyMessage(setConfigInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
65             }
66
67             @Override
68             public void onFailure(final Throwable throwable) {
69                 rpcResultBuilder = RpcResultBuilder.<SetConfigOutput>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
70                 requestContext.setResult(rpcResultBuilder.build());
71                 RequestContextUtil.closeRequstContext(requestContext);
72
73                 getMessageSpy().spyMessage(setConfigInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
74             }
75         });
76         return requestContext.getFuture();
77
78     }
79 }