Fix RpcResultBuilder/RpcContext raw references
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalGroupServiceImpl.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.GroupConvertor;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37
38 public class SalGroupServiceImpl extends CommonService implements SalGroupService {
39
40
41     public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
42         super(requestContextStack, deviceContext);
43     }
44
45     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalGroupServiceImpl.class);
46
47     @Override
48     public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
49         getDeviceContext().getDeviceGroupRegistry().store(input.getGroupId());
50         return this.<AddGroupOutput, Void>handleServiceCall(new Function<RequestContext<AddGroupOutput>, ListenableFuture<RpcResult<Void>>>() {
51             @Override
52             public ListenableFuture<RpcResult<Void>> apply(final RequestContext<AddGroupOutput> requestContext) {
53                 return convertAndSend(input, requestContext);
54             }
55         });
56     }
57
58     @Override
59     public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
60         return this.<UpdateGroupOutput, Void>handleServiceCall(new Function<RequestContext<UpdateGroupOutput>, ListenableFuture<RpcResult<Void>>>() {
61
62             @Override
63             public ListenableFuture<RpcResult<Void>> apply(final RequestContext<UpdateGroupOutput> requestContext) {
64                 return convertAndSend(input.getUpdatedGroup(), requestContext);
65             }
66         });
67     }
68
69     @Override
70     public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
71         getDeviceContext().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
72         return this.<RemoveGroupOutput, Void>handleServiceCall(new Function<RequestContext<RemoveGroupOutput>, ListenableFuture<RpcResult<Void>>>() {
73
74             @Override
75             public ListenableFuture<RpcResult<Void>> apply(final RequestContext<RemoveGroupOutput> requestContext) {
76                 return convertAndSend(input, requestContext);
77             }
78         });
79     }
80
81     <T> ListenableFuture<RpcResult<Void>> convertAndSend(final Group iputGroup, final RequestContext<T> requestContext) {
82         final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
83         getMessageSpy().spyMessage(iputGroup.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
84         final GroupModInputBuilder ofGroupModInput = GroupConvertor.toGroupModInput(iputGroup, getVersion(), getDatapathId());
85         final Xid xid = requestContext.getXid();
86         ofGroupModInput.setXid(xid.getValue());
87         final SettableFuture<RpcResult<Void>> settableFuture = SettableFuture.create();
88         final GroupModInput groupModInput = ofGroupModInput.build();
89         outboundQueue.commitEntry(xid.getValue(), groupModInput, new FutureCallback<OfHeader>() {
90             @Override
91             public void onSuccess(final OfHeader ofHeader) {
92                 RequestContextUtil.closeRequstContext(requestContext);
93                 getDeviceContext().unhookRequestCtx(requestContext.getXid());
94                 getMessageSpy().spyMessage(groupModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
95
96                 settableFuture.set(RpcResultBuilder.<Void>success().build());
97             }
98
99             @Override
100             public void onFailure(final Throwable throwable) {
101                 RpcResultBuilder<Void> rpcResultBuilder = RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
102                 RequestContextUtil.closeRequstContext(requestContext);
103                 getDeviceContext().unhookRequestCtx(requestContext.getXid());
104                 getMessageSpy().spyMessage(groupModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
105                 settableFuture.set(rpcResultBuilder.build());
106             }
107         });
108         return settableFuture;
109     }
110 }