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.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.common.RpcError;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 final class MultipartRequestOnTheFlyCallback extends AbstractRequestCallback<List<MultipartReply>> {
36 private static final Logger LOG = LoggerFactory.getLogger(MultipartRequestOnTheFlyCallback.class);
37 private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
38 private final DeviceInfo deviceInfo;
39 private final DeviceFlowRegistry registry;
40 private boolean virgin = true;
41 private boolean finished = false;
42 private final EventIdentifier doneEventIdentifier;
43 private final TxFacade txFacade;
46 public MultipartRequestOnTheFlyCallback(final RequestContext<List<MultipartReply>> context,
47 final Class<?> requestType,
48 final MessageSpy messageSpy,
49 final EventIdentifier eventIdentifier,
50 final DeviceInfo deviceInfo,
51 final DeviceFlowRegistry registry,
52 final TxFacade txFacade) {
53 super(context, requestType, messageSpy, eventIdentifier);
55 this.deviceInfo = deviceInfo;
56 this.registry = registry;
57 this.txFacade = txFacade;
59 //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
60 doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceInfo.getNodeId().toString());
63 public EventIdentifier getDoneEventIdentifier() {
64 return doneEventIdentifier;
68 public void onSuccess(final OfHeader result) {
70 LOG.info("Ofheader was null.");
75 } else if (finished) {
76 LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
80 if (!(result instanceof MultipartReply)) {
81 LOG.info("Unexpected response type received {}.", result.getClass());
82 final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
83 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
84 String.format("Unexpected response type received %s.", result.getClass()));
85 setResult(rpcResultBuilder.build());
88 final MultipartReply multipartReply = (MultipartReply) result;
90 final MultipartReply singleReply = multipartReply;
91 final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(
92 deviceInfo.getDatapathId(), deviceInfo.getVersion(), singleReply);
93 final Iterable<? extends DataObject> allMultipartData = multipartDataList;
95 //TODO: following part is focused on flow stats only - need more general approach if used for more than flow stats
96 ListenableFuture<Void> future;
98 future = StatisticsGatheringUtils.deleteAllKnownFlows(deviceInfo, registry, txFacade);
101 future = Futures.immediateFuture(null);
104 Futures.transform(future, new Function<Void, Void>() {
107 public Void apply(final Void input) {
108 StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData,
109 deviceInfo, registry, txFacade);
111 if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
120 private void endCollecting() {
121 EventsTimeCounter.markEnd(getDoneEventIdentifier());
122 EventsTimeCounter.markEnd(getEventIdentifier());
123 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
124 spyMessage(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
125 txFacade.submitTransaction();
126 setResult(rpcResult);