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