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