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
9 package org.opendaylight.openflowplugin.applications.notification.supplier.impl;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.Callable;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.openflowplugin.applications.notification.supplier.NotificationSupplierDefinition;
18 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Public abstract basic Supplier implementation contains code for a make Supplier instance,
33 * registration Supplier like {@link org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener}
34 * and close method. In additional case, it contains help methods for all Supplier implementations.
36 * @param <O> - data tree item Object extends {@link DataObject}
38 public abstract class AbstractNotificationSupplierBase<O extends DataObject> implements
39 NotificationSupplierDefinition<O> {
41 private static final Logger LOG = LoggerFactory.getLogger(AbstractNotificationSupplierBase.class);
43 protected final Class<O> clazz;
44 private ListenerRegistration<DataTreeChangeListener<O>> listenerRegistration;
45 private static final int STARTUP_LOOP_TICK = 500;
46 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
49 final DataTreeIdentifier<O> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
52 * Default constructor for all Notification Supplier implementation.
54 * @param db - {@link DataBroker}
55 * @param clazz - API contract class extended {@link DataObject}
57 @SuppressWarnings("IllegalCatch")
58 public AbstractNotificationSupplierBase(final DataBroker db, final Class<O> clazz) {
59 Preconditions.checkArgument(db != null, "DataBroker can not be null!");
62 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
64 listenerRegistration = looper
65 .loopUntilNoException(new Callable<ListenerRegistration<DataTreeChangeListener<O>>>() {
67 public ListenerRegistration<DataTreeChangeListener<O>> call() throws Exception {
68 return db.registerDataTreeChangeListener(treeId, AbstractNotificationSupplierBase.this);
71 } catch (final Exception ex) {
72 LOG.debug("AbstractNotificationSupplierBase DataTreeChange listener registration fail ..{}",
74 throw new IllegalStateException("Notification supplier startup fail! System needs restart.", ex);
79 public void close() throws Exception {
80 if (listenerRegistration != null) {
81 listenerRegistration.close();
82 listenerRegistration = null;
87 * Method returns a wildCard {@link InstanceIdentifier} for {@link Node} from inventory
88 * because this path is a base for every OF paths.
90 * @return WildCarded InstanceIdentifier for Node
92 protected static InstanceIdentifier<Node> getNodeWildII() {
93 return InstanceIdentifier.create(Nodes.class).child(Node.class);
97 * Method returns a keyed {@link InstanceIdentifier} for {@link Node} from inventory
98 * because this path is a base for every OF paths.
100 * @param ii - key for keyed {@link Node} {@link InstanceIdentifier}
101 * @return Keyed InstanceIdentifier for Node
103 protected static KeyedInstanceIdentifier<Node, NodeKey> getNodeII(final InstanceIdentifier<?> ii) {
104 final NodeKey key = ii.firstKeyOf(Node.class);
105 Preconditions.checkArgument(key != null);
106 return InstanceIdentifier.create(Nodes.class).child(Node.class, key);
110 * Create a node reference.
112 * @param path pointer to element
113 * @return extracted {@link NodeKey} and wrapped in {@link NodeRef}
115 public static NodeRef createNodeRef(InstanceIdentifier<?> path) {
116 final InstanceIdentifier<Node> nodePath = Preconditions.checkNotNull(path.firstIdentifierOf(Node.class));
117 return new NodeRef(nodePath);
121 * Get the node identifier.
123 * @param path pointer to element
124 * @return extracted {@link NodeId}
126 public static NodeId getNodeId(InstanceIdentifier<?> path) {
127 final NodeKey nodeKey = Preconditions.checkNotNull(path.firstKeyOf(Node.class));
128 return nodeKey.getId();