Decompose RPC implementation classes
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / RemoveFlowImpl.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.api.openflow.registry.flow.DeviceFlowRegistry;
19 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
20 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlow;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.common.Uint8;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public final class RemoveFlowImpl extends AbstractFlowRpc<RemoveFlowOutput> implements RemoveFlow {
34     private static final Logger LOG = LoggerFactory.getLogger(RemoveFlowImpl.class);
35
36     public RemoveFlowImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext,
37             final ConvertorExecutor convertorExecutor) {
38         super(requestContextStack, deviceContext, convertorExecutor, RemoveFlowOutput.class);
39     }
40
41     @Override
42     public ListenableFuture<RpcResult<RemoveFlowOutput>> invoke(final RemoveFlowInput input) {
43         final var future = single.canUseSingleLayerSerialization() ? single.handleServiceCall(input)
44             : multi.processFlowModInputBuilders(multi.toFlowModInputs(input));
45
46         Futures.addCallback(future, new RemoveFlowCallback(input), MoreExecutors.directExecutor());
47         return future;
48     }
49
50     private final class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
51         private static final Uint8 OFPTT_ALL = Uint8.MAX_VALUE;
52
53         private final RemoveFlowInput input;
54
55         private RemoveFlowCallback(final RemoveFlowInput input) {
56             this.input = input;
57         }
58
59         @Override
60         public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
61             if (result.isSuccessful()) {
62                 if (LOG.isDebugEnabled()) {
63                     LOG.debug("Flow remove finished without error for flow={}", input);
64                 }
65                 final DeviceFlowRegistry flowRegistry = deviceContext.getDeviceFlowRegistry();
66                 if (input.getTableId() != null && !input.getTableId().equals(OFPTT_ALL)) {
67                     var flowRegistryKey =
68                             FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
69                     flowRegistry.addMark(flowRegistryKey);
70
71                     final FlowRef flowRef = input.getFlowRef();
72                     if (flowRef != null) {
73                         final FlowId flowId = flowRef.getValue().firstKeyOf(Flow.class).getId();
74                         flowRegistry.appendHistoryFlow(flowId, input.getTableId(), FlowGroupStatus.REMOVED);
75                     }
76                 } else {
77                     flowRegistry.clearFlowRegistry();
78                 }
79             } else if (LOG.isDebugEnabled()) {
80                 LOG.debug("Flow remove failed for flow={}, errors={}", input,
81                         ErrorUtil.errorsToString(result.getErrors()));
82             }
83         }
84
85         @Override
86         public void onFailure(final Throwable throwable) {
87             LOG.warn("Service call for removing flow={} failed", input, throwable);
88         }
89     }
90 }