Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / MultipartRequestOnTheFlyCallback.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 package org.opendaylight.openflowplugin.impl.services;
9
10 import com.google.common.base.Optional;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
13 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
14 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
15 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
16 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
17 import org.opendaylight.openflowplugin.impl.statistics.SinglePurposeMultipartReplyTranslator;
18 import org.opendaylight.openflowplugin.impl.statistics.StatisticsGatheringUtils;
19 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.common.RpcError;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.concurrent.atomic.AtomicBoolean;
36
37 final class MultipartRequestOnTheFlyCallback extends AbstractRequestCallback<List<MultipartReply>> {
38     private static final Logger LOG = LoggerFactory.getLogger(MultipartRequestOnTheFlyCallback.class);
39     private final SinglePurposeMultipartReplyTranslator multipartReplyTranslator;
40     private final DeviceInfo deviceInfo;
41     private final DeviceFlowRegistry registry;
42     private final EventIdentifier doneEventIdentifier;
43     private final TxFacade txFacade;
44
45     private Optional<FlowCapableNode> fcNodeOpt;
46     private AtomicBoolean virgin = new AtomicBoolean(true);
47     private AtomicBoolean finished = new AtomicBoolean(false);
48
49     public MultipartRequestOnTheFlyCallback(final RequestContext<List<MultipartReply>> context,
50                                             final Class<?> requestType,
51                                             final MessageSpy messageSpy,
52                                             final EventIdentifier eventIdentifier,
53                                             final DeviceInfo deviceInfo,
54                                             final DeviceFlowRegistry registry,
55                                             final TxFacade txFacade,
56                                             final ConvertorExecutor convertorExecutor) {
57         super(context, requestType, messageSpy, eventIdentifier);
58
59         this.deviceInfo = deviceInfo;
60         this.registry = registry;
61         this.txFacade = txFacade;
62
63         multipartReplyTranslator = new SinglePurposeMultipartReplyTranslator(convertorExecutor);
64
65         //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
66         doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceInfo.getNodeId().toString());
67     }
68
69     public EventIdentifier getDoneEventIdentifier() {
70         return doneEventIdentifier;
71     }
72
73     @Override
74     public void onSuccess(final OfHeader result) {
75
76         if (result == null) {
77             LOG.info("Ofheader was null.");
78             if (!finished.getAndSet(true)) {
79                 endCollecting();
80                 return;
81             }
82         } else if (finished.get()) {
83             LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
84             return;
85         }
86
87         if (!(result instanceof MultipartReply)) {
88             if(!finished.getAndSet(true)) {
89                 LOG.info("Unexpected response type received {}.", result.getClass());
90                 final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
91                         RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
92                                 String.format("Unexpected response type received %s.", result.getClass()));
93                 setResult(rpcResultBuilder.build());
94                 endCollecting();
95             }
96         } else {
97             final MultipartReply multipartReply = (MultipartReply) result;
98             if (virgin.get()) {
99                 synchronized (this) {
100                     if (virgin.get()) {
101                         fcNodeOpt = StatisticsGatheringUtils.deleteAllKnownFlows(deviceInfo, txFacade);
102                         virgin.set(false);
103                     }
104                 }
105             }
106
107             final MultipartReply singleReply = multipartReply;
108             final List<? extends DataObject> multipartDataList = multipartReplyTranslator.translate(
109                     deviceInfo.getDatapathId(), deviceInfo.getVersion(), singleReply);
110             final Iterable<FlowsStatisticsUpdate> allMultipartData = (Iterable<FlowsStatisticsUpdate>) multipartDataList;
111
112             StatisticsGatheringUtils.writeFlowStatistics(allMultipartData, deviceInfo, registry, txFacade);
113             if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
114                 endCollecting();
115             }
116         }
117     }
118
119     private void endCollecting() {
120         finished.set(true);
121         EventsTimeCounter.markEnd(getDoneEventIdentifier());
122         EventsTimeCounter.markEnd(getEventIdentifier());
123         final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
124         spyMessage(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
125         setResult(rpcResult);
126         txFacade.submitTransaction();
127     }
128 }