Decompose RPC implementation classes
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / AddMeterImpl.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.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeter;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public final class AddMeterImpl extends AbstractMeterRpc<AddMeterInput, AddMeterOutput> implements AddMeter {
27     private static final Logger LOG = LoggerFactory.getLogger(AddMeterImpl.class);
28
29     public AddMeterImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext,
30             final ConvertorExecutor convertorExecutor) {
31         super(requestContextStack, deviceContext, convertorExecutor, AddMeterOutput.class);
32     }
33
34     @Override
35     public ListenableFuture<RpcResult<AddMeterOutput>> invoke(final AddMeterInput input) {
36         final var resultFuture = single.canUseSingleLayerSerialization() ? single.handleServiceCall(input)
37             : multi.handleServiceCall(input);
38
39         Futures.addCallback(resultFuture, new FutureCallback<>() {
40             @Override
41             public void onSuccess(final RpcResult<AddMeterOutput> result) {
42                 if (result.isSuccessful()) {
43                     if (LOG.isDebugEnabled()) {
44                         LOG.debug("Meter add with id={} finished without error", input.getMeterId());
45                     }
46                 } else if (LOG.isDebugEnabled()) {
47                     LOG.debug("Meter add with id={} failed, errors={}", input.getMeterId(),
48                         ErrorUtil.errorsToString(result.getErrors()));
49                 }
50             }
51
52             @Override
53             public void onFailure(final Throwable throwable) {
54                 LOG.warn("Service call for adding meter={} failed", input.getMeterId(), throwable);
55             }
56         }, MoreExecutors.directExecutor());
57         return resultFuture;
58     }
59 }