Merge "Add NodeConfiguratorImpl enqueue trace"
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / AbstractNodeConnectorCommitter.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.openflowplugin.applications.frm.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import org.opendaylight.mdsal.binding.api.DataObjectModification;
13 import org.opendaylight.mdsal.binding.api.DataTreeModification;
14 import org.opendaylight.openflowplugin.applications.frm.FlowCapableNodeConnectorCommitter;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18
19 public abstract class AbstractNodeConnectorCommitter<T extends DataObject>
20         implements FlowCapableNodeConnectorCommitter<T> {
21
22     @Override
23     public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
24         Preconditions.checkNotNull(changes, "Changes may not be null!");
25
26         for (DataTreeModification<T> change : changes) {
27             final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
28             final DataObjectModification<T> mod = change.getRootNode();
29             final InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent = key
30                     .firstIdentifierOf(FlowCapableNodeConnector.class);
31
32             if (preConfigurationCheck(nodeConnIdent)) {
33                 switch (mod.getModificationType()) {
34                     case DELETE:
35                         remove(key, mod.getDataBefore(), nodeConnIdent);
36                         break;
37                     case SUBTREE_MODIFIED:
38                         update(key, mod.getDataBefore(), mod.getDataAfter(), nodeConnIdent);
39                         break;
40                     case WRITE:
41                         if (mod.getDataBefore() == null) {
42                             add(key, mod.getDataAfter(), nodeConnIdent);
43                         } else {
44                             update(key, mod.getDataBefore(), mod.getDataAfter(), nodeConnIdent);
45                         }
46                         break;
47                     default:
48                         throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
49                 }
50             }
51         }
52     }
53
54     /**
55      * Method return wildCardPath for Listener registration and for identify the
56      * correct KeyInstanceIdentifier from data.
57      */
58     protected abstract InstanceIdentifier<T> getWildCardPath();
59
60     private boolean preConfigurationCheck(final InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
61         Preconditions.checkNotNull(nodeConnIdent, "FlowCapableNodeConnector ident can not be null!");
62         return true;
63     }
64 }