Bug 3435 - add time counter for selected operations on device
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / MultipartRequestCallback.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.services;
9
10 import java.util.List;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
13 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
14 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
17 import org.opendaylight.yangtools.yang.common.RpcError;
18 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 final class MultipartRequestCallback extends AbstractRequestCallback<List<MultipartReply>> {
23     private static final Logger LOG = LoggerFactory.getLogger(MultipartRequestCallback.class);
24     private final MultiMsgCollector collector;
25
26     public MultipartRequestCallback(final RequestContext<List<MultipartReply>> context, final Class<?> requestType, final DeviceContext deviceContext) {
27         super(context, requestType, deviceContext.getMessageSpy());
28         collector = deviceContext.getMultiMsgCollector(context);
29     }
30
31     public MultipartRequestCallback(final RequestContext<List<MultipartReply>> context,
32                                     final Class<?> requestType,
33                                     final DeviceContext deviceContext,
34                                     final EventIdentifier eventIdentifier) {
35         super(context, requestType, deviceContext.getMessageSpy(), eventIdentifier);
36         collector = deviceContext.getMultiMsgCollector(context);
37     }
38
39     @Override
40     public void onSuccess(final OfHeader result) {
41         if (result == null) {
42             LOG.info("Ofheader was null.");
43             collector.endCollecting(getEventIdentifier());
44             return;
45         }
46
47         if (!(result instanceof MultipartReply)) {
48             LOG.info("Unexpected response type received {}.", result.getClass());
49             final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
50                     RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
51                             String.format("Unexpected response type received %s.", result.getClass()));
52             setResult(rpcResultBuilder.build());
53         } else {
54             collector.addMultipartMsg((MultipartReply) result, getEventIdentifier());
55         }
56     }
57
58 }