Merge "SONAR TD - DeviceFlowRegistryImpl, FlowDescriptorFactory, FlowRegistryKeyFactory"
[openflowplugin.git] / applications / statistics-manager / src / main / java / org / opendaylight / openflowplugin / applications / statistics / manager / impl / StatListenCommitFlow.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.openflowplugin.applications.statistics.manager.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.BiMap;
13 import com.google.common.collect.HashBiMap;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.UUID;
22 import java.util.concurrent.atomic.AtomicInteger;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
27 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
28 import org.opendaylight.openflowplugin.applications.statistics.manager.StatNodeRegistration;
29 import org.opendaylight.openflowplugin.applications.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
30 import org.opendaylight.openflowplugin.applications.statistics.manager.StatisticsManager;
31 import org.opendaylight.openflowplugin.applications.statistics.manager.StatisticsManager.StatDataStoreOperation;
32 import org.opendaylight.openflowplugin.applications.statistics.manager.StatisticsManager.StatDataStoreOperation.StatsManagerOperationType;
33 import org.opendaylight.openflowplugin.applications.statistics.manager.impl.helper.FlowComparator;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowHashIdMapping;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowHashIdMappingBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMap;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMapBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMapKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsDataBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.aggregate.flow.statistics.AggregateFlowStatisticsBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionAware;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
64 import org.opendaylight.yangtools.yang.binding.DataObject;
65 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
66 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
69
70 /**
71  * statistics-manager
72  * org.opendaylight.openflowplugin.applications.statistics.manager.impl
73  *
74  * StatListenCommitFlow
75  * Class is a NotifyListener for FlowStatistics and DataTreeChangeListener 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
79  *
80  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
81  *
82  */
83 public class StatListenCommitFlow extends StatAbstractListenCommit<Flow, OpendaylightFlowStatisticsListener>
84                                             implements OpendaylightFlowStatisticsListener {
85
86     protected static final Logger LOG = LoggerFactory.getLogger(StatListenCommitFlow.class);
87
88     private static final String ALIEN_SYSTEM_FLOW_ID = "#UF$TABLE*";
89
90     private static final Integer REMOVE_AFTER_MISSING_COLLECTION = 1;
91     private static final int TRUNCATED_LOG_MESSAGE_LENGTH = 30;
92
93     private final AtomicInteger unaccountedFlowsCounter = new AtomicInteger(0);
94
95     public StatListenCommitFlow (final StatisticsManager manager, final DataBroker db,
96             final NotificationProviderService nps,
97                                  final StatNodeRegistration nrm){
98         super(manager, db, nps, Flow.class,nrm);
99     }
100
101     @Override
102     protected OpendaylightFlowStatisticsListener getStatNotificationListener() {
103         return this;
104     }
105
106     @Override
107     protected InstanceIdentifier<Flow> getWildCardedRegistrationPath() {
108         return InstanceIdentifier.create(Nodes.class).child(Node.class)
109                 .augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
110     }
111
112     @Override
113     public void onAggregateFlowStatisticsUpdate(final AggregateFlowStatisticsUpdate notification) {
114         final TransactionId transId = notification.getTransactionId();
115         final NodeId nodeId = notification.getId();
116         if ( ! isExpectedStatistics(transId, nodeId)) {
117             LOG.debug("STAT-MANAGER - AggregateFlowStatisticsUpdate: unregistred notification detect TransactionId {}", transId);
118             return;
119         }
120         manager.getRpcMsgManager().addNotification(notification, nodeId);
121         if (notification.isMoreReplies()) {
122             return;
123         }
124         /* check flow Capable Node and write statistics */
125         manager.enqueue(new StatDataStoreOperation(StatsManagerOperationType.DATA_COMMIT_OPER_DS,nodeId) {
126             @Override
127             public void applyOperation(final ReadWriteTransaction tx) {
128
129                 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
130                 if (( ! txContainer.isPresent()) || txContainer.get().getNotifications() == null) {
131                     return;
132                 }
133                 final Optional<? extends DataObject> inputObj = txContainer.get().getConfInput();
134                 if (( ! inputObj.isPresent()) || ( ! (inputObj.get() instanceof Table))) {
135                     return;
136                 }
137
138                 if(!nodeRegistrationManager.isFlowCapableNodeOwner(nodeId)) { return; }
139
140                 final Table table = (Table) inputObj.get();
141                 final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
142                 for (final TransactionAware notif : cacheNotifs) {
143                     if (notif instanceof AggregateFlowStatisticsUpdate) {
144                         final AggregateFlowStatisticsData stats = new AggregateFlowStatisticsDataBuilder()
145                             .setAggregateFlowStatistics(new AggregateFlowStatisticsBuilder(notification).build()).build();
146                         final InstanceIdentifier<FlowCapableNode> fNodeIdent = InstanceIdentifier.create(Nodes.class)
147                                 .child(Node.class, new NodeKey(nodeId)).augmentation(FlowCapableNode.class);
148                         final InstanceIdentifier<Table> tableRef = fNodeIdent.child(Table.class, table.getKey());
149                         final InstanceIdentifier<AggregateFlowStatisticsData> tableStatRef = tableRef
150                                 .augmentation(AggregateFlowStatisticsData.class);
151                         Optional<FlowCapableNode> fNode = Optional.absent();
152                         try {
153                             fNode = tx.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
154                         } catch (final ReadFailedException e) {
155                             LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
156                             return;
157                         }
158                         if (fNode.isPresent()) {
159                             ensureTable(tx, table.getId(), tableRef);
160                             tx.put(LogicalDatastoreType.OPERATIONAL, tableStatRef, stats);
161                         }
162                     }
163                 }
164             }
165
166             @Override
167             public UUID generatedUUIDForNode() {
168                 return manager.getGeneratedUUIDForNode(getNodeIdentifier());
169             }
170         });
171     }
172
173     public void ensureTable(final ReadWriteTransaction tx, final Short tableId, final InstanceIdentifier<Table> tableRef) {
174         final Table tableNew = new TableBuilder().setId(tableId).build();
175         tx.merge(LogicalDatastoreType.OPERATIONAL, tableRef, tableNew);
176     }
177
178     @Override
179     public void onFlowsStatisticsUpdate(final FlowsStatisticsUpdate notification) {
180         final TransactionId transId = notification.getTransactionId();
181         final NodeId nodeId = notification.getId();
182         if ( ! isExpectedStatistics(transId, nodeId)) {
183             LOG.debug("STAT-MANAGER - FlowsStatisticsUpdate: unregistred notification detect TransactionId {}", transId);
184             return;
185         }
186         manager.getRpcMsgManager().addNotification(notification, nodeId);
187         if (notification.isMoreReplies()) {
188             LOG.trace("Next notification for join txId {}", transId);
189             return;
190         }
191         /* add flow's statistics */
192         manager.enqueue(new StatDataStoreOperation(StatsManagerOperationType.DATA_COMMIT_OPER_DS,nodeId) {
193             @Override
194             public void applyOperation(final ReadWriteTransaction tx) {
195                 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
196                 if (( ! txContainer.isPresent()) || txContainer.get().getNotifications() == null) {
197                     return;
198                 }
199                 if(!nodeRegistrationManager.isFlowCapableNodeOwner(nodeId)) { return; }
200
201                 final List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>(10);
202                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
203                         .child(Node.class, new NodeKey(nodeId));
204                 final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
205                 for (final TransactionAware notif : cacheNotifs) {
206                     if (notif instanceof FlowsStatisticsUpdate) {
207                         final List<FlowAndStatisticsMapList> notifList =
208                                 ((FlowsStatisticsUpdate) notif).getFlowAndStatisticsMapList();
209                         if (notifList != null) {
210                             flowStats.addAll(notifList);
211                         }
212                     }
213                 }
214
215                 statsFlowCommitAll(flowStats, nodeIdent, tx);
216                 /* cleaning all not cached hash collisions */
217                 final Map<InstanceIdentifier<Flow>, Integer> listAliens = mapNodesForDelete.get(nodeIdent);
218                 if (listAliens != null) {
219                     for (final Entry<InstanceIdentifier<Flow>, Integer> nodeForDelete : listAliens.entrySet()) {
220                         final Integer lifeIndex = nodeForDelete.getValue();
221                         if (nodeForDelete.getValue() > 0) {
222                             nodeForDelete.setValue(Integer.valueOf(lifeIndex.intValue() - 1));
223                         } else {
224                             final InstanceIdentifier<Flow> flowNodeIdent = nodeForDelete.getKey();
225                             mapNodesForDelete.get(nodeIdent).remove(flowNodeIdent);
226                             tx.delete(LogicalDatastoreType.OPERATIONAL, flowNodeIdent);
227                         }
228                     }
229                 }
230                 /* Notification for continue collecting statistics */
231                 notifyToCollectNextStatistics(nodeIdent, transId);
232             }
233
234             @Override
235             public UUID generatedUUIDForNode() {
236                 return manager.getGeneratedUUIDForNode(getNodeIdentifier());
237             }
238
239         });
240     }
241
242     private void statsFlowCommitAll(final List<FlowAndStatisticsMapList> list,
243             final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction tx) {
244
245         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
246
247         final Optional<FlowCapableNode> fNode;
248         try {
249             fNode = tx.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
250         }
251         catch (final ReadFailedException e) {
252             LOG.debug("Read FlowCapableNode {} in Operational/DS fail! Statistic scan not be updated.", nodeIdent, e);
253             return;
254         }
255         if ( ! fNode.isPresent()) {
256             LOG.trace("FlowCapableNode {} is not presented in Operational/DS. Statisticscan not be updated.", nodeIdent);
257             return;
258         }
259
260         final NodeUpdateState nodeState = new NodeUpdateState(fNodeIdent,fNode.get());
261
262         for (final FlowAndStatisticsMapList flowStat : list) {
263             final TableKey tableKey = new TableKey(flowStat.getTableId());
264             final TableFlowUpdateState tableState = nodeState.getTable(tableKey, tx);
265             tableState.reportFlow(flowStat,tx);
266         }
267
268         for (final TableFlowUpdateState table : nodeState.getTables()) {
269             table.removeUnreportedFlows(tx);
270         }
271     }
272
273     /**
274      * Method adds statistics to Flow
275      *
276      * @param flowBuilder
277      * @param deviceFlow
278      */
279     private void addStatistics(final FlowBuilder flowBuilder, final FlowAndStatisticsMapList deviceFlow) {
280         final FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder(deviceFlow);
281         final FlowStatisticsBuilder flowStatisticsBuilder = new FlowStatisticsBuilder(stats.build());
282         final FlowStatisticsDataBuilder flowStatisticsData =new FlowStatisticsDataBuilder();
283         flowStatisticsData.setFlowStatistics(flowStatisticsBuilder.build());
284         flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
285     }
286
287     /**
288      * build pseudoUnique hashCode for flow in table
289      * for future easy identification
290      *
291      * FIXME: we expect same version for YANG models for all clusters and that has to be fix
292      * FIXME: CREATE BETTER KEY - for flow (MATCH is the problem)
293      */
294     static String buildFlowIdOperKey(final FlowAndStatisticsMapList deviceFlow) {
295         return new StringBuilder().append(deviceFlow.getMatch())
296                 .append(deviceFlow.getPriority()).append(deviceFlow.getCookie().getValue()).toString();
297     }
298
299     private class NodeUpdateState {
300         private final InstanceIdentifier<FlowCapableNode> nodeIdentifier;
301         private final Map<TableKey,TableFlowUpdateState> tables = new HashMap<>();
302
303         public NodeUpdateState(final InstanceIdentifier<FlowCapableNode> fNodeIdent, final FlowCapableNode flowCapableNode) {
304             nodeIdentifier = fNodeIdent;
305             final List<Table> tableList = flowCapableNode.getTable();
306             if(tableList != null) {
307             for (final Table table : tableList) {
308                 final TableKey tableKey = table.getKey();
309                     tables.put(tableKey, new TableFlowUpdateState(nodeIdentifier.child(Table.class,tableKey),table));
310                 }
311             }
312         }
313
314         public Iterable<TableFlowUpdateState> getTables() {
315             return tables.values();
316         }
317
318         TableFlowUpdateState getTable(final TableKey key,final ReadWriteTransaction tx) {
319             TableFlowUpdateState table = tables.get(key);
320             if(table == null) {
321                 table = new TableFlowUpdateState(nodeIdentifier.child(Table.class, key), null);
322                 tables.put(key, table);
323             }
324             return table;
325         }
326     }
327
328     private class TableFlowUpdateState {
329
330         private boolean tableEnsured = false;
331         final KeyedInstanceIdentifier<Table, TableKey> tableRef;
332         final TableKey tableKey;
333         final BiMap<FlowHashIdMapKey, FlowId> flowIdByHash;
334         List<Flow> configFlows;
335
336         public TableFlowUpdateState(final KeyedInstanceIdentifier<Table, TableKey> tablePath, final Table table) {
337             tableRef = tablePath;
338             tableKey = tablePath.getKey();
339             flowIdByHash = HashBiMap.create();
340             if(table != null) {
341                 final FlowHashIdMapping flowHashMapping = table.getAugmentation(FlowHashIdMapping.class);
342                 if (flowHashMapping != null) {
343                     final List<FlowHashIdMap>  flowHashMap = flowHashMapping.getFlowHashIdMap() != null
344                             ? flowHashMapping.getFlowHashIdMap() : Collections.<FlowHashIdMap> emptyList();
345                     for (final FlowHashIdMap flowHashId : flowHashMap) {
346                         try {
347                             flowIdByHash.put(flowHashId.getKey(), flowHashId.getFlowId());
348                         } catch (final Exception e) {
349                             //flowHashId.getKey() too verbose for standard log.
350                             if(LOG.isDebugEnabled()) {
351                                 final FlowId currData = flowIdByHash.get(flowHashId.getKey());
352                                 LOG.debug("flow hashing hit a duplicate for {} -> {}. Curr value: {} Equals:{}. Exception was raised:",
353                                     flowHashId.getKey(), flowHashId.getFlowId(), currData, flowHashId.getFlowId().equals(currData), e);
354                             }
355                             else
356                             {
357                                 LOG.warn("flow hashing hit a duplicate {}. Exception was raised: {}. Enable DEBUG for more detail.",
358                                     flowHashId.getFlowId().toString().substring(0, Math.min(TRUNCATED_LOG_MESSAGE_LENGTH,flowHashId.getFlowId().toString().length())),
359                                     e.getMessage().substring(0,Math.min(TRUNCATED_LOG_MESSAGE_LENGTH,e.getMessage().length())));
360                             }
361                         }
362                     }
363                 }
364             }
365         }
366
367         private void ensureTableFowHashIdMapping(final ReadWriteTransaction tx) {
368             if( ! tableEnsured) {
369                 ensureTable(tx, tableKey.getId(), tableRef);
370                 final FlowHashIdMapping emptyMapping = new FlowHashIdMappingBuilder()
371                     .setFlowHashIdMap(Collections.<FlowHashIdMap> emptyList()).build();
372                 tx.merge(LogicalDatastoreType.OPERATIONAL, tableRef.augmentation(FlowHashIdMapping.class), emptyMapping);
373                 tableEnsured = true;
374             }
375         }
376
377         private FlowKey searchInConfiguration(final FlowAndStatisticsMapList flowStat, final ReadWriteTransaction trans) {
378             initConfigFlows();
379             final Iterator<Flow> it = configFlows.iterator();
380             while(it.hasNext()) {
381                 final Flow cfgFlow = it.next();
382                 final FlowKey cfgKey = cfgFlow.getKey();
383                 final FlowId cfgFlowId = cfgKey.getId();
384
385                 if(! flowIdByHash.inverse().containsKey(cfgFlowId)) {
386                     if(FlowComparator.flowEquals(flowStat, cfgFlow)) {
387                         return cfgKey;
388                     }
389                 }
390             }
391             return null;
392         }
393
394         private void initConfigFlows() {
395             final Optional<Table> table = readLatestConfiguration(tableRef);
396             List<Flow> localList = null;
397             if(table.isPresent()) {
398                 localList = table.get().getFlow();
399             }
400             if(localList == null) {
401                 configFlows = Collections.emptyList();
402             } else {
403                 configFlows = new ArrayList<>(localList);
404             }
405         }
406
407         private FlowKey getFlowKeyAndRemoveHash(final FlowHashIdMapKey key) {
408             final FlowId ret = flowIdByHash.get(key);
409             if(ret != null) {
410                 flowIdByHash.remove(key);
411                 return new FlowKey(ret);
412             }
413             return null;
414         }
415
416         /* Returns FlowKey which doesn't exist in any DataStore for now */
417         private FlowKey makeAlienFlowKey() {
418             final StringBuilder sBuilder = new StringBuilder(ALIEN_SYSTEM_FLOW_ID)
419                 .append(tableKey.getId()).append("-").append(unaccountedFlowsCounter.incrementAndGet());
420             final FlowId flowId = new FlowId(sBuilder.toString());
421             return new FlowKey(flowId);
422         }
423
424         private Map<FlowHashIdMapKey, FlowId> getRemovalList() {
425             return flowIdByHash;
426         }
427
428         void reportFlow(final FlowAndStatisticsMapList flowStat, final ReadWriteTransaction trans) {
429             ensureTableFowHashIdMapping(trans);
430             final FlowHashIdMapKey hashingKey = new FlowHashIdMapKey(buildFlowIdOperKey(flowStat));
431             FlowKey flowKey = getFlowKeyAndRemoveHash(hashingKey);
432             if (flowKey == null) {
433                 flowKey = searchInConfiguration(flowStat, trans);
434                 if ( flowKey == null) {
435                     flowKey = makeAlienFlowKey();
436                 }
437                 updateHashCache(trans,flowKey,hashingKey);
438             }
439             final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
440             flowBuilder.setKey(flowKey);
441             addStatistics(flowBuilder, flowStat);
442             final InstanceIdentifier<Flow> flowIdent = tableRef.child(Flow.class, flowKey);
443             trans.put(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
444             /* check life for Alien flows */
445             if (flowKey.getId().getValue().startsWith(ALIEN_SYSTEM_FLOW_ID)) {
446                 removeData(flowIdent, REMOVE_AFTER_MISSING_COLLECTION);
447             }
448         }
449
450         /* Build and deploy new FlowHashId map */
451         private void updateHashCache(final ReadWriteTransaction trans, final FlowKey flowKey, final FlowHashIdMapKey hashingKey) {
452             final FlowHashIdMapBuilder flHashIdMap = new FlowHashIdMapBuilder();
453             flHashIdMap.setFlowId(flowKey.getId());
454             flHashIdMap.setKey(hashingKey);
455             final KeyedInstanceIdentifier<FlowHashIdMap, FlowHashIdMapKey> flHashIdent = tableRef
456                     .augmentation(FlowHashIdMapping.class).child(FlowHashIdMap.class, hashingKey);
457             /* Add new FlowHashIdMap */
458             trans.put(LogicalDatastoreType.OPERATIONAL, flHashIdent, flHashIdMap.build());
459         }
460
461         void removeUnreportedFlows(final ReadWriteTransaction tx) {
462             final InstanceIdentifier<Node> nodeIdent = tableRef.firstIdentifierOf(Node.class);
463             final List<InstanceIdentifier<Flow>> listMissingConfigFlows = notStatReportedConfigFlows();
464             final Map<InstanceIdentifier<Flow>, Integer> nodeDeleteMap = mapNodesForDelete.get(nodeIdent);
465             final Map<FlowHashIdMapKey, FlowId> listForRemove = getRemovalList();
466             for (final Entry<FlowHashIdMapKey, FlowId> entryForRemove : listForRemove.entrySet()) {
467                 final FlowKey flowKey = new FlowKey(entryForRemove.getValue());
468                 final InstanceIdentifier<Flow> flowRef = tableRef.child(Flow.class, flowKey);
469                 if (nodeDeleteMap != null && flowKey.getId().getValue().startsWith(ALIEN_SYSTEM_FLOW_ID)) {
470                     final Integer lifeIndex = nodeDeleteMap.get(flowRef);
471                      if (lifeIndex != null && lifeIndex > 0) {
472                         break;
473                     } else {
474                         nodeDeleteMap.remove(flowRef);
475                     }
476                 } else {
477                     if (listMissingConfigFlows.remove(flowRef)) {
478                         // it is probable that some multipart message was lost
479                         break;
480                     }
481                 }
482                 final InstanceIdentifier<FlowHashIdMap> flHashIdent =
483                         tableRef.augmentation(FlowHashIdMapping.class).child(FlowHashIdMap.class, entryForRemove.getKey());
484                 tx.delete(LogicalDatastoreType.OPERATIONAL, flowRef);
485                 tx.delete(LogicalDatastoreType.OPERATIONAL, flHashIdent);
486             }
487         }
488
489         List<InstanceIdentifier<Flow>> notStatReportedConfigFlows() {
490             if (configFlows != null) {
491                 final List<InstanceIdentifier<Flow>> returnList = new ArrayList<>(configFlows.size());
492                 for (final Flow confFlow : configFlows) {
493                     final InstanceIdentifier<Flow> confFlowIdent = tableRef.child(Flow.class, confFlow.getKey());
494                     returnList.add(confFlowIdent);
495                 }
496                 return returnList;
497             }
498             return Collections.emptyList();
499         }
500     }
501 }
502