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