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