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