Remove unused routedRpcRegistration
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / 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.sal;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import org.opendaylight.openflowplugin.api.openflow.FlowGroupStatus;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerGroupService;
18 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerGroupService;
19 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
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.service.rev130918.group.update.UpdatedGroup;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class SalGroupServiceImpl implements SalGroupService {
35     private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
36     private final MultiLayerGroupService<AddGroupInput, AddGroupOutput> addGroup;
37     private final MultiLayerGroupService<Group, UpdateGroupOutput> updateGroup;
38     private final MultiLayerGroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
39     private final SingleLayerGroupService<AddGroupOutput> addGroupMessage;
40     private final SingleLayerGroupService<UpdateGroupOutput> updateGroupMessage;
41     private final SingleLayerGroupService<RemoveGroupOutput> removeGroupMessage;
42
43     private final DeviceContext deviceContext;
44
45     public SalGroupServiceImpl(final RequestContextStack requestContextStack,
46                                final DeviceContext deviceContext,
47                                final ConvertorExecutor convertorExecutor) {
48         this.deviceContext = deviceContext;
49         addGroup = new MultiLayerGroupService<>(requestContextStack,
50                                                 deviceContext,
51                                                 AddGroupOutput.class,
52                                                 convertorExecutor);
53
54         updateGroup = new MultiLayerGroupService<>(requestContextStack,
55                                                    deviceContext,
56                                                    UpdateGroupOutput.class,
57                                                    convertorExecutor);
58
59         removeGroup = new MultiLayerGroupService<>(requestContextStack,
60                                                    deviceContext,
61                                                    RemoveGroupOutput.class,
62                                                    convertorExecutor);
63
64         addGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
65         updateGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
66         removeGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
67     }
68
69     @Override
70     public ListenableFuture<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
71         final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture =
72             addGroupMessage.canUseSingleLayerSerialization()
73             ? addGroupMessage.handleServiceCall(input)
74             : addGroup.handleServiceCall(input);
75         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
76             @Override
77             public void onSuccess(final RpcResult<AddGroupOutput> result) {
78                 if (result.isSuccessful()) {
79                     LOG.debug("adding group successful {}", input.getGroupId());
80                     deviceContext.getDeviceGroupRegistry().appendHistoryGroup(input.getGroupId(), input.getGroupType(),
81                         FlowGroupStatus.ADDED);
82                 } else {
83                     if (LOG.isDebugEnabled()) {
84                         LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
85                             ErrorUtil.errorsToString(result.getErrors()));
86                     }
87                 }
88             }
89
90             @Override
91             public void onFailure(final Throwable throwable) {
92                 LOG.warn("Service call for adding group={} failed",
93                           input.getGroupId().getValue(),
94                           throwable);
95             }
96         }, MoreExecutors.directExecutor());
97         return resultFuture;
98     }
99
100
101     @Override
102     public ListenableFuture<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
103         final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture =
104             updateGroupMessage.canUseSingleLayerSerialization()
105             ? updateGroupMessage.handleServiceCall(input.getUpdatedGroup())
106             : updateGroup.handleServiceCall(input.getUpdatedGroup());
107
108         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
109             @Override
110             public void onSuccess(final RpcResult<UpdateGroupOutput> result) {
111                 if (result.isSuccessful()) {
112                     UpdatedGroup updatedGroup = input.getUpdatedGroup();
113                     deviceContext.getDeviceGroupRegistry().appendHistoryGroup(
114                         updatedGroup.getGroupId(), updatedGroup.getGroupType(), FlowGroupStatus.MODIFIED);
115                     if (LOG.isDebugEnabled()) {
116                         LOG.debug("Group update with original id={} finished without error",
117                             input.getOriginalGroup().getGroupId().getValue());
118                     }
119                 } else {
120                     LOG.warn("Group update with original id={} failed, errors={}",
121                         input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
122                     LOG.debug("Group input={}", input.getUpdatedGroup());
123                 }
124             }
125
126             @Override
127             public void onFailure(final Throwable throwable) {
128                 LOG.warn("Service call for updating group={} failed",
129                         input.getOriginalGroup().getGroupId(), throwable);
130             }
131         }, MoreExecutors.directExecutor());
132         return resultFuture;
133     }
134
135     @Override
136     public ListenableFuture<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
137         final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture =
138             removeGroupMessage.canUseSingleLayerSerialization()
139             ? removeGroupMessage.handleServiceCall(input)
140             : removeGroup.handleServiceCall(input);
141
142         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
143             @Override
144             public void onSuccess(final RpcResult<RemoveGroupOutput> result) {
145                 if (result.isSuccessful()) {
146                     if (LOG.isDebugEnabled()) {
147                         LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
148                         deviceContext.getDeviceGroupRegistry().appendHistoryGroup(input.getGroupId(),
149                             input.getGroupType(), FlowGroupStatus.REMOVED);
150                     }
151                 } else {
152                     LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
153                         ErrorUtil.errorsToString(result.getErrors()));
154                     LOG.debug("Group input={}", input);
155                 }
156             }
157
158             @Override
159             public void onFailure(final Throwable throwable) {
160                 LOG.warn("Service call for removing group={} failed",
161                         input.getGroupId().getValue(), throwable);
162             }
163         }, MoreExecutors.directExecutor());
164         return resultFuture;
165     }
166
167 }