CommonService has its fields private
[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.JdkFutureAdapters;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.Future;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
16 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.GroupConvertor;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.slf4j.Logger;
30
31 public class SalGroupServiceImpl extends CommonService implements SalGroupService {
32
33
34     public SalGroupServiceImpl(RequestContextStack requestContextStack, DeviceContext deviceContext) {
35         super(requestContextStack, deviceContext);
36     }
37
38     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalGroupServiceImpl.class);
39
40     @Override
41     public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
42         getDeviceContext().getDeviceGroupRegistry().store(input.getGroupId());
43         return this.<AddGroupOutput, Void>handleServiceCall(getPrimaryConnection(),
44                 new Function<DataCrate<AddGroupOutput>, ListenableFuture<RpcResult<Void>>>() {
45
46                     @Override
47                     public ListenableFuture<RpcResult<Void>> apply(final DataCrate<AddGroupOutput> data) {
48                         return convertAndSend(input, data);
49                     }
50                 });
51     }
52
53     @Override
54     public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
55         return this.<UpdateGroupOutput, Void>handleServiceCall(getPrimaryConnection(),
56                 new Function<DataCrate<UpdateGroupOutput>, ListenableFuture<RpcResult<Void>>>() {
57
58                     @Override
59                     public ListenableFuture<RpcResult<Void>> apply(final DataCrate<UpdateGroupOutput> data) {
60                         return convertAndSend(input.getUpdatedGroup(), data);
61                     }
62                 });
63     }
64
65     @Override
66     public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
67         getDeviceContext().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
68         return this.<RemoveGroupOutput, Void>handleServiceCall(getPrimaryConnection(),
69                 new Function<DataCrate<RemoveGroupOutput>, ListenableFuture<RpcResult<Void>>>() {
70
71                     @Override
72                     public ListenableFuture<RpcResult<Void>> apply(final DataCrate<RemoveGroupOutput> data) {
73                         return convertAndSend(input, data);
74                     }
75                 });
76     }
77
78     <T> ListenableFuture<RpcResult<Void>> convertAndSend(final Group iputGroup, final DataCrate<T> data) {
79         getMessageSpy().spyMessage(iputGroup.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_SUCCESS);
80         final GroupModInputBuilder ofGroupModInput = GroupConvertor.toGroupModInput(iputGroup, getVersion(), getDatapathId());
81         final Xid xid = data.getRequestContext().getXid();
82         ofGroupModInput.setXid(xid.getValue());
83         return JdkFutureAdapters.listenInPoolThread(provideConnectionAdapter(data.getiDConnection()).groupMod(ofGroupModInput.build()));
84     }
85 }