2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services;
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;
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;
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);
57 this.deviceInfo = deviceInfo;
58 this.registry = registry;
59 this.txFacade = txFacade;
61 multipartReplyTranslator = new SinglePurposeMultipartReplyTranslator(convertorExecutor);
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());
67 public EventIdentifier getDoneEventIdentifier() {
68 return doneEventIdentifier;
72 public void onSuccess(final OfHeader result) {
74 LOG.info("Ofheader was null.");
79 } else if (finished) {
80 LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
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());
92 final MultipartReply multipartReply = (MultipartReply) result;
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;
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;
102 future = StatisticsGatheringUtils.deleteAllKnownFlows(deviceInfo, registry, txFacade);
105 future = Futures.immediateFuture(null);
108 Futures.transform(future, new Function<Void, Void>() {
111 public Void apply(final Void input) {
112 StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData,
113 deviceInfo, registry, txFacade);
115 if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
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);