Switch to MD-SAL APIs
[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 package org.opendaylight.openflowplugin.applications.notification.supplier.impl;
9
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;
28
29 /**
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.
33  *
34  * @param <O> - data tree item Object extends {@link DataObject}
35  */
36 public abstract class AbstractNotificationSupplierBase<O extends DataObject> implements
37         NotificationSupplierDefinition<O> {
38
39     private static final Logger LOG = LoggerFactory.getLogger(AbstractNotificationSupplierBase.class);
40
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;
45
46
47     final DataTreeIdentifier<O> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
48
49     /**
50      * Default constructor for all Notification Supplier implementation.
51      *
52      * @param db    - {@link DataBroker}
53      * @param clazz - API contract class extended {@link DataObject}
54      */
55     @SuppressWarnings("IllegalCatch")
56     public AbstractNotificationSupplierBase(final DataBroker db, final Class<O> clazz) {
57         Preconditions.checkArgument(db != null, "DataBroker can not be null!");
58         this.clazz = clazz;
59
60         SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
61         try {
62             listenerRegistration = looper
63                     .loopUntilNoException(
64                         () -> db.registerDataTreeChangeListener(treeId, AbstractNotificationSupplierBase.this));
65         } catch (final Exception ex) {
66             LOG.debug("AbstractNotificationSupplierBase DataTreeChange listener registration fail ..{}",
67                       ex.getMessage());
68             throw new IllegalStateException("Notification supplier startup fail! System needs restart.", ex);
69         }
70     }
71
72     @Override
73     public void close() throws Exception {
74         if (listenerRegistration != null) {
75             listenerRegistration.close();
76             listenerRegistration = null;
77         }
78     }
79
80     /**
81      * Method returns a wildCard {@link InstanceIdentifier} for {@link Node} from inventory
82      * because this path is a base for every OF paths.
83      *
84      * @return WildCarded InstanceIdentifier for Node
85      */
86     protected static InstanceIdentifier<Node> getNodeWildII() {
87         return InstanceIdentifier.create(Nodes.class).child(Node.class);
88     }
89
90     /**
91      * Method returns a keyed {@link InstanceIdentifier} for {@link Node} from inventory
92      * because this path is a base for every OF paths.
93      *
94      * @param ii - key for keyed {@link Node} {@link InstanceIdentifier}
95      * @return Keyed InstanceIdentifier for Node
96      */
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);
101     }
102
103     /**
104      * Create a node reference.
105      *
106      * @param path pointer to element
107      * @return extracted {@link NodeKey} and wrapped in {@link NodeRef}
108      */
109     public static NodeRef createNodeRef(InstanceIdentifier<?> path) {
110         final InstanceIdentifier<Node> nodePath = Preconditions.checkNotNull(path.firstIdentifierOf(Node.class));
111         return new NodeRef(nodePath);
112     }
113
114     /**
115      * Get the node identifier.
116      *
117      * @param path pointer to element
118      * @return extracted {@link NodeId}
119      */
120     public static NodeId getNodeId(InstanceIdentifier<?> path) {
121         final NodeKey nodeKey = Preconditions.checkNotNull(path.firstKeyOf(Node.class));
122         return nodeKey.getId();
123     }
124 }