d9f9a361e363981e7f8a9e02763403b0570ca263
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SimplifiedConfigListener.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 javax.annotation.Nonnull;
15 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
19 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
20 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
21 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
22 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
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.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Listens to config changes and delegates sync entry to {@link SyncReactor}.
31  */
32 public class SimplifiedConfigListener extends AbstractFrmSyncListener<FlowCapableNode> {
33
34     private static final Logger LOG = LoggerFactory.getLogger(SimplifiedConfigListener.class);
35     private final SyncReactor reactor;
36     private final FlowCapableNodeSnapshotDao configSnapshot;
37     private final FlowCapableNodeDao operationalDao;
38
39     public SimplifiedConfigListener(final SyncReactor reactor,
40                                     final FlowCapableNodeSnapshotDao configSnapshot,
41                                     final FlowCapableNodeDao operationalDao) {
42         this.reactor = reactor;
43         this.configSnapshot = configSnapshot;
44         this.operationalDao = operationalDao;
45     }
46
47     @Override
48     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
49         super.onDataTreeChanged(modifications);
50     }
51
52     /**
53      * Update cache. If operational data are present, choose appropriate data and start syncup.
54      * Otherwise skip incoming change.
55      */
56     protected Optional<ListenableFuture<Boolean>> processNodeModification(
57             final DataTreeModification<FlowCapableNode> modification) {
58         final InstanceIdentifier<FlowCapableNode> nodePath = modification.getRootPath().getRootIdentifier();
59         final NodeId nodeId = PathUtil.digNodeId(nodePath);
60
61         configSnapshot.updateCache(nodeId, Optional.fromNullable(modification.getRootNode().getDataAfter()));
62
63         final Optional<FlowCapableNode> operationalNode = operationalDao.loadByNodeId(nodeId);
64         if (!operationalNode.isPresent()) {
65             LOG.debug("Skip syncup, {} operational is not present", nodeId.getValue());
66             return Optional.absent();
67         }
68
69         final DataObjectModification<FlowCapableNode> configModification = modification.getRootNode();
70         final FlowCapableNode dataBefore = configModification.getDataBefore();
71         final FlowCapableNode dataAfter = configModification.getDataAfter();
72         final ListenableFuture<Boolean> endResult;
73         if (dataBefore == null && dataAfter != null) {
74             endResult = onNodeAdded(nodePath, dataAfter, operationalNode.get());
75         } else if (dataBefore != null && dataAfter == null) {
76             endResult = onNodeDeleted(nodePath, dataBefore);
77         } else {
78             endResult = onNodeUpdated(nodePath, dataBefore, dataAfter);
79         }
80
81         return Optional.of(endResult);
82     }
83
84     /**
85      * If node was added to config DS and it is already present in operational DS (connected) diff between current
86      * new configuration and actual configuration (seen in operational) should be calculated and sent to device.
87      */
88     private ListenableFuture<Boolean> onNodeAdded(final InstanceIdentifier<FlowCapableNode> nodePath,
89                                                   final FlowCapableNode dataAfter,
90                                                   final FlowCapableNode operationalNode) {
91         LOG.debug("Reconciliation {}: {}", dsType(), PathUtil.digNodeId(nodePath).getValue());
92         final SyncupEntry syncupEntry = new SyncupEntry(dataAfter, dsType(), operationalNode, LogicalDatastoreType.OPERATIONAL);
93         return reactor.syncup(nodePath, syncupEntry);
94     }
95
96     /**
97      * Apply minimal changes very fast. For better performance needed just compare config
98      * after+before. Config listener should not be dependent on operational flows/groups/meters while
99      * updating config because operational store is highly async and it depends on another module in
100      * system which is updating operational store (that components is also trying to solve
101      * scale/performance issues on several layers).
102      */
103     private ListenableFuture<Boolean> onNodeUpdated(final InstanceIdentifier<FlowCapableNode> nodePath,
104                                                     final FlowCapableNode dataBefore,
105                                                     final FlowCapableNode dataAfter) {
106         final SyncupEntry syncupEntry = new SyncupEntry(dataAfter, dsType(), dataBefore, dsType());
107         return reactor.syncup(nodePath, syncupEntry);
108     }
109
110     /**
111      * Remove values that are being deleted in the config from the switch.
112      * Note, this could be probably optimized using dedicated wipe-out RPC.
113      */
114     private ListenableFuture<Boolean> onNodeDeleted(final InstanceIdentifier<FlowCapableNode> nodePath,
115                                                     final FlowCapableNode dataBefore) {
116         final SyncupEntry syncupEntry = new SyncupEntry(null, dsType(), dataBefore, dsType());
117         return reactor.syncup(nodePath, syncupEntry);
118     }
119
120     @Override
121     public LogicalDatastoreType dsType() {
122         return LogicalDatastoreType.CONFIGURATION;
123     }
124 }