Remove Objects.{is,non}Null abuse
[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 javax.annotation.Nonnull;
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 ListenableFuture<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(@Nonnull RpcResult<AddGroupOutput> result) {
78                 if (result.isSuccessful()) {
79                     LOG.debug("adding group successful {}", input.getGroupId().getValue());
80                 } else {
81                     if (LOG.isDebugEnabled()) {
82                         LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
83                             ErrorUtil.errorsToString(result.getErrors()));
84                     }
85                 }
86             }
87
88             @Override
89             public void onFailure(Throwable throwable) {
90                 LOG.warn("Service call for adding group={} failed",
91                           input.getGroupId().getValue(),
92                           throwable);
93             }
94         }, MoreExecutors.directExecutor());
95         return resultFuture;
96     }
97
98
99     @Override
100     public ListenableFuture<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
101         final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture =
102             updateGroupMessage.canUseSingleLayerSerialization()
103             ? updateGroupMessage.handleServiceCall(input.getUpdatedGroup())
104             : updateGroup.handleServiceCall(input.getUpdatedGroup());
105
106         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
107             @Override
108             public void onSuccess(@Nonnull RpcResult<UpdateGroupOutput> result) {
109                 if (result.isSuccessful()) {
110                     if (LOG.isDebugEnabled()) {
111                         LOG.debug("Group update with original id={} finished without error",
112                             input.getOriginalGroup().getGroupId().getValue());
113                     }
114                 } else {
115                     LOG.warn("Group update with original id={} failed, errors={}",
116                         input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
117                     LOG.debug("Group input={}", input.getUpdatedGroup());
118                 }
119             }
120
121             @Override
122             public void onFailure(Throwable throwable) {
123                 LOG.warn("Service call for updating group={} failed",
124                         input.getOriginalGroup().getGroupId(), throwable);
125             }
126         }, MoreExecutors.directExecutor());
127         return resultFuture;
128     }
129
130     @Override
131     public ListenableFuture<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
132         final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture =
133             removeGroupMessage.canUseSingleLayerSerialization()
134             ? removeGroupMessage.handleServiceCall(input)
135             : removeGroup.handleServiceCall(input);
136
137         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
138             @Override
139             public void onSuccess(@Nonnull RpcResult<RemoveGroupOutput> result) {
140                 if (result.isSuccessful()) {
141                     if (LOG.isDebugEnabled()) {
142                         LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
143                     }
144                 } else {
145                     LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
146                         ErrorUtil.errorsToString(result.getErrors()));
147                     LOG.debug("Group input={}", input);
148                 }
149             }
150
151             @Override
152             public void onFailure(Throwable throwable) {
153                 LOG.warn("Service call for removing group={} failed",
154                         input.getGroupId().getValue(), throwable);
155             }
156         }, MoreExecutors.directExecutor());
157         return resultFuture;
158     }
159
160 }