Merge "BUG-5888: moving the reconciliation process into a different thread to prevent...
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / FlowNodeReconciliationImpl.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.frm.impl;
10
11 import java.util.*;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.Iterables;
19 import com.google.common.collect.Lists;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import com.google.common.util.concurrent.FutureCallback;
22 import com.google.common.util.concurrent.Futures;
23
24 import java.util.concurrent.Callable;
25
26 import org.opendaylight.controller.md.sal.binding.api.*;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
30 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
31 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeter;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeterKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
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.inventory.rev130819.tables.table.StaleFlow;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
62 import org.opendaylight.yangtools.concepts.ListenerRegistration;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67
68 import javax.annotation.Nonnull;
69
70
71 /**
72  * forwardingrules-manager
73  * org.opendaylight.openflowplugin.applications.frm
74  *
75  * FlowNode Reconciliation Listener
76  * Reconciliation for a new FlowNode
77  *
78  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
79  *
80  * Created: Jun 13, 2014
81  */
82 public class FlowNodeReconciliationImpl implements FlowNodeReconciliation {
83
84     private static final Logger LOG = LoggerFactory.getLogger(FlowNodeReconciliationImpl.class);
85
86     private final DataBroker dataBroker;
87
88     private final ForwardingRulesManager provider;
89     public static final String SEPARATOR = ":";
90
91     private ListenerRegistration<FlowNodeReconciliationImpl> listenerRegistration;
92
93     private final int THREAD_POOL_SIZE = 4;
94     ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
95
96     private static final InstanceIdentifier<FlowCapableNode> II_TO_FLOW_CAPABLE_NODE
97             = InstanceIdentifier.builder(Nodes.class)
98             .child(Node.class)
99             .augmentation(FlowCapableNode.class)
100             .build();
101
102     public FlowNodeReconciliationImpl (final ForwardingRulesManager manager, final DataBroker db) {
103         this.provider = Preconditions.checkNotNull(manager, "ForwardingRulesManager can not be null!");
104         dataBroker = Preconditions.checkNotNull(db, "DataBroker can not be null!");
105         /* Build Path */
106         final InstanceIdentifier<FlowCapableNode> flowNodeWildCardIdentifier = InstanceIdentifier.create(Nodes.class)
107                 .child(Node.class).augmentation(FlowCapableNode.class);
108
109         final DataTreeIdentifier<FlowCapableNode> treeId =
110                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, flowNodeWildCardIdentifier);
111
112         try {
113         SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
114                 ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
115
116             listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowNodeReconciliationImpl>>() {
117                 @Override
118                 public ListenerRegistration<FlowNodeReconciliationImpl> call() throws Exception {
119                     return dataBroker.registerDataTreeChangeListener(treeId, FlowNodeReconciliationImpl.this);
120                 }
121             });
122         } catch (Exception e) {
123             LOG.warn("data listener registration failed: {}", e.getMessage());
124             LOG.debug("data listener registration failed.. ", e);
125             throw new IllegalStateException("FlowNodeReconciliation startup fail! System needs restart.", e);
126         }
127     }
128
129     @Override
130     public void close() {
131         if (listenerRegistration != null) {
132             try {
133                 listenerRegistration.close();
134             } catch (Exception e) {
135                 LOG.warn("Error by stop FRM FlowNodeReconilListener: {}", e.getMessage());
136                 LOG.debug("Error by stop FRM FlowNodeReconilListener..", e);
137             }
138             listenerRegistration = null;
139         }
140     }
141
142     @Override
143     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<FlowCapableNode>> changes) {
144         Preconditions.checkNotNull(changes, "Changes may not be null!");
145
146         for (DataTreeModification<FlowCapableNode> change : changes) {
147             final InstanceIdentifier<FlowCapableNode> key = change.getRootPath().getRootIdentifier();
148             final DataObjectModification<FlowCapableNode> mod = change.getRootNode();
149             final InstanceIdentifier<FlowCapableNode> nodeIdent =
150                     key.firstIdentifierOf(FlowCapableNode.class);
151
152             switch (mod.getModificationType()) {
153                 case DELETE:
154                     if (mod.getDataAfter() == null) {
155                         remove(key, mod.getDataBefore(), nodeIdent);
156                     }
157                     break;
158                 case SUBTREE_MODIFIED:
159                     //NO-OP since we donot need to reconciliate on Node-updated
160                     break;
161                 case WRITE:
162                     if (mod.getDataBefore() == null) {
163                         add(key, mod.getDataAfter(), nodeIdent);
164                     }
165                     break;
166                 default:
167                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
168             }
169         }
170     }
171
172
173
174     public void remove(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode del,
175                        InstanceIdentifier<FlowCapableNode> nodeIdent) {
176         if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE)){
177             if (LOG.isDebugEnabled()) {
178                 LOG.debug("Node removed: {}",nodeIdent.firstKeyOf(Node.class).getId().getValue());
179             }
180
181             if ( ! nodeIdent.isWildcarded()) {
182                 flowNodeDisconnected(nodeIdent);
183             }
184
185         }
186     }
187
188     public void add(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode add,
189                     InstanceIdentifier<FlowCapableNode> nodeIdent) {
190         if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE)){
191             if (LOG.isDebugEnabled()) {
192                 LOG.debug("Node added: {}",nodeIdent.firstKeyOf(Node.class).getId().getValue());
193             }
194
195             if ( ! nodeIdent.isWildcarded()) {
196                 flowNodeConnected(nodeIdent);
197             }
198         }
199     }
200
201     @Override
202     public void flowNodeDisconnected(InstanceIdentifier<FlowCapableNode> disconnectedNode) {
203         provider.unregistrateNode(disconnectedNode);
204     }
205
206     @Override
207     public void flowNodeConnected(InstanceIdentifier<FlowCapableNode> connectedNode) {
208         flowNodeConnected(connectedNode, false);
209     }
210
211     private void flowNodeConnected(InstanceIdentifier<FlowCapableNode> connectedNode, boolean force) {
212         if (force || !provider.isNodeActive(connectedNode)) {
213             provider.registrateNewNode(connectedNode);
214
215             if(!provider.isNodeOwner(connectedNode)) { return; }
216
217             if (provider.getConfiguration().isStaleMarkingEnabled()) {
218                 LOG.info("Stale-Marking is ENABLED and proceeding with deletion of stale-marked entities on switch {}",
219                         connectedNode.toString());
220                 reconciliationPreProcess(connectedNode);
221             }
222             ReconciliationTask reconciliationTask = new ReconciliationTask(connectedNode);
223             executor.execute(reconciliationTask);
224         }
225     }
226
227     private class ReconciliationTask implements Runnable {
228
229         InstanceIdentifier<FlowCapableNode> nodeIdentity;
230
231         public ReconciliationTask(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
232            nodeIdentity = nodeIdent;
233         }
234
235         @Override
236         public void run() {
237
238             String sNode = nodeIdentity.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
239             long nDpId = getDpnIdFromNodeName(sNode);
240
241             ReadOnlyTransaction trans = provider.getReadTranaction();
242             Optional<FlowCapableNode> flowNode = Optional.absent();
243
244             AtomicInteger counter = new AtomicInteger();
245             //initialize the counter
246             counter.set(0);
247             try {
248                 flowNode = trans.read(LogicalDatastoreType.CONFIGURATION, nodeIdentity).get();
249             } catch (Exception e) {
250                 LOG.error("Fail with read Config/DS for Node {} !", nodeIdentity, e);
251             }
252
253             if (flowNode.isPresent()) {
254             /* Tables - have to be pushed before groups */
255                 // CHECK if while pusing the update, updateTableInput can be null to emulate a table add
256                 List<TableFeatures> tableList = flowNode.get().getTableFeatures() != null
257                         ? flowNode.get().getTableFeatures() : Collections.<TableFeatures>emptyList();
258                 for (TableFeatures tableFeaturesItem : tableList) {
259                     TableFeaturesKey tableKey = tableFeaturesItem.getKey();
260                     KeyedInstanceIdentifier<TableFeatures, TableFeaturesKey> tableFeaturesII
261                             = nodeIdentity.child(TableFeatures.class, new TableFeaturesKey(tableKey.getTableId()));
262                     provider.getTableFeaturesCommiter().update(tableFeaturesII, tableFeaturesItem, null, nodeIdentity);
263                 }
264
265             /* Groups - have to be first */
266                 List<Group> groups = flowNode.get().getGroup() != null
267                         ? flowNode.get().getGroup() : Collections.<Group>emptyList();
268                 List<Group> toBeInstalledGroups = new ArrayList<>();
269                 toBeInstalledGroups.addAll(groups);
270                 List<Long> alreadyInstalledGroupids = new ArrayList<>();
271                 //new list for suspected groups pointing to ports .. when the ports come up late
272                 List<Group> suspectedGroups = new ArrayList<>();
273
274                 while ((!(toBeInstalledGroups.isEmpty()) || !(suspectedGroups.isEmpty())) &&
275                         (counter.get() <= provider.getConfiguration().getReconciliationRetryCount())) { //also check if the counter has not crossed the threshold
276
277                     if (toBeInstalledGroups.isEmpty() && !suspectedGroups.isEmpty()) {
278                         LOG.error("These Groups are pointing to node-connectors that are not up yet {}", suspectedGroups.toString());
279                         toBeInstalledGroups.addAll(suspectedGroups);
280                         break;
281                     }
282
283                     ListIterator<Group> iterator = toBeInstalledGroups.listIterator();
284                     while (iterator.hasNext()) {
285                         Group group = iterator.next();
286                         boolean okToInstall = true;
287                         for (Bucket bucket : group.getBuckets().getBucket()) {
288                             for (Action action : bucket.getAction()) {
289                                 //chained-port
290                                 if (action.getAction().getImplementedInterface().getName()
291                                         .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase")) {
292                                     String nodeConnectorUri = ((OutputActionCase) (action.getAction()))
293                                             .getOutputAction().getOutputNodeConnector().getValue();
294
295                                     LOG.warn("Installing the group for node connector {}", nodeConnectorUri);
296
297                                     //check if the nodeconnector is there in the multimap
298                                     boolean isPresent = provider.getFlowNodeConnectorInventoryTranslatorImpl()
299                                             .isNodeConnectorUpdated(nDpId, nodeConnectorUri);
300                                     //if yes set okToInstall = true
301
302                                     if (isPresent) {
303                                         break;
304                                     }//else put it in a different list and still set okToInstall = true
305                                     else {
306                                         suspectedGroups.add(group);
307                                         LOG.error("Not yet received the node-connector updated for {} " +
308                                                 "for the group with id {}", nodeConnectorUri, group.getGroupId().toString());
309                                         break;
310                                     }
311
312
313                                 }
314                                 //chained groups
315                                 else if (action.getAction().getImplementedInterface().getName()
316                                         .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCase")) {
317                                     Long groupId = ((GroupActionCase) (action.getAction())).getGroupAction().getGroupId();
318                                     if (!alreadyInstalledGroupids.contains(groupId)) {
319                                         okToInstall = false;
320                                         break;
321                                     }
322                                 }
323                             }
324                             if (!okToInstall) {
325                                 //increment retry counter value
326                                 counter.incrementAndGet();
327                                 break;
328                             }
329
330
331                         }
332
333
334                         if (okToInstall) {
335                             final KeyedInstanceIdentifier<Group, GroupKey> groupIdent =
336                                     nodeIdentity.child(Group.class, group.getKey());
337                             provider.getGroupCommiter().add(groupIdent, group, nodeIdentity);
338                             alreadyInstalledGroupids.add(group.getGroupId().getValue());
339                             iterator.remove();
340                             // resetting the counter to zero
341                             counter.set(0);
342                         }
343                     }
344                 }
345
346             /* installation of suspected groups*/
347                 if (!toBeInstalledGroups.isEmpty()) {
348                     for (Group group : toBeInstalledGroups) {
349                         LOG.error("Installing the group {} finally although the port is not up after checking for {} times "
350                                 , group.getGroupId().toString(), provider.getConfiguration().getReconciliationRetryCount());
351                         final KeyedInstanceIdentifier<Group, GroupKey> groupIdent =
352                                 nodeIdentity.child(Group.class, group.getKey());
353                         provider.getGroupCommiter().add(groupIdent, group, nodeIdentity);
354                     }
355                 }
356             /* Meters */
357                 List<Meter> meters = flowNode.get().getMeter() != null
358                         ? flowNode.get().getMeter() : Collections.<Meter>emptyList();
359                 for (Meter meter : meters) {
360                     final KeyedInstanceIdentifier<Meter, MeterKey> meterIdent =
361                             nodeIdentity.child(Meter.class, meter.getKey());
362                     provider.getMeterCommiter().add(meterIdent, meter, nodeIdentity);
363                 }
364             /* Flows */
365                 List<Table> tables = flowNode.get().getTable() != null
366                         ? flowNode.get().getTable() : Collections.<Table>emptyList();
367                 for (Table table : tables) {
368                     final KeyedInstanceIdentifier<Table, TableKey> tableIdent =
369                             nodeIdentity.child(Table.class, table.getKey());
370                     List<Flow> flows = table.getFlow() != null ? table.getFlow() : Collections.<Flow>emptyList();
371                     for (Flow flow : flows) {
372                         final KeyedInstanceIdentifier<Flow, FlowKey> flowIdent =
373                                 tableIdent.child(Flow.class, flow.getKey());
374                         provider.getFlowCommiter().add(flowIdent, flow, nodeIdentity);
375                     }
376                 }
377             }
378         /* clean transaction */
379             trans.close();
380         }
381     }
382     private long getDpnIdFromNodeName(String nodeName) {
383         String dpId = nodeName.substring(nodeName.lastIndexOf(SEPARATOR) + 1);
384         return Long.parseLong(dpId);
385     }
386
387     private void reconciliationPreProcess(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
388
389         List<InstanceIdentifier<StaleFlow>> staleFlowsToBeBulkDeleted = Lists.newArrayList();
390         List<InstanceIdentifier<StaleGroup>> staleGroupsToBeBulkDeleted = Lists.newArrayList();
391         List<InstanceIdentifier<StaleMeter>> staleMetersToBeBulkDeleted = Lists.newArrayList();
392
393
394         ReadOnlyTransaction trans = provider.getReadTranaction();
395         Optional<FlowCapableNode> flowNode = Optional.absent();
396
397         try {
398             flowNode = trans.read(LogicalDatastoreType.CONFIGURATION, nodeIdent).get();
399         }
400         catch (Exception e) {
401             LOG.error("Reconciliation Pre-Processing Fail with read Config/DS for Node {} !", nodeIdent, e);
402         }
403
404         if (flowNode.isPresent()) {
405
406             LOG.debug("Proceeding with deletion of stale-marked Flows on switch {} using Openflow interface",
407                     nodeIdent.toString());
408             /* Stale-Flows - Stale-marked Flows have to be removed first for safety */
409             List<Table> tables = flowNode.get().getTable() != null
410                     ? flowNode.get().getTable() : Collections.<Table> emptyList();
411             for (Table table : tables) {
412                 final KeyedInstanceIdentifier<Table, TableKey> tableIdent =
413                         nodeIdent.child(Table.class, table.getKey());
414                 List<StaleFlow> staleFlows = table.getStaleFlow() != null ? table.getStaleFlow() : Collections.<StaleFlow> emptyList();
415                 for (StaleFlow staleFlow : staleFlows) {
416
417                     FlowBuilder flowBuilder = new FlowBuilder(staleFlow);
418                     Flow toBeDeletedFlow = flowBuilder.setId(staleFlow.getId()).build();
419
420                     final KeyedInstanceIdentifier<Flow, FlowKey> flowIdent =
421                             tableIdent.child(Flow.class, toBeDeletedFlow.getKey());
422
423
424                     this.provider.getFlowCommiter().remove(flowIdent, toBeDeletedFlow, nodeIdent);
425
426                     staleFlowsToBeBulkDeleted.add(getStaleFlowInstanceIdentifier(staleFlow, nodeIdent));
427                 }
428             }
429
430
431             LOG.debug("Proceeding with deletion of stale-marked Groups for switch {} using Openflow interface",
432                     nodeIdent.toString());
433
434             // TODO: Should we collate the futures of RPC-calls to be sure that groups are Flows are fully deleted
435             // before attempting to delete groups - just in case there are references
436
437             /* Stale-marked Groups - Can be deleted after flows */
438             List<StaleGroup> staleGroups = flowNode.get().getStaleGroup() != null
439                     ? flowNode.get().getStaleGroup() : Collections.<StaleGroup> emptyList();
440             for (StaleGroup staleGroup : staleGroups) {
441
442                 GroupBuilder groupBuilder = new GroupBuilder(staleGroup);
443                 Group toBeDeletedGroup = groupBuilder.setGroupId(staleGroup.getGroupId()).build();
444
445                 final KeyedInstanceIdentifier<Group, GroupKey> groupIdent =
446                         nodeIdent.child(Group.class, toBeDeletedGroup.getKey());
447
448                 this.provider.getGroupCommiter().remove(groupIdent, toBeDeletedGroup, nodeIdent);
449
450                 staleGroupsToBeBulkDeleted.add(getStaleGroupInstanceIdentifier(staleGroup, nodeIdent));
451             }
452
453             LOG.debug("Proceeding with deletion of stale-marked Meters for switch {} using Openflow interface",
454                     nodeIdent.toString());
455             /* Stale-marked Meters - can be deleted anytime - so least priority */
456             List<StaleMeter> staleMeters = flowNode.get().getStaleMeter() != null
457                     ? flowNode.get().getStaleMeter() : Collections.<StaleMeter> emptyList();
458
459             for (StaleMeter staleMeter : staleMeters) {
460
461                 MeterBuilder meterBuilder = new MeterBuilder(staleMeter);
462                 Meter toBeDeletedMeter = meterBuilder.setMeterId(staleMeter.getMeterId()).build();
463
464                 final KeyedInstanceIdentifier<Meter, MeterKey> meterIdent =
465                         nodeIdent.child(Meter.class, toBeDeletedMeter.getKey());
466
467
468                 this.provider.getMeterCommiter().remove(meterIdent, toBeDeletedMeter, nodeIdent);
469
470                 staleMetersToBeBulkDeleted.add(getStaleMeterInstanceIdentifier(staleMeter, nodeIdent));
471             }
472
473         }
474         /* clean transaction */
475         trans.close();
476
477         LOG.debug("Deleting all stale-marked flows/groups/meters of for switch {} in Configuration DS",
478                 nodeIdent.toString());
479                 // Now, do the bulk deletions
480                 deleteDSStaleFlows(staleFlowsToBeBulkDeleted);
481         deleteDSStaleGroups(staleGroupsToBeBulkDeleted);
482         deleteDSStaleMeters(staleMetersToBeBulkDeleted);
483
484     }
485
486
487     private void deleteDSStaleFlows(List<InstanceIdentifier<StaleFlow>> flowsForBulkDelete){
488         ImmutableList.Builder<InstanceIdentifier<StaleFlow>> builder = ImmutableList.builder();
489         ImmutableList<InstanceIdentifier<StaleFlow>> bulkDelFlows = builder.addAll(flowsForBulkDelete.iterator()).build();
490
491         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
492
493         for (InstanceIdentifier<StaleFlow> staleFlowIId : flowsForBulkDelete){
494             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, staleFlowIId);
495         }
496
497         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTransaction.submit();
498         handleStaleEntityDeletionResultFuture(submitFuture);
499     }
500
501     private void deleteDSStaleGroups(List<InstanceIdentifier<StaleGroup>> groupsForBulkDelete){
502         ImmutableList.Builder<InstanceIdentifier<StaleGroup>> builder = ImmutableList.builder();
503         ImmutableList<InstanceIdentifier<StaleGroup>> bulkDelGroups = builder.addAll(groupsForBulkDelete.iterator()).build();
504
505         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
506
507         for (InstanceIdentifier<StaleGroup> staleGroupIId : groupsForBulkDelete){
508             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, staleGroupIId);
509         }
510
511         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTransaction.submit();
512         handleStaleEntityDeletionResultFuture(submitFuture);
513
514     }
515
516     private void deleteDSStaleMeters(List<InstanceIdentifier<StaleMeter>> metersForBulkDelete){
517         ImmutableList.Builder<InstanceIdentifier<StaleMeter>> builder = ImmutableList.builder();
518         ImmutableList<InstanceIdentifier<StaleMeter>> bulkDelGroups = builder.addAll(metersForBulkDelete.iterator()).build();
519
520         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
521
522         for (InstanceIdentifier<StaleMeter> staleMeterIId : metersForBulkDelete){
523             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, staleMeterIId);
524         }
525
526         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTransaction.submit();
527         handleStaleEntityDeletionResultFuture(submitFuture);
528
529
530     }
531
532
533     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow> getStaleFlowInstanceIdentifier(StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent) {
534         return nodeIdent
535                 .child(Table.class, new TableKey(staleFlow.getTableId()))
536                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow.class,
537                         new StaleFlowKey(new FlowId(staleFlow.getId())));
538     }
539
540     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup> getStaleGroupInstanceIdentifier(StaleGroup staleGroup, InstanceIdentifier<FlowCapableNode> nodeIdent) {
541         return nodeIdent
542                 .child(StaleGroup.class, new StaleGroupKey(new GroupId(staleGroup.getGroupId())));
543     }
544
545
546     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeter> getStaleMeterInstanceIdentifier(StaleMeter staleMeter, InstanceIdentifier<FlowCapableNode> nodeIdent) {
547         return nodeIdent
548                 .child(StaleMeter.class, new StaleMeterKey(new MeterId(staleMeter.getMeterId())));
549     }
550
551
552     private void handleStaleEntityDeletionResultFuture(CheckedFuture<Void, TransactionCommitFailedException> submitFuture) {
553         Futures.addCallback(submitFuture, new FutureCallback<Void>() {
554             @Override
555             public void onSuccess(Void result) { LOG.debug("Stale entity removal success");
556             }
557
558             @Override
559             public void onFailure(Throwable t) {
560                 LOG.error("Stale entity removal failed {}", t);
561             }
562         });
563
564     }
565
566
567     private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
568                                                   InstanceIdentifier<?> identifier2) {
569         return Iterables.getLast(identifier1.getPathArguments()).equals(Iterables.getLast(identifier2.getPathArguments()));
570     }
571 }
572