Merge "BUG-421: Define multipart-transaction-aware"
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / FlowStatsTracker.java
1 /*
2  * Copyright IBM Corporation, 2013.  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.controller.md.statistics.manager;
9
10 import java.util.Map.Entry;
11
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;
36
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;
41
42     FlowStatsTracker(OpendaylightFlowStatisticsService flowStatsService, final FlowCapableContext context, long lifetimeNanos) {
43         super(context, lifetimeNanos);
44         this.flowStatsService = flowStatsService;
45     }
46
47     @Override
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);
55     }
56
57     @Override
58     protected FlowStatsEntry updateSingleStat(DataModificationTransaction trans, FlowAndStatisticsMapList map) {
59         short tableId = map.getTableId();
60
61         FlowBuilder flowBuilder = new FlowBuilder();
62
63         FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
64
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);
86
87         Flow flowRule = flow.build();
88
89         FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
90         stats.setByteCount(map.getByteCount());
91         stats.setPacketCount(map.getPacketCount());
92         stats.setDuration(map.getDuration());
93
94         GenericStatistics flowStats = stats.build();
95
96         //Augment the data to the flow node
97
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);
118
119         flowStatisticsData.setFlowStatistics(flowStatistics.build());
120
121         logger.debug("Flow : {}",flowRule.toString());
122         logger.debug("Statistics to augment : {}",flowStatistics.build().toString());
123
124         InstanceIdentifier<Table> tableRef = getNodeIdentifierBuilder()
125                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
126
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);
132         if(table != null){
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;
148                 }
149             }
150         }
151
152         table = (Table)trans.readOperationalData(tableRef);
153         if(table != null){
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;
173                     }
174                 }
175             }
176         }
177
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());
188
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;
194     }
195
196     @Override
197     protected InstanceIdentifier<?> listenPath() {
198         return getNodeIdentifierBuilder().augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class).build();
199     }
200
201     @Override
202     protected String statName() {
203         return "Flow";
204     }
205
206     public void requestAllFlowsAllTables() {
207         if (flowStatsService != null) {
208             final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input = new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
209             input.setNode(getNodeRef());
210
211             requestHelper(flowStatsService.getAllFlowsStatisticsFromAllFlowTables(input.build()));
212         }
213     }
214
215     public void requestAggregateFlows(final TableKey key) {
216         if (flowStatsService != null) {
217             GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input =
218                     new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder();
219
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()));
223         }
224     }
225
226     public void requestFlow(final Flow flow) {
227         if (flowStatsService != null) {
228             final GetFlowStatisticsFromFlowTableInputBuilder input =
229                     new GetFlowStatisticsFromFlowTableInputBuilder(flow);
230             input.setNode(getNodeRef());
231
232             requestHelper(flowStatsService.getFlowStatisticsFromFlowTable(input.build()));
233         }
234     }
235
236     @Override
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);
242                 requestFlow(flow);
243             } else {
244                 logger.debug("Ignoring key {}", e.getKey());
245             }
246         }
247
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);
256
257                 trans.removeOperationalData(del);
258             }
259         }
260         trans.commit();
261     }
262
263     @Override
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());
267             return;
268         }
269
270         super.start(dbs);
271     }
272 }