Merge "Bug 8497 - Provide config knob to disable the Forwarding Rule Manager reconcil...
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / multilayer / MultiLayerFlowMultipartRequestOnTheFlyCallback.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.impl.services.multilayer;
10
11 import com.google.common.base.Function;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.List;
15 import java.util.Optional;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
19 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
21 import org.opendaylight.openflowplugin.impl.common.MultipartReplyTranslatorUtil;
22 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
23 import org.opendaylight.openflowplugin.impl.services.AbstractMultipartRequestOnTheFlyCallback;
24 import org.opendaylight.openflowplugin.impl.statistics.StatisticsGatheringUtils;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.multipart.reply.MultipartReplyBody;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
31
32 public class MultiLayerFlowMultipartRequestOnTheFlyCallback<T extends OfHeader> extends AbstractMultipartRequestOnTheFlyCallback<T> {
33
34     private final ConvertorExecutor convertorExecutor;
35     private final DeviceInfo deviceInfo;
36     private final DeviceFlowRegistry deviceFlowRegistry;
37     private boolean virgin = true;
38
39     public MultiLayerFlowMultipartRequestOnTheFlyCallback(final RequestContext<List<T>> context,
40                                                           final Class<?> requestType,
41                                                           final DeviceContext deviceContext,
42                                                           final EventIdentifier eventIdentifier,
43                                                           final MultipartWriterProvider statisticsWriterProvider,
44                                                           final ConvertorExecutor convertorExecutor) {
45         super(context, requestType, deviceContext, eventIdentifier, statisticsWriterProvider);
46         this.convertorExecutor = convertorExecutor;
47         deviceInfo = deviceContext.getDeviceInfo();
48         deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
49     }
50
51     @Override
52     protected boolean isMultipart(OfHeader result) {
53         return result instanceof MultipartReply
54             && MultipartReply.class.cast(result).getType().equals(getMultipartType());
55     }
56
57     @Override
58     protected boolean isReqMore(T result) {
59         return MultipartReply.class.cast(result).getFlags().isOFPMPFREQMORE();
60     }
61
62     @Override
63     protected MultipartType getMultipartType() {
64         return MultipartType.OFPMPFLOW;
65     }
66
67     @Override
68     protected void onFinishedCollecting() {
69         deviceFlowRegistry.processMarks();
70     }
71
72     @Override
73     protected ListenableFuture<Optional<? extends MultipartReplyBody>> processStatistics(T result) {
74         final ListenableFuture<Optional<? extends MultipartReplyBody>> future = Futures.transform(
75             StatisticsGatheringUtils.deleteAllKnownFlows(
76                 getTxFacade(),
77                 deviceInfo
78                     .getNodeInstanceIdentifier()
79                     .augmentation(FlowCapableNode.class),
80                 !virgin),
81             (Function<Void, Optional<? extends MultipartReplyBody>>) input -> MultipartReplyTranslatorUtil
82                 .translate(result, deviceInfo, convertorExecutor, null));
83
84         if (virgin) {
85             virgin = false;
86         }
87
88         return future;
89     }
90
91 }