Cleanup RequestContextStack
[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.JdkFutureAdapters;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.concurrent.Future;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
16 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23
24 public class NodeConfigServiceImpl extends CommonService implements NodeConfigService {
25
26     // FIXME: should be only in CommonService
27     private final DeviceContext deviceContext;
28
29     public NodeConfigServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
30         super(requestContextStack, deviceContext);
31         this.deviceContext = deviceContext;
32     }
33
34     @Override
35     public Future<RpcResult<SetConfigOutput>> setConfig(final SetConfigInput input) {
36         final RequestContext<SetConfigOutput> requestContext = createRequestContext();
37         if (requestContext == null) {
38             return failedFuture();
39         }
40
41         SetConfigInputBuilder builder = new SetConfigInputBuilder();
42         SwitchConfigFlag flag = SwitchConfigFlag.valueOf(input.getFlag());
43         final Long reserverXid = deviceContext.getReservedXid();
44         if (null == reserverXid) {
45             return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
46         }
47
48         final Xid xid = new Xid(reserverXid);
49         builder.setXid(xid.getValue());
50         builder.setFlags(flag);
51         builder.setMissSendLen(input.getMissSearchLength());
52         builder.setVersion(getVersion());
53         ListenableFuture<RpcResult<Void>> futureResultFromOfLib;
54         synchronized (deviceContext) {
55             futureResultFromOfLib = JdkFutureAdapters.listenInPoolThread(deviceContext.getPrimaryConnectionContext().getConnectionAdapter().setConfig(builder.build()));
56         }
57         OFJResult2RequestCtxFuture<SetConfigOutput> OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture<>(requestContext, deviceContext);
58         OFJResult2RequestCtxFuture.processResultFromOfJava(futureResultFromOfLib);
59         return requestContext.getFuture();
60     }
61 }