028f51d616748fabd5c272cad1dcf1e3902689b5
[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             @Override
57             public void onSuccess(final OfHeader ofHeader) {
58                 RequestContextUtil.closeRequstContext(requestContext);
59                 getMessageSpy().spyMessage(setConfigInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
60
61                 settableFuture.set(RpcResultBuilder.<SetConfigOutput>success().build());
62             }
63
64             @Override
65             public void onFailure(final Throwable throwable) {
66                 RpcResultBuilder<SetConfigOutput> rpcResultBuilder = RpcResultBuilder.<SetConfigOutput>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
67                 RequestContextUtil.closeRequstContext(requestContext);
68                 getMessageSpy().spyMessage(setConfigInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
69                 settableFuture.set(rpcResultBuilder.build());
70             }
71         });
72         return settableFuture;
73
74     }
75 }