Adjust to RPC method signature update
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / stats / AclLiveStatisticsRpcServiceImpl.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.netvirt.aclservice.stats;
10
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.OpendaylightDirectStatisticsService;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.AclLiveStatisticsService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.Direction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.GetAclPortStatisticsInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.GetAclPortStatisticsOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.GetAclPortStatisticsOutputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.AclPortStats;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig.SecurityGroupMode;
26 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The class provides RPC service implementation for
34  * {@link AclLiveStatisticsService}.
35  */
36 @Singleton
37 public class AclLiveStatisticsRpcServiceImpl implements AclLiveStatisticsService {
38
39     private static final Logger LOG = LoggerFactory.getLogger(AclLiveStatisticsRpcServiceImpl.class);
40
41     private final DataBroker dataBroker;
42     private final OpendaylightDirectStatisticsService odlDirectStatsService;
43     private final SecurityGroupMode securityGroupMode;
44
45     /**
46      * Instantiates a new acl live statistics rpc service impl.
47      *
48      * @param config the config
49      * @param dataBroker the data broker
50      * @param odlDirectStatsService the odl direct stats service
51      */
52     @Inject
53     public AclLiveStatisticsRpcServiceImpl(final AclserviceConfig config, final DataBroker dataBroker,
54             final OpendaylightDirectStatisticsService odlDirectStatsService) {
55         this.dataBroker = dataBroker;
56         this.odlDirectStatsService = odlDirectStatsService;
57         this.securityGroupMode = config == null ? SecurityGroupMode.Stateful : config.getSecurityGroupMode();
58
59         LOG.info("AclLiveStatisticsRpcServiceImpl initialized");
60     }
61
62     @Override
63     public ListenableFuture<RpcResult<GetAclPortStatisticsOutput>> getAclPortStatistics(
64             GetAclPortStatisticsInput input) {
65         LOG.trace("Get ACL port statistics for input: {}", input);
66         RpcResultBuilder<GetAclPortStatisticsOutput> rpcResultBuilder;
67
68         if (this.securityGroupMode != SecurityGroupMode.Stateful) {
69             rpcResultBuilder = RpcResultBuilder.failed();
70             rpcResultBuilder.withError(ErrorType.APPLICATION, "operation-not-supported",
71                     "Operation not supported for ACL " + this.securityGroupMode + " mode");
72             return rpcResultBuilder.buildFuture();
73         }
74         // Default direction is Both
75         Direction direction = input.getDirection() == null ? Direction.Both : input.getDirection();
76
77         List<AclPortStats> lstAclInterfaceStats = AclLiveStatisticsHelper.getAclPortStats(direction,
78                 input.getInterfaceNames(), this.odlDirectStatsService, this.dataBroker);
79
80         GetAclPortStatisticsOutputBuilder output =
81                 new GetAclPortStatisticsOutputBuilder().setAclPortStats(lstAclInterfaceStats);
82         rpcResultBuilder = RpcResultBuilder.success();
83         rpcResultBuilder.withResult(output.build());
84         return Futures.immediateFuture(rpcResultBuilder.build());
85     }
86
87 }