2 * Copyright (c) 2015 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.notification.supplier.impl;
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.mdsal.binding.api.DataBroker;
12 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
13 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.openflowplugin.applications.notification.supplier.NotificationSupplierDefinition;
16 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Public abstract basic Supplier implementation contains code for a make Supplier instance,
31 * registration Supplier like {@link DataTreeChangeListener}
32 * and close method. In additional case, it contains help methods for all Supplier implementations.
34 * @param <O> - data tree item Object extends {@link DataObject}
36 public abstract class AbstractNotificationSupplierBase<O extends DataObject> implements
37 NotificationSupplierDefinition<O> {
39 private static final Logger LOG = LoggerFactory.getLogger(AbstractNotificationSupplierBase.class);
41 protected final Class<O> clazz;
42 private ListenerRegistration<DataTreeChangeListener<O>> listenerRegistration;
43 private static final int STARTUP_LOOP_TICK = 500;
44 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
47 final DataTreeIdentifier<O> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
50 * Default constructor for all Notification Supplier implementation.
52 * @param db - {@link DataBroker}
53 * @param clazz - API contract class extended {@link DataObject}
55 @SuppressWarnings("IllegalCatch")
56 public AbstractNotificationSupplierBase(final DataBroker db, final Class<O> clazz) {
57 Preconditions.checkArgument(db != null, "DataBroker can not be null!");
60 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
62 listenerRegistration = looper
63 .loopUntilNoException(
64 () -> db.registerDataTreeChangeListener(treeId, AbstractNotificationSupplierBase.this));
65 } catch (final Exception ex) {
66 LOG.debug("AbstractNotificationSupplierBase DataTreeChange listener registration fail ..{}",
68 throw new IllegalStateException("Notification supplier startup fail! System needs restart.", ex);
73 public void close() throws Exception {
74 if (listenerRegistration != null) {
75 listenerRegistration.close();
76 listenerRegistration = null;
81 * Method returns a wildCard {@link InstanceIdentifier} for {@link Node} from inventory
82 * because this path is a base for every OF paths.
84 * @return WildCarded InstanceIdentifier for Node
86 protected static InstanceIdentifier<Node> getNodeWildII() {
87 return InstanceIdentifier.create(Nodes.class).child(Node.class);
91 * Method returns a keyed {@link InstanceIdentifier} for {@link Node} from inventory
92 * because this path is a base for every OF paths.
94 * @param ii - key for keyed {@link Node} {@link InstanceIdentifier}
95 * @return Keyed InstanceIdentifier for Node
97 protected static KeyedInstanceIdentifier<Node, NodeKey> getNodeII(final InstanceIdentifier<?> ii) {
98 final NodeKey key = ii.firstKeyOf(Node.class);
99 Preconditions.checkArgument(key != null);
100 return InstanceIdentifier.create(Nodes.class).child(Node.class, key);
104 * Create a node reference.
106 * @param path pointer to element
107 * @return extracted {@link NodeKey} and wrapped in {@link NodeRef}
109 public static NodeRef createNodeRef(InstanceIdentifier<?> path) {
110 final InstanceIdentifier<Node> nodePath = Preconditions.checkNotNull(path.firstIdentifierOf(Node.class));
111 return new NodeRef(nodePath);
115 * Get the node identifier.
117 * @param path pointer to element
118 * @return extracted {@link NodeId}
120 public static NodeId getNodeId(InstanceIdentifier<?> path) {
121 final NodeKey nodeKey = Preconditions.checkNotNull(path.firstKeyOf(Node.class));
122 return nodeKey.getId();