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