2 * Copyright (c) 2014, 2017 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
8 package org.opendaylight.openflowplugin.applications.frm.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.Collection;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.DataObjectModification;
18 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
22 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
23 import org.opendaylight.openflowplugin.applications.frm.NodeConfigurator;
24 import org.opendaylight.serviceutils.srm.RecoverableListener;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * AbstractChangeListner implemented basic {@link org.opendaylight.mdsal.binding.api.DataTreeModification}
34 * processing for flow node subDataObject (flows, groups and meters).
36 public abstract class AbstractListeningCommiter<T extends DataObject>
37 implements ForwardingRulesCommiter<T>, RecoverableListener {
39 private static final Logger LOG = LoggerFactory.getLogger(AbstractListeningCommiter.class);
40 final ForwardingRulesManager provider;
41 NodeConfigurator nodeConfigurator;
42 protected final DataBroker dataBroker;
43 protected final ListenerRegistrationHelper registrationHelper;
44 protected ListenerRegistration<AbstractListeningCommiter> listenerRegistration;
46 public AbstractListeningCommiter(final ForwardingRulesManager provider, final DataBroker dataBroker,
47 final ListenerRegistrationHelper registrationHelper) {
48 this.provider = Preconditions.checkNotNull(provider, "ForwardingRulesManager can not be null!");
49 this.nodeConfigurator = Preconditions.checkNotNull(provider.getNodeConfigurator(),
50 "NodeConfigurator can not be null!");
51 this.dataBroker = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
52 this.registrationHelper = Preconditions.checkNotNull(registrationHelper, "registrationHelper can not be null!");
54 provider.addRecoverableListener(this);
57 @SuppressWarnings("checkstyle:IllegalCatch")
59 public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
60 Preconditions.checkNotNull(changes, "Changes may not be null!");
61 LOG.trace("Received data changes :{}", changes);
63 for (DataTreeModification<T> change : changes) {
64 final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
65 final DataObjectModification<T> mod = change.getRootNode();
66 final InstanceIdentifier<FlowCapableNode> nodeIdent =
67 key.firstIdentifierOf(FlowCapableNode.class);
69 if (preConfigurationCheck(nodeIdent)) {
70 switch (mod.getModificationType()) {
72 remove(key, mod.getDataBefore(), nodeIdent);
74 case SUBTREE_MODIFIED:
75 update(key, mod.getDataBefore(), mod.getDataAfter(), nodeIdent);
78 if (mod.getDataBefore() == null) {
79 add(key, mod.getDataAfter(), nodeIdent);
81 update(key, mod.getDataBefore(), mod.getDataAfter(), nodeIdent);
86 IllegalArgumentException("Unhandled modification type "
87 + mod.getModificationType());
90 if (provider.isStaleMarkingEnabled()) {
91 LOG.info("Stale-Marking ENABLED and switch {} is NOT connected, storing stale entities",
92 nodeIdent.toString());
93 // Switch is NOT connected
94 switch (mod.getModificationType()) {
96 createStaleMarkEntity(key, mod.getDataBefore(), nodeIdent);
98 case SUBTREE_MODIFIED:
104 IllegalArgumentException("Unhandled modification type "
105 + mod.getModificationType());
109 } catch (RuntimeException e) {
110 LOG.error("Failed to handle event {} key {} due to error ", mod.getModificationType(), key, e);
116 public void registerListener() {
117 final DataTreeIdentifier<T> treeId =
118 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
119 Futures.addCallback(registrationHelper.checkedRegisterListener(treeId, this),
120 new FutureCallback<ListenerRegistration<AbstractListeningCommiter>>() {
122 public void onSuccess(
123 @Nullable ListenerRegistration<AbstractListeningCommiter> flowListenerRegistration) {
124 LOG.info("{} registered successfully", flowListenerRegistration.getInstance());
125 listenerRegistration = flowListenerRegistration;
129 public void onFailure(Throwable throwable) {
130 LOG.error("Registration failed ", throwable);
132 }, MoreExecutors.directExecutor());
136 * Method return wildCardPath for Listener registration
137 * and for identify the correct KeyInstanceIdentifier from data.
139 protected abstract InstanceIdentifier<T> getWildCardPath();
141 private boolean preConfigurationCheck(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
142 Preconditions.checkNotNull(nodeIdent, "FlowCapableNode identifier can not be null!");
143 // In single node cluster, node should be in local cache before we get any flow/group/meter
144 // data change event from data store. So first check should pass.
145 // In case of 3-node cluster, when shard leader changes, clustering will send blob of data
146 // present in operational data store and config data store. So ideally local node cache
147 // should get populated. But to handle a scenario where flow request comes before the blob
148 // of config/operational data gets processes, it won't find node in local cache and it will
149 // skip the flow/group/meter operational. This requires an addition check, where it reads
150 // node from operational data store and if it's present it calls flowNodeConnected to explicitly
151 // trigger the event of new node connected.
152 return provider.isNodeOwner(nodeIdent);