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