2 * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.md.statistics.manager.impl;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.LinkedList;
16 import java.util.List;
18 import java.util.Map.Entry;
19 import java.util.concurrent.atomic.AtomicInteger;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
26 import org.opendaylight.controller.md.statistics.manager.StatisticsManager;
27 import org.opendaylight.controller.md.statistics.manager.StatisticsManager.StatDataStoreOperation;
28 import org.opendaylight.controller.md.statistics.manager.impl.helper.FlowComparator;
29 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowHashIdMapping;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowHashIdMappingBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMap;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMapBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMapKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsDataBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.aggregate.flow.statistics.AggregateFlowStatisticsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
60 import org.opendaylight.yangtools.yang.binding.DataObject;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
66 import com.google.common.base.Optional;
67 import com.google.common.collect.BiMap;
68 import com.google.common.collect.HashBiMap;
72 * org.opendaylight.controller.md.statistics.manager.impl
74 * StatListenCommitFlow
75 * Class is a NotifyListener for FlowStatistics and DataChangeListener for Config/DataStore for Flow node.
76 * All expected (registered) FlowStatistics will be builded and commit to Operational/DataStore.
77 * DataChangeEven should call create/delete Flow in Operational/DS create process needs to pair
78 * Device Flow HashCode and FlowId from Config/DS
80 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
83 public class StatListenCommitFlow extends StatAbstractListenCommit<Flow, OpendaylightFlowStatisticsListener>
84 implements OpendaylightFlowStatisticsListener {
86 protected static final Logger LOG = LoggerFactory.getLogger(StatListenCommitFlow.class);
88 private static final String ALIEN_SYSTEM_FLOW_ID = "#UF$TABLE*";
90 private static final Integer REMOVE_AFTER_MISSING_COLLECTION = 1;
92 private final AtomicInteger unaccountedFlowsCounter = new AtomicInteger(0);
94 public StatListenCommitFlow (final StatisticsManager manager, final DataBroker db,
95 final NotificationProviderService nps){
96 super(manager, db, nps, Flow.class);
100 protected OpendaylightFlowStatisticsListener getStatNotificationListener() {
105 protected InstanceIdentifier<Flow> getWildCardedRegistrationPath() {
106 return InstanceIdentifier.create(Nodes.class).child(Node.class)
107 .augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
111 public void onAggregateFlowStatisticsUpdate(final AggregateFlowStatisticsUpdate notification) {
112 final TransactionId transId = notification.getTransactionId();
113 final NodeId nodeId = notification.getId();
114 if ( ! isExpectedStatistics(transId, nodeId)) {
115 LOG.debug("STAT-MANAGER - AggregateFlowStatisticsUpdate: unregistred notification detect TransactionId {}", transId);
118 manager.getRpcMsgManager().addNotification(notification, nodeId);
119 if (notification.isMoreReplies()) {
122 /* check flow Capable Node and write statistics */
123 manager.enqueue(new StatDataStoreOperation() {
125 public void applyOperation(final ReadWriteTransaction tx) {
127 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
128 if (( ! txContainer.isPresent()) || txContainer.get().getNotifications() == null) {
131 final Optional<? extends DataObject> inputObj = txContainer.get().getConfInput();
132 if (( ! inputObj.isPresent()) || ( ! (inputObj.get() instanceof Table))) {
135 final Table table = (Table) inputObj.get();
136 final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
137 for (final TransactionAware notif : cacheNotifs) {
138 if (notif instanceof AggregateFlowStatisticsUpdate) {
139 final AggregateFlowStatisticsData stats = new AggregateFlowStatisticsDataBuilder()
140 .setAggregateFlowStatistics(new AggregateFlowStatisticsBuilder(notification).build()).build();
141 final InstanceIdentifier<FlowCapableNode> fNodeIdent = InstanceIdentifier.create(Nodes.class)
142 .child(Node.class, new NodeKey(nodeId)).augmentation(FlowCapableNode.class);
143 final InstanceIdentifier<Table> tableRef = fNodeIdent.child(Table.class, table.getKey());
144 final InstanceIdentifier<AggregateFlowStatisticsData> tableStatRef = tableRef
145 .augmentation(AggregateFlowStatisticsData.class);
146 Optional<FlowCapableNode> fNode = Optional.absent();
148 fNode = tx.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
149 } catch (final ReadFailedException e) {
150 LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
153 if (fNode.isPresent()) {
154 ensureTable(tx, table.getId(), tableRef);
155 tx.put(LogicalDatastoreType.OPERATIONAL, tableStatRef, stats);
163 public void ensureTable(final ReadWriteTransaction tx, final Short tableId, final InstanceIdentifier<Table> tableRef) {
164 final Table tableNew = new TableBuilder().setId(tableId).build();
165 tx.merge(LogicalDatastoreType.OPERATIONAL, tableRef, tableNew);
169 public void onFlowsStatisticsUpdate(final FlowsStatisticsUpdate notification) {
170 final TransactionId transId = notification.getTransactionId();
171 final NodeId nodeId = notification.getId();
172 if ( ! isExpectedStatistics(transId, nodeId)) {
173 LOG.debug("STAT-MANAGER - FlowsStatisticsUpdate: unregistred notification detect TransactionId {}", transId);
176 manager.getRpcMsgManager().addNotification(notification, nodeId);
177 if (notification.isMoreReplies()) {
178 LOG.trace("Next notification for join txId {}", transId);
181 /* add flow's statistics */
182 manager.enqueue(new StatDataStoreOperation() {
184 public void applyOperation(final ReadWriteTransaction tx) {
185 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
186 if (( ! txContainer.isPresent()) || txContainer.get().getNotifications() == null) {
189 final List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>(10);
190 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
191 .child(Node.class, new NodeKey(nodeId));
192 final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
193 for (final TransactionAware notif : cacheNotifs) {
194 if (notif instanceof FlowsStatisticsUpdate) {
195 final List<FlowAndStatisticsMapList> notifList =
196 ((FlowsStatisticsUpdate) notif).getFlowAndStatisticsMapList();
197 if (notifList != null) {
198 flowStats.addAll(notifList);
203 statsFlowCommitAll(flowStats, nodeIdent, tx);
204 /* cleaning all not cached hash collisions */
205 final Map<InstanceIdentifier<Flow>, Integer> listAliens = mapNodesForDelete.get(nodeIdent);
206 if (listAliens != null) {
207 for (final Entry<InstanceIdentifier<Flow>, Integer> nodeForDelete : listAliens.entrySet()) {
208 final Integer lifeIndex = nodeForDelete.getValue();
209 if (nodeForDelete.getValue() > 0) {
210 nodeForDelete.setValue(Integer.valueOf(lifeIndex.intValue() - 1));
212 final InstanceIdentifier<Flow> flowNodeIdent = nodeForDelete.getKey();
213 mapNodesForDelete.get(nodeIdent).remove(flowNodeIdent);
214 tx.delete(LogicalDatastoreType.OPERATIONAL, flowNodeIdent);
218 /* Notification for continue collecting statistics */
219 notifyToCollectNextStatistics(nodeIdent, transId);
224 private void statsFlowCommitAll(final List<FlowAndStatisticsMapList> list,
225 final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction tx) {
227 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
229 final Optional<FlowCapableNode> fNode;
231 fNode = tx.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
233 catch (final ReadFailedException e) {
234 LOG.debug("Read FlowCapableNode {} in Operational/DS fail! Statistic scan not be updated.", nodeIdent, e);
237 if ( ! fNode.isPresent()) {
238 LOG.trace("FlowCapableNode {} is not presented in Operational/DS. Statisticscan not be updated.", nodeIdent);
242 final NodeUpdateState nodeState = new NodeUpdateState(fNodeIdent,fNode.get());
244 for (final FlowAndStatisticsMapList flowStat : list) {
245 final TableKey tableKey = new TableKey(flowStat.getTableId());
246 final TableFlowUpdateState tableState = nodeState.getTable(tableKey, tx);
247 tableState.reportFlow(flowStat,tx);
250 for (final TableFlowUpdateState table : nodeState.getTables()) {
251 table.removeUnreportedFlows(tx);
256 * Method adds statistics to Flow
261 private void addStatistics(final FlowBuilder flowBuilder, final FlowAndStatisticsMapList deviceFlow) {
262 final FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder(deviceFlow);
263 final FlowStatisticsBuilder flowStatisticsBuilder = new FlowStatisticsBuilder(stats.build());
264 final FlowStatisticsDataBuilder flowStatisticsData =new FlowStatisticsDataBuilder();
265 flowStatisticsData.setFlowStatistics(flowStatisticsBuilder.build());
266 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
270 * build pseudoUnique hashCode for flow in table
271 * for future easy identification
273 * FIXME: we expect same version for YANG models for all clusters and that has to be fix
274 * FIXME: CREATE BETTER KEY - for flow (MATCH is the problem)
276 static String buildFlowIdOperKey(final FlowAndStatisticsMapList deviceFlow) {
277 return new StringBuffer().append(deviceFlow.getMatch())
278 .append(deviceFlow.getPriority()).append(deviceFlow.getCookie().getValue()).toString();
281 private class NodeUpdateState {
282 private final InstanceIdentifier<FlowCapableNode> nodeIdentifier;
283 private final Map<TableKey,TableFlowUpdateState> tables = new HashMap<>();
285 public NodeUpdateState(final InstanceIdentifier<FlowCapableNode> fNodeIdent, final FlowCapableNode flowCapableNode) {
286 nodeIdentifier = fNodeIdent;
287 final List<Table> tableList = flowCapableNode.getTable();
288 if(tableList != null) {
289 for (final Table table : tableList) {
290 final TableKey tableKey = table.getKey();
291 tables.put(tableKey, new TableFlowUpdateState(nodeIdentifier.child(Table.class,tableKey),table));
296 public Iterable<TableFlowUpdateState> getTables() {
297 return tables.values();
300 TableFlowUpdateState getTable(final TableKey key,final ReadWriteTransaction tx) {
301 TableFlowUpdateState table = tables.get(key);
303 table = new TableFlowUpdateState(nodeIdentifier.child(Table.class, key), null);
304 tables.put(key, table);
310 private class TableFlowUpdateState {
312 private boolean tableEnsured = false;
313 final KeyedInstanceIdentifier<Table, TableKey> tableRef;
314 final TableKey tableKey;
315 final BiMap<FlowHashIdMapKey, FlowId> flowIdByHash;
316 List<Flow> configFlows;
318 public TableFlowUpdateState(final KeyedInstanceIdentifier<Table, TableKey> tablePath, final Table table) {
319 tableRef = tablePath;
320 tableKey = tablePath.getKey();
321 flowIdByHash = HashBiMap.create();
323 final FlowHashIdMapping flowHashMapping = table.getAugmentation(FlowHashIdMapping.class);
324 if (flowHashMapping != null) {
325 final List<FlowHashIdMap> flowHashMap = flowHashMapping.getFlowHashIdMap() != null
326 ? flowHashMapping.getFlowHashIdMap() : Collections.<FlowHashIdMap> emptyList();
327 for (final FlowHashIdMap flowHashId : flowHashMap) {
329 flowIdByHash.put(flowHashId.getKey(), flowHashId.getFlowId());
330 } catch (final Exception e) {
331 LOG.warn("flow hashing hit a duplicate for {} -> {}", flowHashId.getKey(), flowHashId.getFlowId());
338 private void ensureTableFowHashIdMapping(final ReadWriteTransaction tx) {
339 if( ! tableEnsured) {
340 ensureTable(tx, tableKey.getId(), tableRef);
341 final FlowHashIdMapping emptyMapping = new FlowHashIdMappingBuilder()
342 .setFlowHashIdMap(Collections.<FlowHashIdMap> emptyList()).build();
343 tx.merge(LogicalDatastoreType.OPERATIONAL, tableRef.augmentation(FlowHashIdMapping.class), emptyMapping);
348 private FlowKey searchInConfiguration(final FlowAndStatisticsMapList flowStat, final ReadWriteTransaction trans) {
349 initConfigFlows(trans);
350 final Iterator<Flow> it = configFlows.iterator();
351 while(it.hasNext()) {
352 final Flow cfgFlow = it.next();
353 final FlowKey cfgKey = cfgFlow.getKey();
354 if(flowIdByHash.inverse().containsKey(cfgKey)) {
356 } else if(FlowComparator.flowEquals(flowStat, cfgFlow)) {
364 private void initConfigFlows(final ReadWriteTransaction trans) {
365 final Optional<Table> table = readLatestConfiguration(tableRef);
366 List<Flow> localList = null;
367 if(table.isPresent()) {
368 localList = table.get().getFlow();
370 if(localList == null) {
371 configFlows = Collections.emptyList();
373 configFlows = new LinkedList<>(localList);
377 private FlowKey getFlowKeyAndRemoveHash(final FlowHashIdMapKey key) {
378 final FlowId ret = flowIdByHash.get(key);
380 flowIdByHash.remove(key);
381 return new FlowKey(ret);
386 /* Returns FlowKey which doesn't exist in any DataStore for now */
387 private FlowKey makeAlienFlowKey() {
388 final StringBuilder sBuilder = new StringBuilder(ALIEN_SYSTEM_FLOW_ID)
389 .append(tableKey.getId()).append("-").append(unaccountedFlowsCounter.incrementAndGet());
390 final FlowId flowId = new FlowId(sBuilder.toString());
391 return new FlowKey(flowId);
394 private Map<FlowHashIdMapKey, FlowId> getRemovalList() {
398 void reportFlow(final FlowAndStatisticsMapList flowStat, final ReadWriteTransaction trans) {
399 ensureTableFowHashIdMapping(trans);
400 final FlowHashIdMapKey hashingKey = new FlowHashIdMapKey(buildFlowIdOperKey(flowStat));
401 FlowKey flowKey = getFlowKeyAndRemoveHash(hashingKey);
402 if (flowKey == null) {
403 flowKey = searchInConfiguration(flowStat, trans);
404 if ( flowKey == null) {
405 flowKey = makeAlienFlowKey();
407 updateHashCache(trans,flowKey,hashingKey);
409 final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
410 flowBuilder.setKey(flowKey);
411 addStatistics(flowBuilder, flowStat);
412 final InstanceIdentifier<Flow> flowIdent = tableRef.child(Flow.class, flowKey);
413 trans.put(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
414 /* check life for Alien flows */
415 if (flowKey.getId().getValue().startsWith(ALIEN_SYSTEM_FLOW_ID)) {
416 removeData(flowIdent, REMOVE_AFTER_MISSING_COLLECTION);
420 /* Build and deploy new FlowHashId map */
421 private void updateHashCache(final ReadWriteTransaction trans, final FlowKey flowKey, final FlowHashIdMapKey hashingKey) {
422 final FlowHashIdMapBuilder flHashIdMap = new FlowHashIdMapBuilder();
423 flHashIdMap.setFlowId(flowKey.getId());
424 flHashIdMap.setKey(hashingKey);
425 final KeyedInstanceIdentifier<FlowHashIdMap, FlowHashIdMapKey> flHashIdent = tableRef
426 .augmentation(FlowHashIdMapping.class).child(FlowHashIdMap.class, hashingKey);
427 /* Add new FlowHashIdMap */
428 trans.put(LogicalDatastoreType.OPERATIONAL, flHashIdent, flHashIdMap.build());
431 void removeUnreportedFlows(final ReadWriteTransaction tx) {
432 final InstanceIdentifier<Node> nodeIdent = tableRef.firstIdentifierOf(Node.class);
433 final List<InstanceIdentifier<Flow>> listMissingConfigFlows = notStatReportedConfigFlows();
434 final Map<InstanceIdentifier<Flow>, Integer> nodeDeleteMap = mapNodesForDelete.get(nodeIdent);
435 final Map<FlowHashIdMapKey, FlowId> listForRemove = getRemovalList();
436 for (final Entry<FlowHashIdMapKey, FlowId> entryForRemove : listForRemove.entrySet()) {
437 final FlowKey flowKey = new FlowKey(entryForRemove.getValue());
438 final InstanceIdentifier<Flow> flowRef = tableRef.child(Flow.class, flowKey);
439 if (nodeDeleteMap != null && flowKey.getId().getValue().startsWith(ALIEN_SYSTEM_FLOW_ID)) {
440 final Integer lifeIndex = nodeDeleteMap.get(flowRef);
444 nodeDeleteMap.remove(flowRef);
447 if (listMissingConfigFlows.remove(flowRef)) {
448 break; // we probably lost some multipart msg
451 final InstanceIdentifier<FlowHashIdMap> flHashIdent =
452 tableRef.augmentation(FlowHashIdMapping.class).child(FlowHashIdMap.class, entryForRemove.getKey());
453 tx.delete(LogicalDatastoreType.OPERATIONAL, flowRef);
454 tx.delete(LogicalDatastoreType.OPERATIONAL, flHashIdent);
458 List<InstanceIdentifier<Flow>> notStatReportedConfigFlows() {
459 if (configFlows != null) {
460 final List<InstanceIdentifier<Flow>> returnList = new ArrayList<>(configFlows.size());
461 for (final Flow confFlow : configFlows) {
462 final InstanceIdentifier<Flow> confFlowIdent = tableRef.child(Flow.class, confFlow.getKey());
463 returnList.add(confFlowIdent);
467 return Collections.emptyList();