Fix RpcResultBuilder/RpcContext raw references
[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.FutureCallback;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.List;
16 import java.util.concurrent.Future;
17 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
21 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
22 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
23 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
24 import org.opendaylight.openflowplugin.impl.services.CommonService;
25 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
30 import org.opendaylight.yangtools.yang.common.RpcError;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Created by Martin Bobak <mbobak@cisco.com> on 4.4.2015.
38  */
39 public class StatisticsGatheringService extends CommonService {
40
41     private static final Logger LOG = LoggerFactory.getLogger(StatisticsGatheringService.class);
42
43     public StatisticsGatheringService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
44
45         super(requestContextStack, deviceContext);
46     }
47
48
49     public Future<RpcResult<List<MultipartReply>>> getStatisticsOfType(final MultipartType type) {
50         return handleServiceCall(new Function<RequestContext<List<MultipartReply>>, ListenableFuture<RpcResult<Void>>>() {
51                                      @Override
52                                      public ListenableFuture<RpcResult<Void>> apply(final RequestContext<List<MultipartReply>> requestContext) {
53                                          final Xid xid = requestContext.getXid();
54                                          final DeviceContext deviceContext = getDeviceContext();
55                                          final MultiMsgCollector multiMsgCollector = deviceContext.getMultiMsgCollector();
56
57                                          multiMsgCollector.registerMultipartXid(xid.getValue());
58                                          MultipartRequestInput multipartRequestInput = MultipartRequestInputFactory.
59                                                  makeMultipartRequestInput(xid.getValue(),
60                                                          getVersion(),
61                                                          type);
62                                          final OutboundQueue outboundQueue = deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider();
63                                          final SettableFuture<RpcResult<Void>> settableFuture = SettableFuture.create();
64                                          outboundQueue.commitEntry(xid.getValue(), multipartRequestInput, new FutureCallback<OfHeader>() {
65                                              @Override
66                                              public void onSuccess(final OfHeader ofHeader) {
67                                                  if (ofHeader instanceof MultipartReply) {
68                                                      final MultipartReply multipartReply = (MultipartReply) ofHeader;
69                                                      settableFuture.set(RpcResultBuilder.<Void>success().build());
70                                                      multiMsgCollector.addMultipartMsg(multipartReply);
71                                                  } else {
72                                                      if (null != ofHeader) {
73                                                          LOG.info("Unexpected response type received {}.", ofHeader.getClass());
74                                                      } else {
75                                                          LOG.info("Ofheader was null.");
76                                                      }
77                                                  }
78                                              }
79
80                                              @Override
81                                              public void onFailure(final Throwable throwable) {
82                                                  RpcResultBuilder<Void> rpcResultBuilder = RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage());
83                                                  getDeviceContext().unhookRequestCtx(requestContext.getXid());
84                                                  RequestContextUtil.closeRequstContext(requestContext);
85
86                                                  settableFuture.set(rpcResultBuilder.build());
87                                              }
88                                          });
89                                          return settableFuture;
90                                      }
91                                  }
92
93         );
94     }
95
96 }