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