Merge "DevManager functionality add"
[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             LOG.warn("Node removed: {}",nodeIdent.firstKeyOf(Node.class).getId().getValue());
179
180             if ( ! nodeIdent.isWildcarded()) {
181                 flowNodeDisconnected(nodeIdent);
182             }
183
184         }
185     }
186
187     public void add(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode add,
188                     InstanceIdentifier<FlowCapableNode> nodeIdent) {
189         if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE)){
190             LOG.warn("Node added: {}",nodeIdent.firstKeyOf(Node.class).getId().getValue());
191
192             if ( ! nodeIdent.isWildcarded()) {
193                 flowNodeConnected(nodeIdent);
194             }
195         }
196     }
197
198     @Override
199     public void flowNodeDisconnected(InstanceIdentifier<FlowCapableNode> disconnectedNode) {
200         provider.unregistrateNode(disconnectedNode);
201     }
202
203     @Override
204     public void flowNodeConnected(InstanceIdentifier<FlowCapableNode> connectedNode) {
205         flowNodeConnected(connectedNode, false);
206     }
207
208     private void flowNodeConnected(InstanceIdentifier<FlowCapableNode> connectedNode, boolean force) {
209         if (force || !provider.isNodeActive(connectedNode)) {
210             provider.registrateNewNode(connectedNode);
211
212             if(!provider.isNodeOwner(connectedNode)) { return; }
213
214             if (provider.getConfiguration().isStaleMarkingEnabled()) {
215                 LOG.info("Stale-Marking is ENABLED and proceeding with deletion of stale-marked entities on switch {}",
216                         connectedNode.toString());
217                 reconciliationPreProcess(connectedNode);
218             }
219             reconciliation(connectedNode);
220         }
221     }
222
223     private void reconciliation(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
224
225         String sNode = nodeIdent.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
226         long nDpId = getDpnIdFromNodeName(sNode);
227
228         ReadOnlyTransaction trans = provider.getReadTranaction();
229         Optional<FlowCapableNode> flowNode = Optional.absent();
230
231         AtomicInteger counter = new AtomicInteger();
232         //initialize the counter
233         counter.set(0);
234         try {
235             flowNode = trans.read(LogicalDatastoreType.CONFIGURATION, nodeIdent).get();
236         }
237         catch (Exception e) {
238             LOG.error("Fail with read Config/DS for Node {} !", nodeIdent, e);
239         }
240
241         if (flowNode.isPresent()) {
242             /* Tables - have to be pushed before groups */
243             // CHECK if while pusing the update, updateTableInput can be null to emulate a table add
244             List<TableFeatures> tableList = flowNode.get().getTableFeatures() != null
245                     ? flowNode.get().getTableFeatures() : Collections.<TableFeatures> emptyList() ;
246             for (TableFeatures tableFeaturesItem : tableList) {
247                 TableFeaturesKey tableKey = tableFeaturesItem.getKey();
248                 KeyedInstanceIdentifier<TableFeatures, TableFeaturesKey> tableFeaturesII
249                     = nodeIdent.child(TableFeatures.class, new TableFeaturesKey(tableKey.getTableId()));
250                         provider.getTableFeaturesCommiter().update(tableFeaturesII, tableFeaturesItem, null, nodeIdent);
251             }
252
253             /* Groups - have to be first */
254                 List<Group> groups = flowNode.get().getGroup() != null
255                         ? flowNode.get().getGroup() : Collections.<Group>emptyList();
256                 List<Group> toBeInstalledGroups = new ArrayList<>();
257                 toBeInstalledGroups.addAll(groups);
258                 List<Long> alreadyInstalledGroupids = new ArrayList<>();
259                 //new list for suspected groups pointing to ports .. when the ports come up late
260                 List<Group> suspectedGroups = new ArrayList<>();
261
262                 while ((!(toBeInstalledGroups.isEmpty()) || !(suspectedGroups.isEmpty())) &&
263                         (counter.get()<=provider.getConfiguration().getReconciliationRetryCount())) { //also check if the counter has not crossed the threshold
264
265                     if(toBeInstalledGroups.isEmpty() && ! suspectedGroups.isEmpty()){
266                         LOG.error("These Groups are pointing to node-connectors that are not up yet {}",suspectedGroups.toString());
267                         toBeInstalledGroups.addAll(suspectedGroups);
268                         break;
269                     }
270
271                     ListIterator<Group> iterator = toBeInstalledGroups.listIterator();
272                     while (iterator.hasNext()) {
273                         Group group = iterator.next();
274                         boolean okToInstall = true;
275                         for (Bucket bucket : group.getBuckets().getBucket()) {
276                             for (Action action : bucket.getAction()) {
277                                //chained-port
278                                 if (action.getAction().getImplementedInterface().getName()
279                                         .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase")){
280                                     String nodeConnectorUri = ((OutputActionCase)(action.getAction()))
281                                             .getOutputAction().getOutputNodeConnector().getValue();
282
283                                     LOG.warn("Installing the group for node connector {}",nodeConnectorUri);
284
285                                     //check if the nodeconnector is there in the multimap
286                                     boolean isPresent = provider.getFlowNodeConnectorInventoryTranslatorImpl()
287                                             .isNodeConnectorUpdated(nDpId, nodeConnectorUri);
288                                     //if yes set okToInstall = true
289
290                                     if(isPresent){
291                                        break;
292                                     }//else put it in a different list and still set okToInstall = true
293                                     else {
294                                         suspectedGroups.add(group);
295                                         LOG.error("Not yet received the node-connector updated for {} " +
296                                                 "for the group with id {}",nodeConnectorUri,group.getGroupId().toString());
297                                          break;
298                                     }
299
300
301                                 }
302                                 //chained groups
303                                 else if (action.getAction().getImplementedInterface().getName()
304                                         .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCase")) {
305                                     Long groupId = ((GroupActionCase) (action.getAction())).getGroupAction().getGroupId();
306                                     if (!alreadyInstalledGroupids.contains(groupId)) {
307                                         okToInstall = false;
308                                         break;
309                                     }
310                                 }
311                             }
312                             if (!okToInstall){
313                                 //increment retry counter value
314                                 counter.incrementAndGet();
315                                 break;
316                             }
317
318
319
320                         }
321
322
323                         if (okToInstall) {
324                             final KeyedInstanceIdentifier<Group, GroupKey> groupIdent =
325                                     nodeIdent.child(Group.class, group.getKey());
326                             this.provider.getGroupCommiter().add(groupIdent, group, nodeIdent);
327                             alreadyInstalledGroupids.add(group.getGroupId().getValue());
328                             iterator.remove();
329                             // resetting the counter to zero
330                             counter.set(0);
331                         }
332                     }
333                 }
334
335             /* installation of suspected groups*/
336             if(!toBeInstalledGroups.isEmpty()){
337                 for(Group group :toBeInstalledGroups){
338                     LOG.error("Installing the group {} finally although the port is not up after checking for {} times "
339                             ,group.getGroupId().toString(),provider.getConfiguration().getReconciliationRetryCount());
340                     final KeyedInstanceIdentifier<Group, GroupKey> groupIdent =
341                             nodeIdent.child(Group.class, group.getKey());
342                     this.provider.getGroupCommiter().add(groupIdent, group, nodeIdent);
343                 }
344             }
345             /* Meters */
346             List<Meter> meters = flowNode.get().getMeter() != null
347                     ? flowNode.get().getMeter() : Collections.<Meter> emptyList();
348             for (Meter meter : meters) {
349                 final KeyedInstanceIdentifier<Meter, MeterKey> meterIdent =
350                         nodeIdent.child(Meter.class, meter.getKey());
351                 this.provider.getMeterCommiter().add(meterIdent, meter, nodeIdent);
352             }
353             /* Flows */
354             List<Table> tables = flowNode.get().getTable() != null
355                     ? flowNode.get().getTable() : Collections.<Table> emptyList();
356             for (Table table : tables) {
357                 final KeyedInstanceIdentifier<Table, TableKey> tableIdent =
358                         nodeIdent.child(Table.class, table.getKey());
359                 List<Flow> flows = table.getFlow() != null ? table.getFlow() : Collections.<Flow> emptyList();
360                 for (Flow flow : flows) {
361                     final KeyedInstanceIdentifier<Flow, FlowKey> flowIdent =
362                             tableIdent.child(Flow.class, flow.getKey());
363                     this.provider.getFlowCommiter().add(flowIdent, flow, nodeIdent);
364                 }
365             }
366         }
367         /* clean transaction */
368         trans.close();
369     }
370         private long getDpnIdFromNodeName(String nodeName) {
371         String dpId = nodeName.substring(nodeName.lastIndexOf(SEPARATOR) + 1);
372                 return Long.parseLong(dpId);
373         }
374
375     private void reconciliationPreProcess(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
376
377
378         List<InstanceIdentifier<StaleFlow>> staleFlowsToBeBulkDeleted = Lists.newArrayList();
379         List<InstanceIdentifier<StaleGroup>> staleGroupsToBeBulkDeleted = Lists.newArrayList();
380         List<InstanceIdentifier<StaleMeter>> staleMetersToBeBulkDeleted = Lists.newArrayList();
381
382
383         ReadOnlyTransaction trans = provider.getReadTranaction();
384         Optional<FlowCapableNode> flowNode = Optional.absent();
385
386         try {
387             flowNode = trans.read(LogicalDatastoreType.CONFIGURATION, nodeIdent).get();
388         }
389         catch (Exception e) {
390             LOG.error("Reconciliation Pre-Processing Fail with read Config/DS for Node {} !", nodeIdent, e);
391         }
392
393         if (flowNode.isPresent()) {
394
395             LOG.debug("Proceeding with deletion of stale-marked Flows on switch {} using Openflow interface",
396                     nodeIdent.toString());
397             /* Stale-Flows - Stale-marked Flows have to be removed first for safety */
398             List<Table> tables = flowNode.get().getTable() != null
399                     ? flowNode.get().getTable() : Collections.<Table> emptyList();
400             for (Table table : tables) {
401                 final KeyedInstanceIdentifier<Table, TableKey> tableIdent =
402                         nodeIdent.child(Table.class, table.getKey());
403                 List<StaleFlow> staleFlows = table.getStaleFlow() != null ? table.getStaleFlow() : Collections.<StaleFlow> emptyList();
404                 for (StaleFlow staleFlow : staleFlows) {
405
406                     FlowBuilder flowBuilder = new FlowBuilder(staleFlow);
407                     Flow toBeDeletedFlow = flowBuilder.setId(staleFlow.getId()).build();
408
409                     final KeyedInstanceIdentifier<Flow, FlowKey> flowIdent =
410                             tableIdent.child(Flow.class, toBeDeletedFlow.getKey());
411
412
413                     this.provider.getFlowCommiter().remove(flowIdent, toBeDeletedFlow, nodeIdent);
414
415                     staleFlowsToBeBulkDeleted.add(getStaleFlowInstanceIdentifier(staleFlow, nodeIdent));
416                 }
417             }
418
419
420             LOG.debug("Proceeding with deletion of stale-marked Groups for switch {} using Openflow interface",
421                     nodeIdent.toString());
422
423             // TODO: Should we collate the futures of RPC-calls to be sure that groups are Flows are fully deleted
424             // before attempting to delete groups - just in case there are references
425
426             /* Stale-marked Groups - Can be deleted after flows */
427             List<StaleGroup> staleGroups = flowNode.get().getStaleGroup() != null
428                     ? flowNode.get().getStaleGroup() : Collections.<StaleGroup> emptyList();
429             for (StaleGroup staleGroup : staleGroups) {
430
431                 GroupBuilder groupBuilder = new GroupBuilder(staleGroup);
432                 Group toBeDeletedGroup = groupBuilder.setGroupId(staleGroup.getGroupId()).build();
433
434                 final KeyedInstanceIdentifier<Group, GroupKey> groupIdent =
435                         nodeIdent.child(Group.class, toBeDeletedGroup.getKey());
436
437                 this.provider.getGroupCommiter().remove(groupIdent, toBeDeletedGroup, nodeIdent);
438
439                 staleGroupsToBeBulkDeleted.add(getStaleGroupInstanceIdentifier(staleGroup, nodeIdent));
440             }
441
442             LOG.debug("Proceeding with deletion of stale-marked Meters for switch {} using Openflow interface",
443                     nodeIdent.toString());
444             /* Stale-marked Meters - can be deleted anytime - so least priority */
445             List<StaleMeter> staleMeters = flowNode.get().getStaleMeter() != null
446                     ? flowNode.get().getStaleMeter() : Collections.<StaleMeter> emptyList();
447
448             for (StaleMeter staleMeter : staleMeters) {
449
450                 MeterBuilder meterBuilder = new MeterBuilder(staleMeter);
451                 Meter toBeDeletedMeter = meterBuilder.setMeterId(staleMeter.getMeterId()).build();
452
453                 final KeyedInstanceIdentifier<Meter, MeterKey> meterIdent =
454                         nodeIdent.child(Meter.class, toBeDeletedMeter.getKey());
455
456
457                 this.provider.getMeterCommiter().remove(meterIdent, toBeDeletedMeter, nodeIdent);
458
459                 staleMetersToBeBulkDeleted.add(getStaleMeterInstanceIdentifier(staleMeter, nodeIdent));
460             }
461
462         }
463         /* clean transaction */
464         trans.close();
465
466         LOG.debug("Deleting all stale-marked flows/groups/meters of for switch {} in Configuration DS",
467                 nodeIdent.toString());
468                 // Now, do the bulk deletions
469                 deleteDSStaleFlows(staleFlowsToBeBulkDeleted);
470         deleteDSStaleGroups(staleGroupsToBeBulkDeleted);
471         deleteDSStaleMeters(staleMetersToBeBulkDeleted);
472
473     }
474
475
476     private void deleteDSStaleFlows(List<InstanceIdentifier<StaleFlow>> flowsForBulkDelete){
477         ImmutableList.Builder<InstanceIdentifier<StaleFlow>> builder = ImmutableList.builder();
478         ImmutableList<InstanceIdentifier<StaleFlow>> bulkDelFlows = builder.addAll(flowsForBulkDelete.iterator()).build();
479
480         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
481
482         for (InstanceIdentifier<StaleFlow> staleFlowIId : flowsForBulkDelete){
483             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, staleFlowIId);
484         }
485
486         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTransaction.submit();
487         handleStaleEntityDeletionResultFuture(submitFuture);
488     }
489
490     private void deleteDSStaleGroups(List<InstanceIdentifier<StaleGroup>> groupsForBulkDelete){
491         ImmutableList.Builder<InstanceIdentifier<StaleGroup>> builder = ImmutableList.builder();
492         ImmutableList<InstanceIdentifier<StaleGroup>> bulkDelGroups = builder.addAll(groupsForBulkDelete.iterator()).build();
493
494         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
495
496         for (InstanceIdentifier<StaleGroup> staleGroupIId : groupsForBulkDelete){
497             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, staleGroupIId);
498         }
499
500         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTransaction.submit();
501         handleStaleEntityDeletionResultFuture(submitFuture);
502
503     }
504
505     private void deleteDSStaleMeters(List<InstanceIdentifier<StaleMeter>> metersForBulkDelete){
506         ImmutableList.Builder<InstanceIdentifier<StaleMeter>> builder = ImmutableList.builder();
507         ImmutableList<InstanceIdentifier<StaleMeter>> bulkDelGroups = builder.addAll(metersForBulkDelete.iterator()).build();
508
509         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
510
511         for (InstanceIdentifier<StaleMeter> staleMeterIId : metersForBulkDelete){
512             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, staleMeterIId);
513         }
514
515         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTransaction.submit();
516         handleStaleEntityDeletionResultFuture(submitFuture);
517
518
519     }
520
521
522     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow> getStaleFlowInstanceIdentifier(StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent) {
523         return nodeIdent
524                 .child(Table.class, new TableKey(staleFlow.getTableId()))
525                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow.class,
526                         new StaleFlowKey(new FlowId(staleFlow.getId())));
527     }
528
529     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup> getStaleGroupInstanceIdentifier(StaleGroup staleGroup, InstanceIdentifier<FlowCapableNode> nodeIdent) {
530         return nodeIdent
531                 .child(StaleGroup.class, new StaleGroupKey(new GroupId(staleGroup.getGroupId())));
532     }
533
534
535     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeter> getStaleMeterInstanceIdentifier(StaleMeter staleMeter, InstanceIdentifier<FlowCapableNode> nodeIdent) {
536         return nodeIdent
537                 .child(StaleMeter.class, new StaleMeterKey(new MeterId(staleMeter.getMeterId())));
538     }
539
540
541     private void handleStaleEntityDeletionResultFuture(CheckedFuture<Void, TransactionCommitFailedException> submitFuture) {
542         Futures.addCallback(submitFuture, new FutureCallback<Void>() {
543             @Override
544             public void onSuccess(Void result) { LOG.debug("Stale entity removal success");
545             }
546
547             @Override
548             public void onFailure(Throwable t) {
549                 LOG.error("Stale entity removal failed {}", t);
550             }
551         });
552
553     }
554
555
556     private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
557                                                   InstanceIdentifier<?> identifier2) {
558         return Iterables.getLast(identifier1.getPathArguments()).equals(Iterables.getLast(identifier2.getPathArguments()));
559     }
560 }
561