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