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