DataCrate removed and its usage replaced by RequestContext
[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.FlowModInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.SalPortService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortOutput;
30 import org.opendaylight.yangtools.yang.common.RpcError;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 import org.slf4j.Logger;
34
35 public class SalPortServiceImpl extends CommonService implements SalPortService {
36     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalPortServiceImpl.class);
37
38     public SalPortServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
39         super(requestContextStack, deviceContext);
40     }
41
42     @Override
43     public Future<RpcResult<UpdatePortOutput>> updatePort(final UpdatePortInput input) {
44         return this.<UpdatePortOutput, Void>handleServiceCall(new Function<RequestContext<UpdatePortOutput>, ListenableFuture<RpcResult<Void>>>() {
45             @Override
46             public ListenableFuture<RpcResult<Void>> apply(final RequestContext<UpdatePortOutput> requestContext) {
47                 getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
48
49                 final Port inputPort = input.getUpdatedPort().getPort().getPort().get(0);
50                 final PortModInput ofPortModInput = PortConvertor.toPortModInput(inputPort, getVersion());
51                 final PortModInputBuilder mdInput = new PortModInputBuilder(ofPortModInput);
52                 final Xid xid = requestContext.getXid();
53                 final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
54
55                 mdInput.setXid(xid.getValue());
56                 final SettableFuture<RpcResult<Void>> settableFuture = SettableFuture.create();
57                 outboundQueue.commitEntry(xid.getValue(), mdInput.build(), new FutureCallback<OfHeader>() {
58                     @Override
59                     public void onSuccess(final OfHeader ofHeader) {
60                         RequestContextUtil.closeRequstContext(requestContext);
61                         getDeviceContext().unhookRequestCtx(requestContext.getXid());
62                         getMessageSpy().spyMessage(FlowModInput.class, 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 rpcResultBuilder = RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
70                         RequestContextUtil.closeRequstContext(requestContext);
71                         getDeviceContext().unhookRequestCtx(requestContext.getXid());
72                         settableFuture.set(rpcResultBuilder.build());
73                     }
74                 });
75                 return settableFuture;
76             }
77         });
78     }
79
80 }