Fix errors in serializers and deserializers
[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.DeviceInfo;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
18 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
19 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
21 import org.opendaylight.openflowplugin.impl.statistics.SinglePurposeMultipartReplyTranslator;
22 import org.opendaylight.openflowplugin.impl.statistics.StatisticsGatheringUtils;
23 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
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 final SinglePurposeMultipartReplyTranslator multipartReplyTranslator;
39     private final DeviceInfo deviceInfo;
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 DeviceInfo deviceInfo,
52                                             final DeviceFlowRegistry registry,
53                                             final TxFacade txFacade,
54                                             final ConvertorExecutor convertorExecutor) {
55         super(context, requestType, messageSpy, eventIdentifier);
56
57         this.deviceInfo = deviceInfo;
58         this.registry = registry;
59         this.txFacade = txFacade;
60
61         multipartReplyTranslator = new SinglePurposeMultipartReplyTranslator(convertorExecutor);
62
63         //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
64         doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceInfo.getNodeId().toString());
65     }
66
67     public EventIdentifier getDoneEventIdentifier() {
68         return doneEventIdentifier;
69     }
70
71     @Override
72     public void onSuccess(final OfHeader result) {
73         if (result == null) {
74             LOG.info("Ofheader was null.");
75             if (!finished) {
76                 endCollecting();
77                 return;
78             }
79         } else if (finished) {
80             LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
81             return;
82         }
83
84         if (!(result instanceof MultipartReply)) {
85             LOG.info("Unexpected response type received {}.", result.getClass());
86             final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
87                     RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
88                             String.format("Unexpected response type received %s.", result.getClass()));
89             setResult(rpcResultBuilder.build());
90             endCollecting();
91         } else {
92             final MultipartReply multipartReply = (MultipartReply) result;
93
94             final MultipartReply singleReply = multipartReply;
95             final List<? extends DataObject> multipartDataList = multipartReplyTranslator.translate(
96                     deviceInfo.getDatapathId(), deviceInfo.getVersion(), singleReply);
97             final Iterable<? extends DataObject> allMultipartData = multipartDataList;
98
99             //TODO: following part is focused on flow stats only - need more general approach if used for more than flow stats
100             ListenableFuture<Void> future;
101             if (virgin) {
102                 future = StatisticsGatheringUtils.deleteAllKnownFlows(deviceInfo, registry, txFacade);
103                 virgin = false;
104             } else {
105                 future = Futures.immediateFuture(null);
106             }
107
108             Futures.transform(future, new Function<Void, Void>() {
109
110                 @Override
111                 public Void apply(final Void input) {
112                     StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData,
113                             deviceInfo, registry, txFacade);
114
115                     if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
116                         endCollecting();
117                     }
118                     return input;
119                 }
120             });
121         }
122     }
123
124     private void endCollecting() {
125         EventsTimeCounter.markEnd(getDoneEventIdentifier());
126         EventsTimeCounter.markEnd(getEventIdentifier());
127         final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
128         spyMessage(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
129         txFacade.submitTransaction();
130         setResult(rpcResult);
131         finished = true;
132     }
133 }