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.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;
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;
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());
52 public EventIdentifier getDoneEventIdentifier() {
53 return doneEventIdentifier;
57 public void onSuccess(final OfHeader result) {
59 LOG.info("Ofheader was null.");
64 } else if (finished) {
65 LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
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());
77 final MultipartReply multipartReply = (MultipartReply) result;
79 final MultipartReply singleReply = multipartReply;
80 final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
81 final Iterable<? extends DataObject> allMultipartData = multipartDataList;
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;
86 future = StatisticsGatheringUtils.deleteAllKnownFlows(deviceContext);
89 future = Futures.immediateFuture(null);
92 Futures.transform(future, new Function<Void, Void>() {
95 public Void apply(final Void input) {
96 StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData,deviceContext);
98 if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
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();