Decompose RPC implementation classes
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / AddGroupImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  * Copyright (c) 2024 PANTHEON.tech, s.r.o.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.openflowplugin.impl.services.sal;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import org.opendaylight.openflowplugin.api.openflow.FlowGroupStatus;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
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.AddGroup;
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.yangtools.yang.common.RpcResult;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public final class AddGroupImpl extends AbstractGroupRpc<AddGroupInput, AddGroupOutput> implements AddGroup {
28     private static final Logger LOG = LoggerFactory.getLogger(AddGroupImpl.class);
29
30     public AddGroupImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext,
31             final ConvertorExecutor convertorExecutor) {
32         super(requestContextStack, deviceContext, convertorExecutor, AddGroupOutput.class);
33     }
34
35     @Override
36     public ListenableFuture<RpcResult<AddGroupOutput>> invoke(final AddGroupInput input) {
37         final var resultFuture = single.canUseSingleLayerSerialization() ? single.handleServiceCall(input)
38             : multi.handleServiceCall(input);
39         Futures.addCallback(resultFuture, new FutureCallback<>() {
40             @Override
41             public void onSuccess(final RpcResult<AddGroupOutput> result) {
42                 if (result.isSuccessful()) {
43                     LOG.debug("adding group successful {}", input.getGroupId());
44                     deviceContext.getDeviceGroupRegistry().appendHistoryGroup(input.getGroupId(), input.getGroupType(),
45                         FlowGroupStatus.ADDED);
46                 } else if (LOG.isDebugEnabled()) {
47                     LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
48                         ErrorUtil.errorsToString(result.getErrors()));
49                 }
50             }
51
52             @Override
53             public void onFailure(final Throwable throwable) {
54                 LOG.warn("Service call for adding group={} failed",
55                           input.getGroupId().getValue(),
56                           throwable);
57             }
58         }, MoreExecutors.directExecutor());
59         return resultFuture;
60     }
61 }