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
9 package org.opendaylight.netvirt.aclservice.stats;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
19 import org.opendaylight.genius.mdsalutil.NwConstants;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
22 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.OpendaylightDirectStatisticsService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Metadata;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.Direction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.AclPortStats;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.AclPortStatsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.AclDropStats;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.AclDropStatsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.ErrorBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.acl.drop.stats.BytesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.acl.drop.stats.PacketsBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * The helper class for ACL live statistics.
54 public final class AclLiveStatisticsHelper {
56 private static final Logger LOG = LoggerFactory.getLogger(AclLiveStatisticsHelper.class);
58 /** The Constant COOKIE_ACL_DROP_FLOW_MASK. */
59 static final BigInteger COOKIE_ACL_DROP_FLOW_MASK = new BigInteger("FFFFFFFFFFFFFFFF", 16);
61 private AclLiveStatisticsHelper() {
62 throw new IllegalStateException("Utility class");
66 * Gets the acl port stats.
68 * @param direction the direction
69 * @param interfaceNames the interface names
70 * @param odlDirectStatsService the odl direct stats service
71 * @param dataBroker the data broker
72 * @return the acl port stats
74 public static List<AclPortStats> getAclPortStats(Direction direction, @NonNull List<String> interfaceNames,
75 OpendaylightDirectStatisticsService odlDirectStatsService, DataBroker dataBroker) {
76 LOG.trace("Get ACL port stats for direction {} and interfaces {}", direction, interfaceNames);
77 List<AclPortStats> lstAclPortStats = new ArrayList<>();
79 FlowCookie aclDropFlowCookieMask = new FlowCookie(COOKIE_ACL_DROP_FLOW_MASK);
81 for (String interfaceName : interfaceNames) {
82 AclPortStatsBuilder aclStatsBuilder = new AclPortStatsBuilder().setInterfaceName(interfaceName);
84 Interface interfaceState = AclServiceUtils.getInterfaceStateFromOperDS(dataBroker, interfaceName);
85 if (interfaceState == null) {
86 String errMsg = "Interface not found in datastore.";
87 addError(lstAclPortStats, aclStatsBuilder, errMsg);
90 BigInteger dpId = AclServiceUtils.getDpIdFromIterfaceState(interfaceState);
92 String errMsg = "Failed to find device for the interface.";
93 addError(lstAclPortStats, aclStatsBuilder, errMsg);
97 NodeRef nodeRef = buildNodeRef(dpId);
98 Integer lportTag = interfaceState.getIfIndex();
99 FlowCookie aclDropFlowCookie = new FlowCookie(AclServiceUtils.getDropFlowCookie(lportTag));
101 GetFlowStatisticsInputBuilder input =
102 new GetFlowStatisticsInputBuilder().setNode(nodeRef).setCookie(aclDropFlowCookie)
103 .setCookieMask(aclDropFlowCookieMask).setStoreStats(false);
105 Future<RpcResult<GetFlowStatisticsOutput>> rpcResultFuture =
106 odlDirectStatsService.getFlowStatistics(input.build());
107 RpcResult<GetFlowStatisticsOutput> rpcResult = null;
109 rpcResult = rpcResultFuture.get();
110 } catch (InterruptedException | ExecutionException e) {
111 String errMsg = "Unable to retrieve drop counts due to error: " + e.getMessage();
112 addError(lstAclPortStats, aclStatsBuilder, errMsg);
113 LOG.error("Exception occurred during get flow statistics for interface {}", interfaceName, e);
116 if (rpcResult != null && rpcResult.isSuccessful() && rpcResult.getResult() != null) {
117 GetFlowStatisticsOutput flowStatsOutput = rpcResult.getResult();
118 getAclDropStats(direction, aclStatsBuilder, flowStatsOutput);
119 lstAclPortStats.add(aclStatsBuilder.build());
121 handleRpcErrors(lstAclPortStats, aclStatsBuilder, rpcResult);
124 return lstAclPortStats;
130 * @param lstAclPortStats the lst acl port stats
131 * @param aclStatsBuilder the acl stats builder
132 * @param rpcResult the rpc result
134 private static void handleRpcErrors(List<AclPortStats> lstAclPortStats, AclPortStatsBuilder aclStatsBuilder,
135 @Nullable RpcResult<GetFlowStatisticsOutput> rpcResult) {
136 LOG.error("Unable to retrieve drop counts due to error: {}", rpcResult);
137 String errMsg = "Unable to retrieve drop counts due to error: ";
138 if (rpcResult != null && rpcResult.getErrors() != null && !rpcResult.getErrors().isEmpty()) {
139 errMsg += rpcResult.getErrors().iterator().next().getMessage();
141 errMsg += "Internal RPC call failed.";
143 addError(lstAclPortStats, aclStatsBuilder, errMsg);
147 * Gets the acl drop stats.
149 * @param direction the direction
150 * @param aclStatsBuilder the acl stats builder
151 * @param flowStatsOutput the flow stats output
153 private static void getAclDropStats(Direction direction, AclPortStatsBuilder aclStatsBuilder,
154 GetFlowStatisticsOutput flowStatsOutput) {
155 List<FlowAndStatisticsMapList> flowAndStatisticsMapList = flowStatsOutput.getFlowAndStatisticsMapList();
156 if (flowAndStatisticsMapList == null || flowAndStatisticsMapList.isEmpty()) {
157 String errMsg = "Unable to retrieve drop counts as interface is not configured for statistics collection.";
158 aclStatsBuilder.setError(new ErrorBuilder().setErrorMessage(errMsg).build());
162 BytesBuilder portEgressBytesBuilder = new BytesBuilder();
163 BytesBuilder portIngressBytesBuilder = new BytesBuilder();
165 PacketsBuilder portEgressPacketsBuilder = new PacketsBuilder();
166 PacketsBuilder portIngressPacketsBuilder = new PacketsBuilder();
168 for (FlowAndStatisticsMapList flowStats : flowAndStatisticsMapList) {
169 switch (flowStats.getTableId().toJava()) {
170 case NwConstants.INGRESS_ACL_FILTER_CUM_DISPATCHER_TABLE:
171 if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority().toJava())) {
172 portEgressBytesBuilder.setInvalidDropCount(flowStats.getByteCount().getValue());
173 portEgressPacketsBuilder.setInvalidDropCount(flowStats.getPacketCount().getValue());
174 } else if (AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY.equals(flowStats.getPriority().toJava())
175 || AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY.equals(flowStats.getPriority().toJava())) {
176 BigInteger portEgressBytesBuilderDropCount = BigInteger.valueOf(0);
177 BigInteger portEgressPacketsBuilderDropCount = BigInteger.valueOf(0);
178 if (portEgressBytesBuilder.getDropCount() != null) {
179 portEgressBytesBuilderDropCount = portEgressBytesBuilder.getDropCount().toJava()
180 .add(flowStats.getByteCount().getValue().toJava());
181 portEgressPacketsBuilderDropCount = portEgressPacketsBuilder.getDropCount().toJava()
182 .add(flowStats.getPacketCount().getValue().toJava());
184 portEgressBytesBuilderDropCount = flowStats.getByteCount().getValue().toJava();
185 portEgressPacketsBuilderDropCount = flowStats.getPacketCount().getValue().toJava();
187 portEgressBytesBuilder.setDropCount(portEgressBytesBuilderDropCount);
188 portEgressPacketsBuilder.setDropCount(portEgressPacketsBuilderDropCount);
190 // TODO: Update stats for other drops
193 case NwConstants.EGRESS_ACL_FILTER_CUM_DISPATCHER_TABLE:
194 if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority().toJava())) {
195 portIngressBytesBuilder.setInvalidDropCount(flowStats.getByteCount().getValue());
196 portIngressPacketsBuilder.setInvalidDropCount(flowStats.getPacketCount().getValue());
197 } else if (AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY.equals(flowStats.getPriority().toJava())
198 || AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY.equals(flowStats.getPriority().toJava())) {
199 BigInteger portIngressBytesBuilderDropCount = BigInteger.valueOf(0);
200 BigInteger portIngressPacketsBuilderDropCount = BigInteger.valueOf(0);
201 if (portIngressBytesBuilder.getDropCount() != null) {
202 portIngressBytesBuilderDropCount = portIngressBytesBuilder.getDropCount().toJava()
203 .add(flowStats.getByteCount().getValue().toJava());
204 portIngressPacketsBuilderDropCount = portIngressPacketsBuilder.getDropCount().toJava()
205 .add(flowStats.getPacketCount().getValue().toJava());
207 portIngressBytesBuilderDropCount = flowStats.getByteCount().getValue().toJava();
208 portIngressPacketsBuilderDropCount = flowStats.getPacketCount().getValue().toJava();
210 portIngressBytesBuilder.setDropCount(portIngressBytesBuilderDropCount);
211 portIngressPacketsBuilder.setDropCount(portIngressPacketsBuilderDropCount);
213 // TODO: Update stats for other drops
215 case NwConstants.INGRESS_ACL_COMMITTER_TABLE:
216 if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority().toJava())) {
217 portEgressBytesBuilder.setAntiSpoofDropCount(flowStats.getByteCount().getValue());
218 portEgressPacketsBuilder.setAntiSpoofDropCount(flowStats.getPacketCount().getValue());
221 case NwConstants.EGRESS_ACL_COMMITTER_TABLE:
222 if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority().toJava())) {
223 portIngressBytesBuilder.setAntiSpoofDropCount(flowStats.getByteCount().getValue());
224 portIngressPacketsBuilder.setAntiSpoofDropCount(flowStats.getPacketCount().getValue());
229 LOG.warn("Invalid table ID filtered for Acl flow stats: {}", flowStats);
234 List<AclDropStats> lstAclDropStats = new ArrayList<>();
235 if (direction == Direction.Egress || direction == Direction.Both) {
236 updateTotalDropCount(portEgressBytesBuilder,portEgressPacketsBuilder);
237 AclDropStats aclEgressDropStats = new AclDropStatsBuilder().setDirection(Direction.Egress)
238 .setBytes(portEgressBytesBuilder.build()).setPackets(portEgressPacketsBuilder.build()).build();
239 lstAclDropStats.add(aclEgressDropStats);
242 if (direction == Direction.Ingress || direction == Direction.Both) {
243 updateTotalDropCount(portIngressBytesBuilder,portIngressPacketsBuilder);
244 AclDropStats aclIngressDropStats = new AclDropStatsBuilder().setDirection(Direction.Ingress)
245 .setBytes(portIngressBytesBuilder.build()).setPackets(portIngressPacketsBuilder.build()).build();
246 lstAclDropStats.add(aclIngressDropStats);
248 aclStatsBuilder.setAclDropStats(lstAclDropStats);
251 private static void updateTotalDropCount(BytesBuilder portBytesBuilder, PacketsBuilder portPacketsBuilder) {
252 BigInteger dropCountByt = BigInteger.ZERO;
253 BigInteger invalidDropCountByt = BigInteger.ZERO;
254 BigInteger antispoofDropCountByt = BigInteger.ZERO;
255 BigInteger dropCountPkt = BigInteger.ZERO;
256 BigInteger invalidDropCountPkt = BigInteger.ZERO;
257 BigInteger antispoofDropCountPkt = BigInteger.ZERO;
259 if (portBytesBuilder.getDropCount() != null) {
260 dropCountByt = portBytesBuilder.getDropCount().toJava();
262 if (portPacketsBuilder.getDropCount() != null) {
263 dropCountPkt = portPacketsBuilder.getDropCount().toJava();
265 if (portBytesBuilder.getDropCount() != null) {
266 invalidDropCountByt = portBytesBuilder.getInvalidDropCount().toJava();
268 if (portPacketsBuilder.getDropCount() != null) {
269 invalidDropCountPkt = portPacketsBuilder.getInvalidDropCount().toJava();
271 if (portBytesBuilder.getDropCount() != null) {
272 antispoofDropCountByt = portBytesBuilder.getAntiSpoofDropCount().toJava();
274 if (portPacketsBuilder.getDropCount() != null) {
275 antispoofDropCountPkt = portPacketsBuilder.getAntiSpoofDropCount().toJava();
277 portBytesBuilder.setTotalDropCount(antispoofDropCountByt.add(dropCountByt.add(invalidDropCountByt)));
278 portPacketsBuilder.setTotalDropCount(antispoofDropCountPkt.add(dropCountPkt.add(invalidDropCountPkt)));
285 * @param lstAclPortStats the lst acl port stats
286 * @param aclStatsBuilder the acl stats builder
287 * @param errMsg the error message
289 private static void addError(List<AclPortStats> lstAclPortStats, AclPortStatsBuilder aclStatsBuilder,
291 aclStatsBuilder.setError(new ErrorBuilder().setErrorMessage(errMsg).build());
292 lstAclPortStats.add(aclStatsBuilder.build());
296 * Builds the metadata match.
298 * @param lportTag the lport tag
301 protected static Match buildMetadataMatch(Integer lportTag) {
302 Metadata metadata = new MetadataBuilder().setMetadata(MetaDataUtil.getLportTagMetaData(lportTag))
303 .setMetadataMask(MetaDataUtil.METADATA_MASK_LPORT_TAG).build();
304 return new MatchBuilder().setMetadata(metadata).build();
308 * Builds the node ref.
310 * @param dpId the dp id
311 * @return the node ref
313 @SuppressWarnings("deprecation")
314 private static NodeRef buildNodeRef(BigInteger dpId) {
315 return new NodeRef(InstanceIdentifier.builder(Nodes.class)
316 .child(Node.class, new NodeKey(new NodeId("openflow:" + dpId))).build());