Fix RpcResultBuilder/RpcContext raw references
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalPortServiceImpl.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.base.Function;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.concurrent.Future;
15 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PortConvertor;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port;
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.PortModInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.SalPortService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortOutput;
29 import org.opendaylight.yangtools.yang.common.RpcError;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32 import org.slf4j.Logger;
33
34 public class SalPortServiceImpl extends CommonService implements SalPortService {
35     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalPortServiceImpl.class);
36
37     public SalPortServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
38         super(requestContextStack, deviceContext);
39     }
40
41     @Override
42     public Future<RpcResult<UpdatePortOutput>> updatePort(final UpdatePortInput input) {
43         return this.<UpdatePortOutput, Void>handleServiceCall(new Function<RequestContext<UpdatePortOutput>, ListenableFuture<RpcResult<Void>>>() {
44             @Override
45             public ListenableFuture<RpcResult<Void>> apply(final RequestContext<UpdatePortOutput> requestContext) {
46                 getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
47
48                 final Port inputPort = input.getUpdatedPort().getPort().getPort().get(0);
49                 final PortModInput ofPortModInput = PortConvertor.toPortModInput(inputPort, getVersion());
50                 final PortModInputBuilder mdInput = new PortModInputBuilder(ofPortModInput);
51                 final Xid xid = requestContext.getXid();
52                 final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
53
54                 mdInput.setXid(xid.getValue());
55                 final SettableFuture<RpcResult<Void>> settableFuture = SettableFuture.create();
56                 final PortModInput portModInput = mdInput.build();
57                 outboundQueue.commitEntry(xid.getValue(), portModInput, new FutureCallback<OfHeader>() {
58                     @Override
59                     public void onSuccess(final OfHeader ofHeader) {
60                         RequestContextUtil.closeRequstContext(requestContext);
61                         getDeviceContext().unhookRequestCtx(requestContext.getXid());
62                         getMessageSpy().spyMessage(portModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
63
64                         settableFuture.set(RpcResultBuilder.<Void>success().build());
65                     }
66
67                     @Override
68                     public void onFailure(final Throwable throwable) {
69                         RpcResultBuilder<Void> rpcResultBuilder = RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
70                         RequestContextUtil.closeRequstContext(requestContext);
71                         getDeviceContext().unhookRequestCtx(requestContext.getXid());
72                         getMessageSpy().spyMessage(portModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
73                         settableFuture.set(rpcResultBuilder.build());
74                     }
75                 });
76                 return settableFuture;
77             }
78         });
79     }
80
81 }