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
9 package org.opendaylight.openflowplugin.impl.device.listener;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
17 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
19 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
30 * Implementation for {@link MultiMsgCollector} interface
32 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
33 * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
35 * Created: Mar 23, 2015
37 public class MultiMsgCollectorImpl implements MultiMsgCollector {
39 private static final Logger LOG = LoggerFactory.getLogger(MultiMsgCollectorImpl.class);
41 private final List<MultipartReply> replyCollection = new ArrayList<>();
42 private final RequestContext<List<MultipartReply>> requestContext;
43 private final DeviceReplyProcessor deviceReplyProcessor;
44 private MultipartType msgType;
46 public MultiMsgCollectorImpl(final DeviceReplyProcessor deviceReplyProcessor, final RequestContext<List<MultipartReply>> requestContext) {
47 this.deviceReplyProcessor = Preconditions.checkNotNull(deviceReplyProcessor);
48 this.requestContext = Preconditions.checkNotNull(requestContext);
52 public void addMultipartMsg(final MultipartReply reply) {
53 addMultipartMsg(reply, null);
57 public void addMultipartMsg(@Nonnull final MultipartReply reply, @Nonnull final EventIdentifier eventIdentifier) {
58 Preconditions.checkNotNull(reply);
59 Preconditions.checkArgument(requestContext.getXid().getValue().equals(reply.getXid()));
60 LOG.trace("Try to add Multipart reply msg with XID {}", reply.getXid());
62 if (msgType == null) {
63 msgType = reply.getType();
66 if (!msgType.equals(reply.getType())) {
67 LOG.warn("MultiMsgCollector get incorrect multipart msg with type {} but expected type is {}", reply.getType(), msgType);
70 replyCollection.add(reply);
71 if (!reply.getFlags().isOFPMPFREQMORE()) {
72 endCollecting(eventIdentifier);
76 public void endCollecting() {
80 public void endCollecting(final EventIdentifier eventIdentifier) {
81 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(replyCollection).build();
82 if (null != eventIdentifier) {
83 EventsTimeCounter.markEnd(eventIdentifier);
85 requestContext.setResult(rpcResult);
86 requestContext.close();
87 deviceReplyProcessor.processReply(requestContext.getXid(), replyCollection);