Merge "Bug-4957: Make async operational DS Read"
[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.Function;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
19 import org.opendaylight.openflowplugin.impl.statistics.SinglePurposeMultipartReplyTranslator;
20 import org.opendaylight.openflowplugin.impl.statistics.StatisticsGatheringUtils;
21 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
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 final class MultipartRequestOnTheFlyCallback extends AbstractRequestCallback<List<MultipartReply>> {
34     private static final Logger LOG = LoggerFactory.getLogger(MultipartRequestOnTheFlyCallback.class);
35     private final DeviceContext deviceContext;
36     private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
37     private boolean virgin = true;
38     private boolean finished = false;
39     private final EventIdentifier doneEventIdentifier;
40
41
42     public MultipartRequestOnTheFlyCallback(final RequestContext<List<MultipartReply>> context,
43                                             final Class<?> requestType,
44                                             final DeviceContext deviceContext,
45                                             final EventIdentifier eventIdentifier) {
46         super(context, requestType, deviceContext.getMessageSpy(), eventIdentifier);
47         this.deviceContext = deviceContext;
48         //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
49         doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceContext.getPrimaryConnectionContext().getNodeId().toString());
50     }
51
52     public EventIdentifier getDoneEventIdentifier() {
53         return doneEventIdentifier;
54     }
55
56     @Override
57     public void onSuccess(final OfHeader result) {
58         if (result == null) {
59             LOG.info("Ofheader was null.");
60             if (!finished) {
61                 endCollecting();
62                 return;
63             }
64         } else if (finished) {
65             LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
66             return;
67         }
68
69         if (!(result instanceof MultipartReply)) {
70             LOG.info("Unexpected response type received {}.", result.getClass());
71             final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
72                     RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
73                             String.format("Unexpected response type received %s.", result.getClass()));
74             setResult(rpcResultBuilder.build());
75             endCollecting();
76         } else {
77             final MultipartReply multipartReply = (MultipartReply) result;
78
79             final MultipartReply singleReply = multipartReply;
80             final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
81             final Iterable<? extends DataObject> 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             ListenableFuture<Void> future;
85             if (virgin) {
86                 future = StatisticsGatheringUtils.deleteAllKnownFlows(deviceContext);
87                 virgin = false;
88             } else {
89                 future = Futures.immediateFuture(null);
90             }
91
92             Futures.transform(future, new Function<Void, Void>() {
93
94                 @Override
95                 public Void apply(final Void input) {
96                     StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData,deviceContext);
97
98                     if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
99                         endCollecting();
100                     }
101                     return input;
102                 }
103             });
104         }
105     }
106
107     private void endCollecting() {
108         EventsTimeCounter.markEnd(getDoneEventIdentifier());
109         EventsTimeCounter.markEnd(getEventIdentifier());
110         final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
111         spyMessage(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
112         setResult(rpcResult);
113         deviceContext.submitTransaction();
114         finished = true;
115     }
116 }