Make sure RpcResultBuilder is local
[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 java.util.concurrent.Future;
14 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.GroupConvertor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
32 import org.opendaylight.yangtools.yang.common.RpcError;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.slf4j.Logger;
36
37 public class SalGroupServiceImpl extends CommonService implements SalGroupService {
38
39
40     public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
41         super(requestContextStack, deviceContext);
42     }
43
44     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalGroupServiceImpl.class);
45
46     @Override
47     public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
48         getDeviceContext().getDeviceGroupRegistry().store(input.getGroupId());
49         return handleServiceCall(new Function<RequestContext<AddGroupOutput>,
50                 ListenableFuture<RpcResult<AddGroupOutput>>>() {
51             @Override
52             public ListenableFuture<RpcResult<AddGroupOutput>> 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 handleServiceCall(new Function<RequestContext<UpdateGroupOutput>,
61                 ListenableFuture<RpcResult<UpdateGroupOutput>>>() {
62
63             @Override
64             public ListenableFuture<RpcResult<UpdateGroupOutput>> apply(final RequestContext<UpdateGroupOutput> requestContext) {
65                 return convertAndSend(input.getUpdatedGroup(), requestContext);
66             }
67         });
68     }
69
70     @Override
71     public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
72         getDeviceContext().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
73         return handleServiceCall(new Function<RequestContext<RemoveGroupOutput>,
74                 ListenableFuture<RpcResult<RemoveGroupOutput>>>() {
75
76             @Override
77             public ListenableFuture<RpcResult<RemoveGroupOutput>> apply(final RequestContext<RemoveGroupOutput> requestContext) {
78                 return convertAndSend(input, requestContext);
79             }
80         });
81     }
82
83     <T> ListenableFuture<RpcResult<T>> convertAndSend(final Group iputGroup, final RequestContext<T> requestContext) {
84         final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
85         getMessageSpy().spyMessage(iputGroup.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
86         final GroupModInputBuilder ofGroupModInput = GroupConvertor.toGroupModInput(iputGroup, getVersion(), getDatapathId());
87         final Xid xid = requestContext.getXid();
88         ofGroupModInput.setXid(xid.getValue());
89
90         final GroupModInput groupModInput = ofGroupModInput.build();
91         outboundQueue.commitEntry(xid.getValue(), groupModInput, new FutureCallback<OfHeader>() {
92             @Override
93             public void onSuccess(final OfHeader ofHeader) {
94                 RpcResultBuilder<T> rpcResultBuilder = RpcResultBuilder.success((T)ofHeader);
95                 requestContext.setResult(rpcResultBuilder.build());
96                 RequestContextUtil.closeRequstContext(requestContext);
97
98                 getMessageSpy().spyMessage(groupModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
99             }
100
101             @Override
102             public void onFailure(final Throwable throwable) {
103                 RpcResultBuilder<T> rpcResultBuilder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
104                 requestContext.setResult(rpcResultBuilder.build());
105                 RequestContextUtil.closeRequstContext(requestContext);
106
107                 getMessageSpy().spyMessage(groupModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
108             }
109         });
110         return requestContext.getFuture();
111     }
112 }