2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.statisitcs;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import javax.annotation.Nullable;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
17 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
18 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius._interface.statistics.rev150824.ResultCode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.ElanStatisticsService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsOutputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.get.elan._interface.statistics.output.StatResultBuilder;
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;
33 public class ElanStatisticsImpl implements ElanStatisticsService {
34 private static final Logger LOG = LoggerFactory.getLogger(ElanStatisticsImpl.class);
36 private final IInterfaceManager interfaceManager;
37 private final ElanInterfaceCache elanInterfaceCache;
40 public ElanStatisticsImpl(IInterfaceManager interfaceManager,
41 ElanInterfaceCache elanInterfaceCache) {
42 this.interfaceManager = interfaceManager;
43 this.elanInterfaceCache = elanInterfaceCache;
47 public ListenableFuture<RpcResult<GetElanInterfaceStatisticsOutput>> getElanInterfaceStatistics(
48 GetElanInterfaceStatisticsInput input) {
49 String interfaceName = input.getInterfaceName();
50 LOG.debug("getElanInterfaceStatistics is called for elan interface {}", interfaceName);
51 RpcResultBuilder<GetElanInterfaceStatisticsOutput> rpcResultBuilder = null;
52 if (interfaceName == null) {
53 rpcResultBuilder = RpcResultBuilder.failed();
54 return getFutureWithAppErrorMessage(rpcResultBuilder, "Interface name is not provided");
56 Optional<ElanInterface> elanInterface = elanInterfaceCache.get(interfaceName);
57 if (!elanInterface.isPresent()) {
58 rpcResultBuilder = RpcResultBuilder.failed();
59 return getFutureWithAppErrorMessage(rpcResultBuilder,
60 String.format("Interface %s is not a ELAN interface", interfaceName));
62 String elanInstanceName = elanInterface.get().getElanInstanceName();
63 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
64 //FIXME [ELANBE] Get this API Later
68 // //tableId = interfaceManager.getTableIdForService(interfaceName, serviceInfo);
69 // } catch (InterfaceNotFoundException | InterfaceServiceNotFoundException e) {
70 // rpcResultBuilder = RpcResultBuilder.failed();
71 // return getFutureWithAppErrorMessage(rpcResultBuilder,
72 // String.format("Interface %s or Service %s doesn't exist", interfaceName, serviceInfo));
74 if (!interfaceInfo.isOperational()) {
75 LOG.debug("interface {} is down and returning with no statistics", interfaceName);
76 rpcResultBuilder = RpcResultBuilder.success();
77 return rpcResultBuilder.withResult(new GetElanInterfaceStatisticsOutputBuilder()
79 new StatResultBuilder().setStatResultCode(ResultCode.NotFound).setByteRxCount(0L)
80 .setByteTxCount(0L).setPacketRxCount(0L).setPacketTxCount(0L).build())
81 .build()).buildFuture();
83 rpcResultBuilder = RpcResultBuilder.success();
84 return Futures.immediateFuture(rpcResultBuilder
85 .withResult(queryforElanInterfaceStatistics(tableId, elanInstanceName, interfaceInfo)).build());
89 private GetElanInterfaceStatisticsOutput queryforElanInterfaceStatistics(short tableId, String elanInstanceName,
90 InterfaceInfo interfaceInfo) {
91 // BigInteger dpId = interfaceInfo.getDpId();
92 // List<MatchInfo> matches;
93 // String interfaceName = interfaceInfo.getInterfaceName();
94 // if (interfaceInfo instanceof VlanInterfaceInfo) {
95 // VlanInterfaceInfo vlanInterfaceInfo = (VlanInterfaceInfo)interfaceInfo;
96 // matches = InterfaceServiceUtil.getMatchInfoForVlanLPort(dpId, interfaceInfo.getPortNo(),
97 // InterfaceServiceUtil.getVlanId(interfaceName, dataBroker), vlanInterfaceInfo.isVlanTransparent());
99 // matches = InterfaceServiceUtil.getLPortDispatcherMatches(
100 // ServiceIndex.getIndex(NwConstants.ELAN_SERVICE_NAME, NwConstants.ELAN_SERVICE_INDEX),
101 // interfaceInfo.getInterfaceTag());
103 // long groupId = interfaceInfo.getGroupId();
104 // Set<Object> statRequestKeys = InterfaceServiceUtil.getStatRequestKeys(dpId, tableId, matches,
105 // String.format("%s.%s", elanInstanceName, interfaceName), groupId);
106 // StatisticsInfo statsInfo = new StatisticsInfo(statRequestKeys);
107 // org.opendaylight.vpnservice.ericsson.mdsalutil.statistics.StatResult statResult
108 // = mdsalMgr.queryForStatistics(interfaceName, statsInfo);
109 // ResultCode resultCode = ResultCode.Success;
110 // if (!statResult.isComplete()) {
111 // resultCode = ResultCode.Incomplete;
114 //StatValue ingressFlowStats = statResult.getStatResult(InterfaceServiceUtil
115 // .getFlowStatisticsKey(dpId, tableId, matches, elanInstanceName));
116 //StatValue groupStats = statResult.getStatResult(InterfaceServiceUtil.getGroupStatisticsKey(dpId, groupId));
117 // return new GetElanInterfaceStatisticsOutputBuilder().setStatResult(new
118 // StatResultBuilder().setStatResultCode(resultCode)
119 // .setByteRxCount(ingressFlowStats.getByteCount()).setPacketRxCount(ingressFlowStats.getPacketCount())
120 // .setByteTxCount(groupStats.getByteCount()).setPacketTxCount(groupStats.getPacketCount()).build())
125 private ListenableFuture<RpcResult<GetElanInterfaceStatisticsOutput>> getFutureWithAppErrorMessage(
126 RpcResultBuilder<GetElanInterfaceStatisticsOutput> rpcResultBuilder, String message) {
127 rpcResultBuilder.withError(ErrorType.APPLICATION, message);
128 return rpcResultBuilder.buildFuture();