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.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;
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;
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);
56 this.deviceState = deviceState;
57 this.registry = registry;
58 this.txFacade = txFacade;
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());
64 public EventIdentifier getDoneEventIdentifier() {
65 return doneEventIdentifier;
69 public void onSuccess(final OfHeader result) {
71 LOG.info("Ofheader was null.");
76 } else if (finished) {
77 LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
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());
89 final MultipartReply multipartReply = (MultipartReply) result;
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;
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;
99 future = StatisticsGatheringUtils.deleteAllKnownFlows(deviceState, registry, txFacade);
102 future = Futures.immediateFuture(null);
105 Futures.transform(future, new Function<Void, Void>() {
108 public Void apply(final Void input) {
109 StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData,
110 deviceState, registry, txFacade);
112 if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
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 txFacade.submitTransaction();
127 setResult(rpcResult);