Eliminate circular dependencies and convert to BP annotations in elanmanager
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / statisitcs / ElanStatisticsImpl.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 package org.opendaylight.netvirt.elan.statisitcs;
9
10 import com.google.common.util.concurrent.Futures;
11 import java.util.concurrent.Future;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
16 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
17 import org.opendaylight.netvirt.elan.utils.ElanUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius._interface.statistics.rev150824.ResultCode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.ElanStatisticsService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsOutputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.get.elan._interface.statistics.output.StatResultBuilder;
25 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 @Singleton
32 public class ElanStatisticsImpl implements ElanStatisticsService {
33     private static final Logger LOG = LoggerFactory.getLogger(ElanStatisticsImpl.class);
34
35     private final DataBroker dataBroker;
36     private final IInterfaceManager interfaceManager;
37
38     @Inject
39     public ElanStatisticsImpl(DataBroker dataBroker, IInterfaceManager interfaceManager) {
40         this.dataBroker = dataBroker;
41         this.interfaceManager = interfaceManager;
42     }
43
44     @Override
45     public Future<RpcResult<GetElanInterfaceStatisticsOutput>> getElanInterfaceStatistics(
46         GetElanInterfaceStatisticsInput input) {
47         String interfaceName = input.getInterfaceName();
48         LOG.debug("getElanInterfaceStatistics is called for elan interface {}", interfaceName);
49         RpcResultBuilder<GetElanInterfaceStatisticsOutput> rpcResultBuilder = null;
50         if (interfaceName == null) {
51             rpcResultBuilder = RpcResultBuilder.failed();
52             return getFutureWithAppErrorMessage(rpcResultBuilder, "Interface name is not provided");
53         }
54         ElanInterface elanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(dataBroker, interfaceName);
55         if (elanInterface == null) {
56             rpcResultBuilder = RpcResultBuilder.failed();
57             return getFutureWithAppErrorMessage(rpcResultBuilder,
58                     String.format("Interface %s is not a ELAN interface", interfaceName));
59         }
60         String elanInstanceName = elanInterface.getElanInstanceName();
61         InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
62         //FIXME [ELANBE] Get this API Later
63         short tableId = 0;
64 //        try {
65 //
66 //            //tableId = interfaceManager.getTableIdForService(interfaceName, serviceInfo);
67 //        } catch (InterfaceNotFoundException | InterfaceServiceNotFoundException e) {
68 //            rpcResultBuilder = RpcResultBuilder.failed();
69 //            return getFutureWithAppErrorMessage(rpcResultBuilder,
70 //                String.format("Interface %s or Service %s doesn't exist", interfaceName, serviceInfo));
71 //        }
72         if (!interfaceInfo.isOperational()) {
73             LOG.debug("interface {} is down and returning with no statistics", interfaceName);
74             rpcResultBuilder = RpcResultBuilder.success();
75             return Futures
76                     .immediateFuture(rpcResultBuilder.withResult(new GetElanInterfaceStatisticsOutputBuilder()
77                             .setStatResult(
78                                     new StatResultBuilder().setStatResultCode(ResultCode.NotFound).setByteRxCount(0L)
79                                             .setByteTxCount(0L).setPacketRxCount(0L).setPacketTxCount(0L).build())
80                             .build()).build());
81         }
82         rpcResultBuilder = RpcResultBuilder.success();
83         return Futures.immediateFuture(rpcResultBuilder
84                 .withResult(queryforElanInterfaceStatistics(tableId, elanInstanceName, interfaceInfo)).build());
85     }
86
87     private GetElanInterfaceStatisticsOutput queryforElanInterfaceStatistics(short tableId, String elanInstanceName,
88             InterfaceInfo interfaceInfo) {
89 //        BigInteger dpId = interfaceInfo.getDpId();
90 //        List<MatchInfo> matches;
91 //        String interfaceName = interfaceInfo.getInterfaceName();
92 //        if (interfaceInfo instanceof VlanInterfaceInfo) {
93 //            VlanInterfaceInfo vlanInterfaceInfo = (VlanInterfaceInfo)interfaceInfo;
94 //            matches = InterfaceServiceUtil.getMatchInfoForVlanLPort(dpId, interfaceInfo.getPortNo(),
95 //                InterfaceServiceUtil.getVlanId(interfaceName, dataBroker), vlanInterfaceInfo.isVlanTransparent());
96 //        } else {
97 //            matches = InterfaceServiceUtil.getLPortDispatcherMatches(
98 //                    ServiceIndex.getIndex(NwConstants.ELAN_SERVICE_NAME, NwConstants.ELAN_SERVICE_INDEX),
99 //                    interfaceInfo.getInterfaceTag());
100 //        }
101 //        long groupId = interfaceInfo.getGroupId();
102 //        Set<Object> statRequestKeys = InterfaceServiceUtil.getStatRequestKeys(dpId, tableId, matches,
103 //                String.format("%s.%s", elanInstanceName, interfaceName), groupId);
104         // StatisticsInfo statsInfo = new StatisticsInfo(statRequestKeys);
105 //        org.opendaylight.vpnservice.ericsson.mdsalutil.statistics.StatResult statResult
106 //            = mdsalMgr.queryForStatistics(interfaceName, statsInfo);
107 //        ResultCode resultCode = ResultCode.Success;
108 //        if (!statResult.isComplete()) {
109 //            resultCode = ResultCode.Incomplete;
110 //        }
111
112         //StatValue ingressFlowStats = statResult.getStatResult(InterfaceServiceUtil
113 //            .getFlowStatisticsKey(dpId, tableId, matches, elanInstanceName));
114         //StatValue groupStats = statResult.getStatResult(InterfaceServiceUtil.getGroupStatisticsKey(dpId, groupId));
115 //      return new GetElanInterfaceStatisticsOutputBuilder().setStatResult(new
116 //          StatResultBuilder().setStatResultCode(resultCode)
117 //                .setByteRxCount(ingressFlowStats.getByteCount()).setPacketRxCount(ingressFlowStats.getPacketCount())
118 //                .setByteTxCount(groupStats.getByteCount()).setPacketTxCount(groupStats.getPacketCount()).build())
119 //                .build();
120         return null;
121     }
122
123     private Future<RpcResult<GetElanInterfaceStatisticsOutput>> getFutureWithAppErrorMessage(
124         RpcResultBuilder<GetElanInterfaceStatisticsOutput> rpcResultBuilder, String message) {
125         rpcResultBuilder.withError(ErrorType.APPLICATION, message);
126         return Futures.immediateFuture(rpcResultBuilder.build());
127     }
128
129 }