Merge "BUG8607 Fix checkstyle issues"
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / AbstractListeningCommiter.java
1 /**
2  * Copyright (c) 2014, 2017 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 package org.opendaylight.openflowplugin.applications.frm.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
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.ForwardingRulesCommiter;
15 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * AbstractChangeListner implemented basic {@link org.opendaylight.controller.md.sal.binding.api.DataTreeModification}
24  * processing for flow node subDataObject (flows, groups and meters).
25  */
26 public abstract class AbstractListeningCommiter<T extends DataObject> implements ForwardingRulesCommiter<T> {
27
28     private static final Logger LOG = LoggerFactory.getLogger(AbstractListeningCommiter.class);
29     ForwardingRulesManager provider;
30     private final Class<T> clazz;
31
32     public AbstractListeningCommiter(ForwardingRulesManager provider, Class<T> clazz) {
33         this.provider = Preconditions.checkNotNull(provider, "ForwardingRulesManager can not be null!");
34         this.clazz = Preconditions.checkNotNull(clazz, "Class can not be null!");
35     }
36
37     @Override
38     public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
39         Preconditions.checkNotNull(changes, "Changes may not be null!");
40         LOG.trace("Received data changes :{}", changes);
41
42         for (DataTreeModification<T> change : changes) {
43             final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
44             final DataObjectModification<T> mod = change.getRootNode();
45             final InstanceIdentifier<FlowCapableNode> nodeIdent =
46                     key.firstIdentifierOf(FlowCapableNode.class);
47             if (preConfigurationCheck(nodeIdent)) {
48                 switch (mod.getModificationType()) {
49                     case DELETE:
50                         remove(key, mod.getDataBefore(), nodeIdent);
51                         break;
52                     case SUBTREE_MODIFIED:
53                         update(key, mod.getDataBefore(), mod.getDataAfter(), nodeIdent);
54                         break;
55                     case WRITE:
56                         if (mod.getDataBefore() == null) {
57                             add(key, mod.getDataAfter(), nodeIdent);
58                         } else {
59                             update(key, mod.getDataBefore(), mod.getDataAfter(), nodeIdent);
60                         }
61                         break;
62                     default:
63                         throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
64                 }
65             } else {
66                 if (provider.isStaleMarkingEnabled()) {
67                     LOG.info("Stale-Marking ENABLED and switch {} is NOT connected, storing stale entities",
68                             nodeIdent.toString());
69                     // Switch is NOT connected
70                     switch (mod.getModificationType()) {
71                         case DELETE:
72                             createStaleMarkEntity(key, mod.getDataBefore(), nodeIdent);
73                             break;
74                         case SUBTREE_MODIFIED:
75                             break;
76                         case WRITE:
77                             break;
78                         default:
79                             throw new
80                             IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
81                     }
82                 }
83             }
84         }
85     }
86
87     /**
88      * Method return wildCardPath for Listener registration
89      * and for identify the correct KeyInstanceIdentifier from data.
90      */
91     protected abstract InstanceIdentifier<T> getWildCardPath();
92
93     private boolean preConfigurationCheck(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
94         Preconditions.checkNotNull(nodeIdent, "FlowCapableNode identifier can not be null!");
95         // In single node cluster, node should be in local cache before we get any flow/group/meter
96         // data change event from data store. So first check should pass.
97         // In case of 3-node cluster, when shard leader changes, clustering will send blob of data
98         // present in operational data store and config data store. So ideally local node cache
99         // should get populated. But to handle a scenario where flow request comes before the blob
100         // of config/operational data gets processes, it won't find node in local cache and it will
101         // skip the flow/group/meter operational. This requires an addition check, where it reads
102         // node from operational data store and if it's present it calls flowNodeConnected to explicitly
103         // trigger the event of new node connected.
104
105         if (!provider.isNodeOwner(nodeIdent)) {
106             return false;
107         }
108
109         if (!provider.isNodeActive(nodeIdent)) {
110             if (provider.checkNodeInOperationalDataStore(nodeIdent)) {
111                 return true;
112             } else {
113                 return false;
114             }
115         }
116         return true;
117     }
118 }
119