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;
46 private final DeviceState deviceState;
49 public MultipartRequestOnTheFlyCallback(final RequestContext<List<MultipartReply>> context,
50 final Class<?> requestType,
51 final MessageSpy messageSpy,
52 final EventIdentifier eventIdentifier,
53 final DeviceInfo deviceInfo,
54 final DeviceFlowRegistry registry,
55 final TxFacade txFacade,
56 final DeviceState deviceState) {
57 super(context, requestType, messageSpy, eventIdentifier);
59 this.deviceInfo = deviceInfo;
60 this.registry = registry;
61 this.txFacade = txFacade;
62 this.deviceState = deviceState;
64 //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
65 doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceInfo.getNodeId().toString());
68 public EventIdentifier getDoneEventIdentifier() {
69 return doneEventIdentifier;
73 public void onSuccess(final OfHeader result) {
75 LOG.info("Ofheader was null.");
80 } else if (finished) {
81 LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
85 if (!(result instanceof MultipartReply)) {
86 LOG.info("Unexpected response type received {}.", result.getClass());
87 final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
88 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
89 String.format("Unexpected response type received %s.", result.getClass()));
90 setResult(rpcResultBuilder.build());
93 final MultipartReply multipartReply = (MultipartReply) result;
95 final MultipartReply singleReply = multipartReply;
96 final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(
97 deviceInfo.getDatapathId(), deviceInfo.getVersion(), singleReply);
98 final Iterable<? extends DataObject> allMultipartData = multipartDataList;
100 //TODO: following part is focused on flow stats only - need more general approach if used for more than flow stats
101 ListenableFuture<Void> future;
103 future = StatisticsGatheringUtils.deleteAllKnownFlows(deviceInfo, registry, txFacade, deviceState);
106 future = Futures.immediateFuture(null);
109 Futures.transform(future, new Function<Void, Void>() {
112 public Void apply(final Void input) {
113 StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData,
114 deviceInfo, registry, txFacade);
116 if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
125 private void endCollecting() {
126 EventsTimeCounter.markEnd(getDoneEventIdentifier());
127 EventsTimeCounter.markEnd(getEventIdentifier());
128 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
129 spyMessage(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
130 setResult(rpcResult);
131 txFacade.submitTransaction();