Bug 6088 - Threads problem on gathering statistics
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SimplifiedOperationalListener.java
1 /**
2  * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collection;
14 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
16 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
21 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
22 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Listens to operational new nodes and delegates add/remove/update/barrier to {@link SyncReactor}.
35  */
36 public class SimplifiedOperationalListener extends AbstractFrmSyncListener<Node> {
37     private static final Logger LOG = LoggerFactory.getLogger(SimplifiedOperationalListener.class);
38
39     private final SyncReactor reactor;
40     private FlowCapableNodeSnapshotDao operationalSnapshot;
41     private FlowCapableNodeDao configDao;
42
43     public SimplifiedOperationalListener(SyncReactor reactor,
44             FlowCapableNodeSnapshotDao operationalSnapshot, FlowCapableNodeDao configDao) {
45         this.reactor = reactor;
46         this.operationalSnapshot = operationalSnapshot;
47         this.configDao = configDao;
48     }
49
50     @Override
51     public void onDataTreeChanged(Collection<DataTreeModification<Node>> modifications) {
52         LOG.trace("Inventory Operational changes {}", modifications.size());
53         super.onDataTreeChanged(modifications);
54     }
55
56     /**
57      * This method behaves like this:
58      * <ul>
59      * <li>If node is added to operational store then reconciliation.</li>
60      * <li>Node is deleted from operational cache is removed.</li>
61      * <li>Skip this event otherwise.</li>
62      * </ul>
63      *
64      * @throws InterruptedException from syncup
65      */
66     protected Optional<ListenableFuture<Boolean>> processNodeModification(
67             DataTreeModification<Node> modification) throws ReadFailedException, InterruptedException {
68         updateCache(modification);
69
70         if (isAdd(modification) || isAddLogical(modification)) {
71             return reconciliation(modification);
72         }
73         // TODO: else = explicit reconciliation required
74
75         return skipModification(modification);
76     }
77
78     /**
79      * Remove if delete. Update only if FlowCapableNode Augmentation modified.
80      *
81      * @param modification Datastore modification
82      */
83     protected void updateCache(DataTreeModification<Node> modification) {
84         try {
85             boolean isDelete = isDelete(modification) || isDeleteLogical(modification);
86             if (isDelete) {
87                 operationalSnapshot.updateCache(nodeId(modification), Optional.<FlowCapableNode>absent());
88                 return;
89             }
90
91             operationalSnapshot.updateCache(nodeId(modification), Optional.fromNullable(flowCapableNodeAfter(modification)));
92         } catch(Exception e) {
93             LOG.error("update cache failed {}", nodeId(modification), e);
94         }
95     }
96
97     protected Optional<ListenableFuture<Boolean>> skipModification(DataTreeModification<Node> modification) {
98         LOG.trace("Skipping Inventory Operational modification {}, before {}, after {}", nodeIdValue(modification),
99                 modification.getRootNode().getDataBefore() == null ? "null" : "nonnull",
100                 modification.getRootNode().getDataAfter() == null ? "null" : "nonnull");
101         return Optional.absent();// skip otherwise event
102     }
103
104     /**
105      * ModificationType.DELETE
106      */
107     protected boolean isDelete(DataTreeModification<Node> modification) {
108         if (ModificationType.DELETE == modification.getRootNode().getModificationType()) {
109             LOG.trace("Delete {} (physical)", nodeIdValue(modification));
110             return true;
111         }
112
113         return false;
114     }
115
116     /**
117      * All connectors disappeared from operational store (logical delete).
118      */
119     protected boolean isDeleteLogical(DataTreeModification<Node> modification) {
120         final DataObjectModification<Node> rootNode = modification.getRootNode();
121         if (!safeConnectorsEmpty(rootNode.getDataBefore()) && safeConnectorsEmpty(rootNode.getDataAfter())) {
122             LOG.trace("Delete {} (logical)", nodeIdValue(modification));
123             return true;
124         }
125
126         return false;
127     }
128
129     protected boolean isAdd(DataTreeModification<Node> modification) {
130         final DataObjectModification<Node> rootNode = modification.getRootNode();
131         final Node dataAfter = rootNode.getDataAfter();
132         final Node dataBefore = rootNode.getDataBefore();
133
134         final boolean nodeAppearedInOperational = dataBefore == null && dataAfter != null;
135         if (nodeAppearedInOperational) {
136             LOG.trace("Add {} (physical)", nodeIdValue(modification));
137         }
138         return nodeAppearedInOperational;
139     }
140
141     /**
142      * All connectors appeared in operational store (logical add).
143      */
144     protected boolean isAddLogical(DataTreeModification<Node> modification) {
145         final DataObjectModification<Node> rootNode = modification.getRootNode();
146         if (safeConnectorsEmpty(rootNode.getDataBefore()) && !safeConnectorsEmpty(rootNode.getDataAfter())) {
147             LOG.trace("Add {} (logical)", nodeIdValue(modification));
148             return true;
149         }
150
151         return false;
152     }
153
154     protected Optional<ListenableFuture<Boolean>> reconciliation(
155             DataTreeModification<Node> modification) throws InterruptedException {
156         final NodeId nodeId = nodeId(modification);
157
158         LOG.debug("Reconciliation: {}", nodeId.getValue());
159
160         final Optional<FlowCapableNode> nodeConfiguration = configDao.loadByNodeId(nodeId);
161         final InstanceIdentifier<FlowCapableNode> nodePath = InstanceIdentifier.create(Nodes.class)
162                 .child(Node.class, new NodeKey(nodeId(modification))).augmentation(FlowCapableNode.class);
163
164         if (nodeConfiguration.isPresent())
165             return Optional.of(reactor.syncup(nodePath, nodeConfiguration.get(), flowCapableNodeAfter(modification)));
166         else
167             return skipModification(modification);
168     }
169
170     static FlowCapableNode flowCapableNodeAfter(DataTreeModification<Node> modification) {
171         final Node dataAfter = modification.getRootNode().getDataAfter();
172         if (dataAfter == null) {
173             return null;
174         }
175         return dataAfter.getAugmentation(FlowCapableNode.class);
176     }
177
178     static boolean safeConnectorsEmpty(Node node) {
179         if (node == null) {
180             return true;
181         }
182
183         final List<NodeConnector> nodeConnectors = node.getNodeConnector();
184
185         return nodeConnectors == null || nodeConnectors.isEmpty();
186     }
187
188     static String nodeIdValue(DataTreeModification<Node> modification) {
189         final NodeId nodeId = nodeId(modification);
190
191         if (nodeId == null) {
192             return null;
193         }
194
195         return nodeId.getValue();
196     }
197
198     static NodeId nodeId(DataTreeModification<Node> modification) {
199         final DataObjectModification<Node> rootNode = modification.getRootNode();
200         final Node dataAfter = rootNode.getDataAfter();
201
202
203         if (dataAfter != null) {
204             return dataAfter.getId();
205         }
206
207         final Node dataBefore = rootNode.getDataBefore();
208         if (dataBefore != null) {
209             return dataBefore.getId();
210         }
211
212         return null;
213     }
214
215     @Override
216     public LogicalDatastoreType dsType() {
217         return LogicalDatastoreType.OPERATIONAL;
218     }
219 }