Merge "BUG-4117: preparation for FlowCapableNode notification impl."
[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
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
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.api.openflow.statistics.ofpspecific.MessageSpy;
22 import org.opendaylight.openflowplugin.impl.statistics.SinglePurposeMultipartReplyTranslator;
23 import org.opendaylight.openflowplugin.impl.statistics.StatisticsGatheringUtils;
24 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.common.RpcError;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 final class MultipartRequestOnTheFlyCallback extends AbstractRequestCallback<List<MultipartReply>> {
37     private static final Logger LOG = LoggerFactory.getLogger(MultipartRequestOnTheFlyCallback.class);
38     private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
39     private final DeviceState deviceState;
40     private final DeviceFlowRegistry registry;
41     private boolean virgin = true;
42     private boolean finished = false;
43     private final EventIdentifier doneEventIdentifier;
44     private final TxFacade txFacade;
45
46
47     public MultipartRequestOnTheFlyCallback(final RequestContext<List<MultipartReply>> context,
48                                             final Class<?> requestType,
49                                             final MessageSpy messageSpy,
50                                             final EventIdentifier eventIdentifier,
51                                             final DeviceState deviceState,
52                                             final DeviceFlowRegistry registry,
53                                             final TxFacade txFacade) {
54         super(context, requestType, messageSpy, eventIdentifier);
55
56         this.deviceState = deviceState;
57         this.registry = registry;
58         this.txFacade = txFacade;
59
60         //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
61         doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceState.getNodeId().toString());
62     }
63
64     public EventIdentifier getDoneEventIdentifier() {
65         return doneEventIdentifier;
66     }
67
68     @Override
69     public void onSuccess(final OfHeader result) {
70         if (result == null) {
71             LOG.info("Ofheader was null.");
72             if (!finished) {
73                 endCollecting();
74                 return;
75             }
76         } else if (finished) {
77             LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
78             return;
79         }
80
81         if (!(result instanceof MultipartReply)) {
82             LOG.info("Unexpected response type received {}.", result.getClass());
83             final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
84                     RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
85                             String.format("Unexpected response type received %s.", result.getClass()));
86             setResult(rpcResultBuilder.build());
87             endCollecting();
88         } else {
89             final MultipartReply multipartReply = (MultipartReply) result;
90
91             final MultipartReply singleReply = multipartReply;
92             final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(
93                     deviceState.getFeatures().getDatapathId(), deviceState.getFeatures().getVersion(), singleReply);
94             final Iterable<? extends DataObject> allMultipartData = multipartDataList;
95
96             //TODO: following part is focused on flow stats only - need more general approach if used for more than flow stats
97             ListenableFuture<Void> future;
98             if (virgin) {
99                 future = StatisticsGatheringUtils.deleteAllKnownFlows(deviceState, registry, txFacade);
100                 virgin = false;
101             } else {
102                 future = Futures.immediateFuture(null);
103             }
104
105             Futures.transform(future, new Function<Void, Void>() {
106
107                 @Override
108                 public Void apply(final Void input) {
109                     StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData,
110                             deviceState, registry, txFacade);
111
112                     if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
113                         endCollecting();
114                     }
115                     return input;
116                 }
117             });
118         }
119     }
120
121     private void endCollecting() {
122         EventsTimeCounter.markEnd(getDoneEventIdentifier());
123         EventsTimeCounter.markEnd(getEventIdentifier());
124         final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
125         spyMessage(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
126         setResult(rpcResult);
127         txFacade.submitTransaction();
128         finished = true;
129     }
130 }