Merge "fixing late hooking of request into deviceContext registry"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / dedicated / StatisticsGatheringService.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.statistics.services.dedicated;
10
11 import com.google.common.base.Function;
12 import com.google.common.util.concurrent.JdkFutureAdapters;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.List;
15 import java.util.concurrent.Future;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
20 import org.opendaylight.openflowplugin.impl.services.CommonService;
21 import org.opendaylight.openflowplugin.impl.services.DataCrate;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Created by Martin Bobak <mbobak@cisco.com> on 4.4.2015.
31  */
32 public class StatisticsGatheringService extends CommonService {
33
34     private static final Logger LOG = LoggerFactory.getLogger(StatisticsGatheringService.class);
35
36     public StatisticsGatheringService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
37
38         super(requestContextStack, deviceContext);
39     }
40
41
42     public Future<RpcResult<List<MultipartReply>>> getStatisticsOfType(final MultipartType type) {
43         return handleServiceCall(
44                 PRIMARY_CONNECTION, new Function<DataCrate<List<MultipartReply>>, ListenableFuture<RpcResult<Void>>>() {
45                     @Override
46                     public ListenableFuture<RpcResult<Void>> apply(final DataCrate<List<MultipartReply>> data) {
47
48                         LOG.info("Calling multipart request for type {}", type);
49                         final Xid xid = data.getRequestContext().getXid();
50
51                         deviceContext.getOpenflowMessageListenerFacade().registerMultipartXid(xid.getValue());
52                         MultipartRequestInput multipartRequestInput = MultipartRequestInputFactory.
53                                 makeMultipartRequestInput(xid.getValue(),
54                                         version,
55                                         type);
56                         final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
57                                 .getConnectionAdapter().multipartRequest(multipartRequestInput);
58                         return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
59                     }
60                 }
61
62         );
63     }
64
65 }