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