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