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.Optional;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
13 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
14 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
15 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
16 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
17 import org.opendaylight.openflowplugin.impl.statistics.SinglePurposeMultipartReplyTranslator;
18 import org.opendaylight.openflowplugin.impl.statistics.StatisticsGatheringUtils;
19 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
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 import java.util.Collections;
34 import java.util.List;
35 import java.util.concurrent.atomic.AtomicBoolean;
37 final class MultipartRequestOnTheFlyCallback extends AbstractRequestCallback<List<MultipartReply>> {
38 private static final Logger LOG = LoggerFactory.getLogger(MultipartRequestOnTheFlyCallback.class);
39 private final SinglePurposeMultipartReplyTranslator multipartReplyTranslator;
40 private final DeviceInfo deviceInfo;
41 private final DeviceFlowRegistry registry;
42 private final EventIdentifier doneEventIdentifier;
43 private final TxFacade txFacade;
45 private Optional<FlowCapableNode> fcNodeOpt;
46 private AtomicBoolean virgin = new AtomicBoolean(true);
47 private AtomicBoolean finished = new AtomicBoolean(false);
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 ConvertorExecutor convertorExecutor) {
57 super(context, requestType, messageSpy, eventIdentifier);
59 this.deviceInfo = deviceInfo;
60 this.registry = registry;
61 this.txFacade = txFacade;
63 multipartReplyTranslator = new SinglePurposeMultipartReplyTranslator(convertorExecutor);
65 //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
66 doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceInfo.getNodeId().toString());
69 public EventIdentifier getDoneEventIdentifier() {
70 return doneEventIdentifier;
74 public void onSuccess(final OfHeader result) {
77 LOG.info("Ofheader was null.");
78 if (!finished.getAndSet(true)) {
82 } else if (finished.get()) {
83 LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
87 if (!(result instanceof MultipartReply)) {
88 if(!finished.getAndSet(true)) {
89 LOG.info("Unexpected response type received {}.", result.getClass());
90 final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
91 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
92 String.format("Unexpected response type received %s.", result.getClass()));
93 setResult(rpcResultBuilder.build());
97 final MultipartReply multipartReply = (MultipartReply) result;
101 fcNodeOpt = StatisticsGatheringUtils.deleteAllKnownFlows(deviceInfo, txFacade);
107 final MultipartReply singleReply = multipartReply;
108 final List<? extends DataObject> multipartDataList = multipartReplyTranslator.translate(
109 deviceInfo.getDatapathId(), deviceInfo.getVersion(), singleReply);
110 final Iterable<FlowsStatisticsUpdate> allMultipartData = (Iterable<FlowsStatisticsUpdate>) multipartDataList;
112 StatisticsGatheringUtils.writeFlowStatistics(allMultipartData, deviceInfo, registry, txFacade);
113 if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
119 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 setResult(rpcResult);
126 txFacade.submitTransaction();