BUG-3741 - groups and meters failing
[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     public MultipartRequestOnTheFlyCallback(final RequestContext<List<MultipartReply>> context,
41                                             final Class<?> requestType,
42                                             final DeviceContext deviceContext,
43                                             final EventIdentifier eventIdentifier) {
44         super(context, requestType, deviceContext.getMessageSpy(), eventIdentifier);
45         this.deviceContext = deviceContext;
46         //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
47         doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceContext.getPrimaryConnectionContext().getNodeId().toString());
48     }
49
50     public EventIdentifier getDoneEventIdentifier() {
51         return doneEventIdentifier;
52     }
53
54     @Override
55     public void onSuccess(final OfHeader result) {
56         if (result == null) {
57             LOG.info("Ofheader was null.");
58             if (!finished) {
59                 endCollecting();
60                 return;
61             }
62         } else if (finished) {
63             LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
64             return;
65         }
66
67         if (!(result instanceof MultipartReply)) {
68             LOG.info("Unexpected response type received {}.", result.getClass());
69             final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
70                     RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
71                             String.format("Unexpected response type received %s.", result.getClass()));
72             setResult(rpcResultBuilder.build());
73             endCollecting();
74         } else {
75             MultipartReply multipartReply = (MultipartReply) result;
76
77             Iterable<? extends DataObject> allMultipartData = Collections.emptyList();
78             final MultipartReply singleReply = multipartReply;
79             final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
80             allMultipartData = Iterables.concat(allMultipartData, multipartDataList);
81
82             //TODO: following part is focused on flow stats only - need more general approach if used for more than flow stats
83             if (virgin) {
84                 StatisticsGatheringUtils.deleteAllKnownFlows(deviceContext);
85                 virgin = false;
86             }
87             StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData, deviceContext);
88             // ^^^^
89
90             if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
91                 endCollecting();
92             }
93         }
94     }
95
96     private void endCollecting() {
97         EventsTimeCounter.markEnd(getDoneEventIdentifier());
98         EventsTimeCounter.markEnd(getEventIdentifier());
99         final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
100         spyMessage(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
101         setResult(rpcResult);
102         deviceContext.submitTransaction();
103         finished = true;
104     }
105 }