Merge "Fix checkstyle warnings for impl/protocol test package"
[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  * Implementation for {@link MultiMsgCollector} interface.
31  *
32  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
33  * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
34  */
35 public class MultiMsgCollectorImpl<T extends OfHeader> implements MultiMsgCollector<T> {
36     private static final Logger LOG = LoggerFactory.getLogger(MultiMsgCollectorImpl.class);
37     private final List<T> replyCollection = new ArrayList<>();
38     private final RequestContext<List<T>> requestContext;
39     private final DeviceReplyProcessor deviceReplyProcessor;
40
41     public MultiMsgCollectorImpl(final DeviceReplyProcessor deviceReplyProcessor,
42                                  final RequestContext<List<T>> requestContext) {
43         this.deviceReplyProcessor = Preconditions.checkNotNull(deviceReplyProcessor);
44         this.requestContext = Preconditions.checkNotNull(requestContext);
45     }
46
47     @Override
48     public void addMultipartMsg(@Nonnull final T reply, final boolean reqMore,
49                                 @Nullable final EventIdentifier eventIdentifier) {
50         Preconditions.checkNotNull(reply);
51         Preconditions.checkNotNull(requestContext.getXid());
52         Preconditions.checkArgument(requestContext.getXid().getValue().equals(reply.getXid()));
53         LOG.trace("Try to add Multipart reply msg with XID {}", reply.getXid());
54         replyCollection.add(reply);
55
56         if (!reqMore) {
57             endCollecting(eventIdentifier);
58         }
59     }
60
61     @Override
62     public void endCollecting(@Nullable final EventIdentifier eventIdentifier) {
63         final RpcResult<List<T>> rpcResult = RpcResultBuilder.success(replyCollection).build();
64
65         if (Objects.nonNull(eventIdentifier)) {
66             EventsTimeCounter.markEnd(eventIdentifier);
67         }
68
69         requestContext.setResult(rpcResult);
70         requestContext.close();
71         deviceReplyProcessor.processReply(requestContext.getXid(), replyCollection);
72     }
73 }