OPNFLWPLUG-1071 : Removal of javax.annotation.Nonnnull and replacement of javax.annot...
[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 package org.opendaylight.openflowplugin.impl.device.listener;
9
10 import com.google.common.base.Preconditions;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
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.protocol.rev130731.OfHeader;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Implementation for {@link MultiMsgCollector} interface.
28  *
29  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
30  * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
31  */
32 public class MultiMsgCollectorImpl<T extends OfHeader> implements MultiMsgCollector<T> {
33     private static final Logger LOG = LoggerFactory.getLogger(MultiMsgCollectorImpl.class);
34     private final List<T> replyCollection = new ArrayList<>();
35     private final RequestContext<List<T>> requestContext;
36     private final DeviceReplyProcessor deviceReplyProcessor;
37
38     public MultiMsgCollectorImpl(final DeviceReplyProcessor deviceReplyProcessor,
39                                  final RequestContext<List<T>> requestContext) {
40         this.deviceReplyProcessor = Preconditions.checkNotNull(deviceReplyProcessor);
41         this.requestContext = Preconditions.checkNotNull(requestContext);
42     }
43
44     @Override
45     public void addMultipartMsg(@NonNull final T reply, final boolean reqMore,
46                                 @Nullable final EventIdentifier eventIdentifier) {
47         Preconditions.checkNotNull(reply);
48         Preconditions.checkNotNull(requestContext.getXid());
49         Preconditions.checkArgument(requestContext.getXid().getValue().equals(reply.getXid()));
50         LOG.trace("Try to add Multipart reply msg with XID {}", reply.getXid());
51         replyCollection.add(reply);
52
53         if (!reqMore) {
54             endCollecting(eventIdentifier);
55         }
56     }
57
58     @Override
59     public void endCollecting(@Nullable final EventIdentifier eventIdentifier) {
60         final RpcResult<List<T>> rpcResult = RpcResultBuilder.success(replyCollection).build();
61
62         if (eventIdentifier != null) {
63             EventsTimeCounter.markEnd(eventIdentifier);
64         }
65
66         requestContext.setResult(rpcResult);
67         requestContext.close();
68         deviceReplyProcessor.processReply(requestContext.getXid(), replyCollection);
69     }
70 }