Convert ListenerRegistrationHelper into a component
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.util.Collection;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.DataObjectModification;
19 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.mdsal.binding.api.DataTreeModification;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
23 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
24 import org.opendaylight.openflowplugin.applications.frm.NodeConfigurator;
25 import org.opendaylight.serviceutils.srm.RecoverableListener;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yangtools.concepts.ObjectRegistration;
28 import org.opendaylight.yangtools.concepts.Registration;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * AbstractChangeListner implemented basic {@link org.opendaylight.mdsal.binding.api.DataTreeModification}
36  * processing for flow node subDataObject (flows, groups and meters).
37  */
38 public abstract class AbstractListeningCommiter<T extends DataObject>
39         implements ForwardingRulesCommiter<T>, RecoverableListener {
40     private static final Logger LOG = LoggerFactory.getLogger(AbstractListeningCommiter.class);
41
42     final ForwardingRulesManager provider;
43     NodeConfigurator nodeConfigurator;
44     protected final DataBroker dataBroker;
45     private final ListenerRegistrationHelper registrationHelper;
46
47     private Registration listenerRegistration;
48
49     @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "See FIXME below")
50     protected AbstractListeningCommiter(final ForwardingRulesManager provider, final DataBroker dataBroker,
51                                      final ListenerRegistrationHelper registrationHelper) {
52         this.provider = requireNonNull(provider, "ForwardingRulesManager can not be null!");
53         nodeConfigurator = requireNonNull(provider.getNodeConfigurator(), "NodeConfigurator can not be null!");
54         this.dataBroker = requireNonNull(dataBroker, "DataBroker can not be null!");
55         this.registrationHelper = requireNonNull(registrationHelper, "registrationHelper can not be null!");
56
57         // FIXME: this may start listening on an uninitialized object: clean up the lifecycle here
58         registerListener();
59         provider.addRecoverableListener(this);
60     }
61
62     @SuppressWarnings("checkstyle:IllegalCatch")
63     @Override
64     public void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
65         LOG.trace("Received data changes :{}", requireNonNull(changes, "Changes may not be null!"));
66
67         for (DataTreeModification<T> change : changes) {
68             final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
69             final DataObjectModification<T> mod = change.getRootNode();
70             final InstanceIdentifier<FlowCapableNode> nodeIdent =
71                     key.firstIdentifierOf(FlowCapableNode.class);
72             try {
73                 if (preConfigurationCheck(nodeIdent)) {
74                     switch (mod.getModificationType()) {
75                         case DELETE:
76                             remove(key, mod.getDataBefore(), nodeIdent);
77                             break;
78                         case SUBTREE_MODIFIED:
79                             update(key, mod.getDataBefore(), mod.getDataAfter(), nodeIdent);
80                             break;
81                         case WRITE:
82                             if (mod.getDataBefore() == null) {
83                                 add(key, mod.getDataAfter(), nodeIdent);
84                             } else {
85                                 update(key, mod.getDataBefore(), mod.getDataAfter(), nodeIdent);
86                             }
87                             break;
88                         default:
89                             throw new IllegalArgumentException(
90                                 "Unhandled modification type " + mod.getModificationType());
91                     }
92                 } else if (provider.isStaleMarkingEnabled()) {
93                     LOG.info("Stale-Marking ENABLED and switch {} is NOT connected, storing stale entities", nodeIdent);
94                     // Switch is NOT connected
95                     switch (mod.getModificationType()) {
96                         case DELETE:
97                             createStaleMarkEntity(key, mod.getDataBefore(), nodeIdent);
98                             break;
99                         case SUBTREE_MODIFIED:
100                         case WRITE:
101                             break;
102                         default:
103                             throw new IllegalArgumentException(
104                                 "Unhandled modification type " + mod.getModificationType());
105                     }
106                 }
107             } catch (RuntimeException e) {
108                 LOG.error("Failed to handle event {} key {} due to error ", mod.getModificationType(), key, e);
109             }
110         }
111     }
112
113     @Override
114     public final void registerListener() {
115         Futures.addCallback(registrationHelper.checkedRegisterListener(
116             DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, getWildCardPath()), this),
117             new FutureCallback<>() {
118                 @Override
119                 public void onSuccess(final ObjectRegistration<?> flowListenerRegistration) {
120                     LOG.info("{} registered successfully", flowListenerRegistration.getInstance());
121                     listenerRegistration = flowListenerRegistration;
122                 }
123
124                 @Override
125                 public void onFailure(final Throwable throwable) {
126                     LOG.error("Registration failed ", throwable);
127                 }
128             }, MoreExecutors.directExecutor());
129     }
130
131     @Override
132     public void deregisterListener() {
133         close();
134     }
135
136     @Override
137     public void close() {
138         if (listenerRegistration != null) {
139             listenerRegistration.close();
140             listenerRegistration = null;
141         }
142     }
143
144     /**
145      * Method return wildCardPath for Listener registration
146      * and for identify the correct KeyInstanceIdentifier from data.
147      */
148     protected abstract InstanceIdentifier<T> getWildCardPath();
149
150     private boolean preConfigurationCheck(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
151         requireNonNull(nodeIdent, "FlowCapableNode identifier can not be null!");
152         // In single node cluster, node should be in local cache before we get any flow/group/meter
153         // data change event from data store. So first check should pass.
154         // In case of 3-node cluster, when shard leader changes, clustering will send blob of data
155         // present in operational data store and config data store. So ideally local node cache
156         // should get populated. But to handle a scenario where flow request comes before the blob
157         // of config/operational data gets processes, it won't find node in local cache and it will
158         // skip the flow/group/meter operational. This requires an addition check, where it reads
159         // node from operational data store and if it's present it calls flowNodeConnected to explicitly
160         // trigger the event of new node connected.
161         return provider.isNodeOwner(nodeIdent);
162     }
163 }