Bug-3104: Bypassing MultimessageCollector
[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.collect.Iterables;
11 import java.util.Collections;
12 import java.util.List;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
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.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.common.RpcError;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 final class MultipartRequestOnTheFlyCallback extends AbstractRequestCallback<List<MultipartReply>> {
32     private static final Logger LOG = LoggerFactory.getLogger(MultipartRequestOnTheFlyCallback.class);
33     private final DeviceContext deviceContext;
34     private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
35     private boolean virgin = true;
36     private boolean finished = false;
37     private final EventIdentifier doneEventIdentifier;
38
39
40
41     public MultipartRequestOnTheFlyCallback(final RequestContext<List<MultipartReply>> context,
42                                             final Class<?> requestType,
43                                             final DeviceContext deviceContext,
44                                             final EventIdentifier eventIdentifier) {
45         super(context, requestType, deviceContext.getMessageSpy(), eventIdentifier);
46         this.deviceContext = deviceContext;
47         //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
48         doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceContext.getPrimaryConnectionContext().getNodeId().toString());
49     }
50
51     public EventIdentifier getDoneEventIdentifier() {
52         return doneEventIdentifier;
53     }
54
55     @Override
56     public void onSuccess(final OfHeader result) {
57         if (result == null) {
58             LOG.info("Ofheader was null.");
59             if (!finished) {
60                 endCollecting();
61                 return;
62             }
63         } else if (finished) {
64             LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
65             return;
66         }
67
68         if (!(result instanceof MultipartReply)) {
69             LOG.info("Unexpected response type received {}.", result.getClass());
70             final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
71                     RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
72                             String.format("Unexpected response type received %s.", result.getClass()));
73             setResult(rpcResultBuilder.build());
74             endCollecting();
75         } else {
76             MultipartReply multipartReply = (MultipartReply) result;
77
78             Iterable<? extends DataObject> allMultipartData = Collections.emptyList();
79             final MultipartReply singleReply = multipartReply;
80             final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
81             allMultipartData = Iterables.concat(allMultipartData, multipartDataList);
82
83             //TODO: following part is focused on flow stats only - need more general approach if used for more than flow stats
84             if (virgin) {
85                 StatisticsGatheringUtils.deleteAllKnownFlows(deviceContext, deviceContext.getDeviceState().getNodeInstanceIdentifier());
86                 virgin = false;
87             }
88             StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData, deviceContext);
89             // ^^^^
90
91             if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
92                 endCollecting();
93             }
94         }
95     }
96
97     private void endCollecting() {
98         EventsTimeCounter.markEnd(getDoneEventIdentifier());
99         EventsTimeCounter.markEnd(getEventIdentifier());
100         final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
101         spyMessage(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
102         setResult(rpcResult);
103         deviceContext.submitTransaction();
104         finished = true;
105     }
106 }