Merge "BUG-2188: To populate the port_number of switches - yang model"
[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 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;
26
27
28 /**
29  * <p>
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  *         </p>
35  *         Created: Mar 23, 2015
36  */
37 public class MultiMsgCollectorImpl implements MultiMsgCollector {
38
39     private static final Logger LOG = LoggerFactory.getLogger(MultiMsgCollectorImpl.class);
40
41     private final List<MultipartReply> replyCollection = new ArrayList<>();
42     private final RequestContext<List<MultipartReply>> requestContext;
43     private final DeviceReplyProcessor deviceReplyProcessor;
44     private MultipartType msgType;
45
46     public MultiMsgCollectorImpl(final DeviceReplyProcessor deviceReplyProcessor, final RequestContext<List<MultipartReply>> requestContext) {
47         this.deviceReplyProcessor = Preconditions.checkNotNull(deviceReplyProcessor);
48         this.requestContext = Preconditions.checkNotNull(requestContext);
49     }
50
51     @Override
52     public void addMultipartMsg(final MultipartReply reply) {
53         addMultipartMsg(reply, null);
54     }
55
56     @Override
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());
61
62         if (msgType == null) {
63             msgType = reply.getType();
64         }
65
66         if (!msgType.equals(reply.getType())) {
67             LOG.warn("MultiMsgCollector get incorrect multipart msg with type {} but expected type is {}", reply.getType(), msgType);
68         }
69
70         replyCollection.add(reply);
71         if (!reply.getFlags().isOFPMPFREQMORE()) {
72             endCollecting(eventIdentifier);
73         }
74     }
75
76     public void endCollecting() {
77         endCollecting(null);
78     }
79
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);
84         }
85         requestContext.setResult(rpcResult);
86         requestContext.close();
87         deviceReplyProcessor.processReply(requestContext.getXid(), replyCollection);
88     }
89 }