Merge "BUG-4117: add support of Old Notif. for Flow Statistics"
[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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
10
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
14 import org.opendaylight.openflowplugin.applications.frm.FlowCapableNodeConnectorCommitter;
15 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
16 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21
22 import java.util.Collection;
23
24 public abstract class AbstractNodeConnectorCommitter <T extends DataObject> implements FlowCapableNodeConnectorCommitter<T> {
25     protected ForwardingRulesManager provider;
26
27     protected final Class<T> clazz;
28
29     public AbstractNodeConnectorCommitter (ForwardingRulesManager provider, Class<T> clazz) {
30         this.provider = Preconditions.checkNotNull(provider, "ForwardingRulesManager can not be null!");
31         this.clazz = Preconditions.checkNotNull(clazz, "Class can not be null!");
32     }
33
34     @Override
35     public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
36         Preconditions.checkNotNull(changes, "Changes may not be null!");
37
38         for (DataTreeModification<T> change : changes) {
39             final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
40             final DataObjectModification<T> mod = change.getRootNode();
41             final InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent =
42                     key.firstIdentifierOf(FlowCapableNodeConnector.class);
43
44             if (preConfigurationCheck(nodeConnIdent)) {
45                 switch (mod.getModificationType()) {
46                     case DELETE:
47                         remove(key, mod.getDataBefore(), nodeConnIdent);
48                         break;
49                     case SUBTREE_MODIFIED:
50                         update(key, mod.getDataBefore(), mod.getDataAfter(), nodeConnIdent);
51                         break;
52                     case WRITE:
53                         if (mod.getDataBefore() == null) {
54                             add(key, mod.getDataAfter(), nodeConnIdent);
55                         } else {
56                             update(key, mod.getDataBefore(), mod.getDataAfter(), nodeConnIdent);
57                         }
58                         break;
59                     default:
60                         throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
61                 }
62             }
63         }
64     }
65
66     /**
67      * Method return wildCardPath for Listener registration
68      * and for identify the correct KeyInstanceIdentifier from data;
69      */
70     protected abstract InstanceIdentifier<T> getWildCardPath();
71
72     private boolean preConfigurationCheck(final InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
73         Preconditions.checkNotNull(nodeConnIdent, "FlowCapableNodeConnector ident can not be null!");
74         return true;
75         //return provider.isNodeActive(nodeConnIdent);
76     }
77 }