Merge "Remove old drop-test module and files."
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / listener / MultiMsgCollectorImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.device.listener;
10
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Objects;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
19 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
21 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29 /**
30  * <p>
31  * Implementation for {@link MultiMsgCollector} interface
32  *
33  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
34  * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
35  *         </p>
36  *         Created: Mar 23, 2015
37  */
38 public class MultiMsgCollectorImpl<T extends OfHeader> implements MultiMsgCollector<T> {
39     private static final Logger LOG = LoggerFactory.getLogger(MultiMsgCollectorImpl.class);
40     private final List<T> replyCollection = new ArrayList<>();
41     private final RequestContext<List<T>> requestContext;
42     private final DeviceReplyProcessor deviceReplyProcessor;
43
44     public MultiMsgCollectorImpl(final DeviceReplyProcessor deviceReplyProcessor, final RequestContext<List<T>> requestContext) {
45         this.deviceReplyProcessor = Preconditions.checkNotNull(deviceReplyProcessor);
46         this.requestContext = Preconditions.checkNotNull(requestContext);
47     }
48
49     @Override
50     public void addMultipartMsg(@Nonnull final T reply, final boolean reqMore, @Nullable final EventIdentifier eventIdentifier) {
51         Preconditions.checkNotNull(reply);
52         Preconditions.checkNotNull(requestContext.getXid());
53         Preconditions.checkArgument(requestContext.getXid().getValue().equals(reply.getXid()));
54         LOG.trace("Try to add Multipart reply msg with XID {}", reply.getXid());
55         replyCollection.add(reply);
56
57         if (!reqMore) {
58             endCollecting(eventIdentifier);
59         }
60     }
61
62     @Override
63     public void endCollecting(@Nullable final EventIdentifier eventIdentifier) {
64         final RpcResult<List<T>> rpcResult = RpcResultBuilder.success(replyCollection).build();
65
66         if (Objects.nonNull(eventIdentifier)) {
67             EventsTimeCounter.markEnd(eventIdentifier);
68         }
69
70         requestContext.setResult(rpcResult);
71         requestContext.close();
72         deviceReplyProcessor.processReply(requestContext.getXid(), replyCollection);
73     }
74 }