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