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