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