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