2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.text.ParseException;
14 import java.text.SimpleDateFormat;
15 import java.util.Collection;
16 import java.util.Date;
17 import java.util.List;
18 import java.util.Objects;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
24 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
25 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
26 import org.opendaylight.openflowplugin.applications.frsync.impl.clustering.DeviceMastershipManager;
27 import org.opendaylight.openflowplugin.applications.frsync.util.ModificationUtil;
28 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
29 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
30 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableStatisticsGatheringStatus;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.snapshot.gathering.status.grouping.SnapshotGatheringStatusEnd;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Listens to operational changes and starts reconciliation through {@link SyncReactor} when necessary.
46 public class SimplifiedOperationalListener extends AbstractFrmSyncListener<Node> {
48 private static final Logger LOG = LoggerFactory.getLogger(SimplifiedOperationalListener.class);
49 public static final String DATE_AND_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
50 private final SyncReactor reactor;
51 private final FlowCapableNodeSnapshotDao operationalSnapshot;
52 private final FlowCapableNodeDao configDao;
53 private final ReconciliationRegistry reconciliationRegistry;
54 private final DeviceMastershipManager deviceMastershipManager;
56 public SimplifiedOperationalListener(final SyncReactor reactor,
57 final FlowCapableNodeSnapshotDao operationalSnapshot,
58 final FlowCapableNodeDao configDao,
59 final ReconciliationRegistry reconciliationRegistry,
60 final DeviceMastershipManager deviceMastershipManager) {
61 this.reactor = reactor;
62 this.operationalSnapshot = operationalSnapshot;
63 this.configDao = configDao;
64 this.reconciliationRegistry = reconciliationRegistry;
65 this.deviceMastershipManager = deviceMastershipManager;
69 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<Node>> modifications) {
70 super.onDataTreeChanged(modifications);
74 * Update cache, register for device mastership when device connected and start reconciliation if device
75 * is registered and actual modification is consistent.Skip the event otherwise.
78 protected Optional<ListenableFuture<Boolean>> processNodeModification(
79 final DataTreeModification<Node> modification) {
80 Optional<ListenableFuture<Boolean>> result;
81 final NodeId nodeId = ModificationUtil.nodeId(modification);
82 final DataObjectModification<Node> nodeModification = modification.getRootNode();
84 if (isDelete(nodeModification) || isDeleteLogical(nodeModification)) {
85 operationalSnapshot.updateCache(nodeId, Optional.absent());
86 deviceMastershipManager.onDeviceDisconnected(nodeId);
87 result = skipModification(modification);
89 operationalSnapshot.updateCache(nodeId, Optional.fromNullable(
90 ModificationUtil.flowCapableNodeAfter(modification)));
92 final boolean isAdd = isAdd(nodeModification) || isAddLogical(nodeModification);
95 deviceMastershipManager.onDeviceConnected(nodeId);
98 // if node is registered for reconcile we need consistent data from operational DS (skip partial
99 // collections) but we can accept first modification since all statistics are intentionally collected in
100 // one step on startup
101 if (reconciliationRegistry.isRegistered(nodeId) && (isAdd || isConsistentForReconcile(modification))) {
102 result = reconciliation(modification);
104 result = skipModification(modification);
110 private Optional<ListenableFuture<Boolean>> skipModification(final DataTreeModification<Node> modification) {
111 if (LOG.isTraceEnabled()) {
112 LOG.trace("Skipping operational modification: {}, before {}, after {}",
113 ModificationUtil.nodeIdValue(modification),
114 modification.getRootNode().getDataBefore() == null ? "null" : "nonnull",
115 modification.getRootNode().getDataAfter() == null ? "null" : "nonnull");
117 return Optional.absent();
120 private boolean isDelete(final DataObjectModification<Node> nodeModification) {
121 return Objects.nonNull(nodeModification.getDataBefore()) && Objects.isNull(nodeModification.getDataAfter());
125 * All connectors disappeared from operational store (logical delete).
127 private boolean isDeleteLogical(final DataObjectModification<Node> nodeModification) {
128 return !safeConnectorsEmpty(nodeModification.getDataBefore())
129 && safeConnectorsEmpty(nodeModification.getDataAfter());
133 private boolean isAdd(final DataObjectModification<Node> nodeModification) {
134 return Objects.isNull(nodeModification.getDataBefore()) && Objects.nonNull(nodeModification.getDataAfter());
138 * All connectors appeared in operational store (logical add).
140 private boolean isAddLogical(final DataObjectModification<Node> nodeModification) {
141 return safeConnectorsEmpty(nodeModification.getDataBefore())
142 && !safeConnectorsEmpty(nodeModification.getDataAfter());
146 * If node is present in config DS diff between wanted configuration (in config DS) and actual device
147 * configuration (coming from operational) should be calculated and sent to device.
148 * @param modification from DS
149 * @return optional syncup future
151 private Optional<ListenableFuture<Boolean>> reconciliation(final DataTreeModification<Node> modification) {
152 final NodeId nodeId = ModificationUtil.nodeId(modification);
153 final Optional<FlowCapableNode> nodeConfiguration = configDao.loadByNodeId(nodeId);
155 if (nodeConfiguration.isPresent()) {
156 LOG.debug("Reconciliation {}: {}", dsType(), nodeId.getValue());
157 final InstanceIdentifier<FlowCapableNode> nodePath = InstanceIdentifier.create(Nodes.class)
158 .child(Node.class, new NodeKey(ModificationUtil.nodeId(modification)))
159 .augmentation(FlowCapableNode.class);
160 final FlowCapableNode fcOperationalNode = ModificationUtil.flowCapableNodeAfter(modification);
161 final SyncupEntry syncupEntry = new SyncupEntry(nodeConfiguration.get(), LogicalDatastoreType.CONFIGURATION,
162 fcOperationalNode, dsType());
163 return Optional.of(reactor.syncup(nodePath, syncupEntry));
165 LOG.debug("Config not present for reconciliation: {}", nodeId.getValue());
166 reconciliationRegistry.unregisterIfRegistered(nodeId);
167 return skipModification(modification);
172 * Check if modification is consistent for reconciliation. We need fresh data, which means that current statistics
173 * were collected after registration for reconcile and whole bunch of statistics was collected successfully.
174 * @param modification from DS
175 * @return status of modification
177 private boolean isConsistentForReconcile(final DataTreeModification<Node> modification) {
178 final NodeId nodeId = PathUtil.digNodeId(modification.getRootPath().getRootIdentifier());
179 final FlowCapableStatisticsGatheringStatus gatheringStatus = modification.getRootNode().getDataAfter()
180 .getAugmentation(FlowCapableStatisticsGatheringStatus.class);
182 if (gatheringStatus == null) {
183 LOG.trace("Statistics gathering never started: {}", nodeId.getValue());
187 final SnapshotGatheringStatusEnd gatheringStatusEnd = gatheringStatus.getSnapshotGatheringStatusEnd();
189 if (gatheringStatusEnd == null) {
190 LOG.trace("Statistics gathering is not over yet: {}", nodeId.getValue());
194 if (!gatheringStatusEnd.isSucceeded()) {
195 LOG.trace("Statistics gathering was not successful: {}", nodeId.getValue());
200 Date timestampOfRegistration = reconciliationRegistry.getRegistrationTimestamp(nodeId);
201 final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
202 Date timestampOfStatistics = simpleDateFormat.parse(gatheringStatusEnd.getEnd().getValue());
203 if (timestampOfStatistics.after(timestampOfRegistration)) {
204 LOG.debug("Fresh operational present: {}", nodeId.getValue());
207 } catch (ParseException e) {
208 LOG.warn("Timestamp parsing error {}", e);
210 LOG.debug("Fresh operational not present: {}", nodeId.getValue());
214 private static boolean safeConnectorsEmpty(final Node node) {
218 final List<NodeConnector> nodeConnectors = node.getNodeConnector();
219 return nodeConnectors == null || nodeConnectors.isEmpty();
223 public LogicalDatastoreType dsType() {
224 return LogicalDatastoreType.OPERATIONAL;