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