2 * Copyright IBM Corporation, 2013. 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.controller.md.statistics.manager;
10 import java.util.Map.Entry;
12 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
13 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
14 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericStatistics;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 final class FlowStatsTracker extends AbstractListeningStatsTracker<FlowAndStatisticsMapList, FlowStatsEntry> {
38 private static final Logger logger = LoggerFactory.getLogger(FlowStatsTracker.class);
39 private final OpendaylightFlowStatisticsService flowStatsService;
40 private int unaccountedFlowsCounter = 1;
42 FlowStatsTracker(OpendaylightFlowStatisticsService flowStatsService, final FlowCapableContext context, long lifetimeNanos) {
43 super(context, lifetimeNanos);
44 this.flowStatsService = flowStatsService;
48 protected void cleanupSingleStat(DataModificationTransaction trans, FlowStatsEntry item) {
49 InstanceIdentifier<?> flowRef = getNodeIdentifierBuilder()
50 .augmentation(FlowCapableNode.class)
51 .child(Table.class, new TableKey(item.getTableId()))
52 .child(Flow.class,item.getFlow().getKey())
53 .augmentation(FlowStatisticsData.class).toInstance();
54 trans.removeOperationalData(flowRef);
58 protected FlowStatsEntry updateSingleStat(DataModificationTransaction trans, FlowAndStatisticsMapList map) {
59 short tableId = map.getTableId();
61 FlowBuilder flowBuilder = new FlowBuilder();
63 FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
65 FlowBuilder flow = new FlowBuilder();
66 flow.setContainerName(map.getContainerName());
67 flow.setBufferId(map.getBufferId());
68 flow.setCookie(map.getCookie());
69 flow.setCookieMask(map.getCookieMask());
70 flow.setFlags(map.getFlags());
71 flow.setFlowName(map.getFlowName());
72 flow.setHardTimeout(map.getHardTimeout());
73 if(map.getFlowId() != null)
74 flow.setId(new FlowId(map.getFlowId().getValue()));
75 flow.setIdleTimeout(map.getIdleTimeout());
76 flow.setInstallHw(map.isInstallHw());
77 flow.setInstructions(map.getInstructions());
78 if(map.getFlowId()!= null)
79 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
80 flow.setMatch(map.getMatch());
81 flow.setOutGroup(map.getOutGroup());
82 flow.setOutPort(map.getOutPort());
83 flow.setPriority(map.getPriority());
84 flow.setStrict(map.isStrict());
85 flow.setTableId(tableId);
87 Flow flowRule = flow.build();
89 FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
90 stats.setByteCount(map.getByteCount());
91 stats.setPacketCount(map.getPacketCount());
92 stats.setDuration(map.getDuration());
94 GenericStatistics flowStats = stats.build();
96 //Augment the data to the flow node
98 FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
99 flowStatistics.setByteCount(flowStats.getByteCount());
100 flowStatistics.setPacketCount(flowStats.getPacketCount());
101 flowStatistics.setDuration(flowStats.getDuration());
102 flowStatistics.setContainerName(map.getContainerName());
103 flowStatistics.setBufferId(map.getBufferId());
104 flowStatistics.setCookie(map.getCookie());
105 flowStatistics.setCookieMask(map.getCookieMask());
106 flowStatistics.setFlags(map.getFlags());
107 flowStatistics.setFlowName(map.getFlowName());
108 flowStatistics.setHardTimeout(map.getHardTimeout());
109 flowStatistics.setIdleTimeout(map.getIdleTimeout());
110 flowStatistics.setInstallHw(map.isInstallHw());
111 flowStatistics.setInstructions(map.getInstructions());
112 flowStatistics.setMatch(map.getMatch());
113 flowStatistics.setOutGroup(map.getOutGroup());
114 flowStatistics.setOutPort(map.getOutPort());
115 flowStatistics.setPriority(map.getPriority());
116 flowStatistics.setStrict(map.isStrict());
117 flowStatistics.setTableId(tableId);
119 flowStatisticsData.setFlowStatistics(flowStatistics.build());
121 logger.debug("Flow : {}",flowRule.toString());
122 logger.debug("Statistics to augment : {}",flowStatistics.build().toString());
124 InstanceIdentifier<Table> tableRef = getNodeIdentifierBuilder()
125 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
127 //TODO: Not a good way to do it, need to figure out better way.
128 //TODO: major issue in any alternate approach is that flow key is incrementally assigned
129 //to the flows stored in data store.
130 // Augment same statistics to all the matching masked flow
131 Table table= (Table)trans.readConfigurationData(tableRef);
133 for(Flow existingFlow : table.getFlow()){
134 logger.debug("Existing flow in data store : {}",existingFlow.toString());
135 if(FlowComparator.flowEquals(flowRule,existingFlow)){
136 InstanceIdentifier<Flow> flowRef = getNodeIdentifierBuilder()
137 .augmentation(FlowCapableNode.class)
138 .child(Table.class, new TableKey(tableId))
139 .child(Flow.class,existingFlow.getKey()).toInstance();
140 flowBuilder.setKey(existingFlow.getKey());
141 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
142 logger.debug("Found matching flow in the datastore, augmenting statistics");
143 // Update entry with timestamp of latest response
144 flow.setKey(existingFlow.getKey());
145 FlowStatsEntry flowStatsEntry = new FlowStatsEntry(tableId,flow.build());
146 trans.putOperationalData(flowRef, flowBuilder.build());
147 return flowStatsEntry;
152 table = (Table)trans.readOperationalData(tableRef);
154 for(Flow existingFlow : table.getFlow()){
155 FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
156 if(augmentedflowStatisticsData != null){
157 FlowBuilder existingOperationalFlow = new FlowBuilder();
158 existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
159 logger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
160 if(FlowComparator.flowEquals(flowRule,existingOperationalFlow.build())){
161 InstanceIdentifier<Flow> flowRef = getNodeIdentifierBuilder()
162 .augmentation(FlowCapableNode.class)
163 .child(Table.class, new TableKey(tableId))
164 .child(Flow.class,existingFlow.getKey()).toInstance();
165 flowBuilder.setKey(existingFlow.getKey());
166 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
167 logger.debug("Found matching unaccounted flow in the operational datastore, augmenting statistics");
168 // Update entry with timestamp of latest response
169 flow.setKey(existingFlow.getKey());
170 FlowStatsEntry flowStatsEntry = new FlowStatsEntry(tableId,flow.build());
171 trans.putOperationalData(flowRef, flowBuilder.build());
172 return flowStatsEntry;
178 String flowKey = "#UF$TABLE*"+Short.toString(tableId)+"*"+Integer.toString(this.unaccountedFlowsCounter);
179 this.unaccountedFlowsCounter++;
180 FlowKey newFlowKey = new FlowKey(new FlowId(flowKey));
181 InstanceIdentifier<Flow> flowRef = getNodeIdentifierBuilder().augmentation(FlowCapableNode.class)
182 .child(Table.class, new TableKey(tableId))
183 .child(Flow.class,newFlowKey).toInstance();
184 flowBuilder.setKey(newFlowKey);
185 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
186 logger.debug("Flow {} is not present in config data store, augmenting statistics as an unaccounted flow",
187 flowBuilder.build());
189 // Update entry with timestamp of latest response
190 flow.setKey(newFlowKey);
191 FlowStatsEntry flowStatsEntry = new FlowStatsEntry(tableId,flow.build());
192 trans.putOperationalData(flowRef, flowBuilder.build());
193 return flowStatsEntry;
197 protected InstanceIdentifier<?> listenPath() {
198 return getNodeIdentifierBuilder().augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class).build();
202 protected String statName() {
206 public void requestAllFlowsAllTables() {
207 if (flowStatsService != null) {
208 final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input = new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
209 input.setNode(getNodeRef());
211 requestHelper(flowStatsService.getAllFlowsStatisticsFromAllFlowTables(input.build()));
215 public void requestAggregateFlows(final TableKey key) {
216 if (flowStatsService != null) {
217 GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input =
218 new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder();
220 input.setNode(getNodeRef());
221 input.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(key.getId()));
222 requestHelper(flowStatsService.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build()));
226 public void requestFlow(final Flow flow) {
227 if (flowStatsService != null) {
228 final GetFlowStatisticsFromFlowTableInputBuilder input =
229 new GetFlowStatisticsFromFlowTableInputBuilder(flow);
230 input.setNode(getNodeRef());
232 requestHelper(flowStatsService.getFlowStatisticsFromFlowTable(input.build()));
237 public void onDataChanged(DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
238 for (Entry<InstanceIdentifier<?>, DataObject> e : change.getCreatedConfigurationData().entrySet()) {
239 if (Flow.class.equals(e.getKey().getTargetType())) {
240 final Flow flow = (Flow) e.getValue();
241 logger.debug("Key {} triggered request for flow {}", e.getKey(), flow);
244 logger.debug("Ignoring key {}", e.getKey());
248 final DataModificationTransaction trans = startTransaction();
249 for (InstanceIdentifier<?> key : change.getRemovedConfigurationData()) {
250 if (Flow.class.equals(key.getTargetType())) {
251 @SuppressWarnings("unchecked")
252 final InstanceIdentifier<Flow> flow = (InstanceIdentifier<Flow>)key;
253 final InstanceIdentifier<?> del = InstanceIdentifier.builder(flow)
254 .augmentation(FlowStatisticsData.class).build();
255 logger.debug("Key {} triggered remove of augmentation {}", key, del);
257 trans.removeOperationalData(del);
264 public void start(final DataBrokerService dbs) {
265 if (flowStatsService == null) {
266 logger.debug("No Flow Statistics service, not subscribing to flows on node {}", getNodeIdentifier());