Merge "Bug 5578 Improve frsync (threadpool)"
[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.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.openflowplugin.applications.notification.supplier.NotificationSupplierDefinition;
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
28 /**
29  * Public abstract basic Supplier implementation contains code for a make Supplier instance,
30  * registration Supplier like {@link org.opendaylight.controller.md.sal.binding.api.DataChangeListener}
31  * and close method. In additional case, it contains help methods for all Supplier implementations.
32  *
33  * @param <O> - data tree item Object extends {@link DataObject}
34  */
35 public abstract class AbstractNotificationSupplierBase<O extends DataObject> implements
36         NotificationSupplierDefinition<O> {
37
38     protected final Class<O> clazz;
39     private ListenerRegistration<DataTreeChangeListener> listenerRegistration;
40
41     /**
42      * Default constructor for all Notification Supplier implementation
43      *
44      * @param db    - {@link DataBroker}
45      * @param clazz - API contract class extended {@link DataObject}
46      */
47     public AbstractNotificationSupplierBase(final DataBroker db, final Class<O> clazz) {
48         Preconditions.checkArgument(db != null, "DataBroker can not be null!");
49         //TODO retries
50         listenerRegistration = db.registerDataTreeChangeListener( new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
51                 getWildCardPath()),this);
52         this.clazz = clazz;
53     }
54
55     @Override
56     public void close() throws Exception {
57         if (listenerRegistration != null) {
58             listenerRegistration.close();
59             listenerRegistration = null;
60         }
61     }
62
63     /**
64      * Method returns a wildCard {@link InstanceIdentifier} for {@link Node} from inventory
65      * because this path is a base for every OF paths.
66      *
67      * @return WildCarded InstanceIdentifier for Node
68      */
69     protected static InstanceIdentifier<Node> getNodeWildII() {
70         return InstanceIdentifier.create(Nodes.class).child(Node.class);
71     }
72
73     /**
74      * Method returns a keyed {@link InstanceIdentifier} for {@link Node} from inventory
75      * because this path is a base for every OF paths.
76      *
77      * @param ii - key for keyed {@link Node} {@link InstanceIdentifier}
78      * @return Keyed InstanceIdentifier for Node
79      */
80     protected static KeyedInstanceIdentifier<Node, NodeKey> getNodeII(final InstanceIdentifier<?> ii) {
81         final NodeKey key = ii.firstKeyOf(Node.class, NodeKey.class);
82         Preconditions.checkArgument(key != null);
83         return InstanceIdentifier.create(Nodes.class).child(Node.class, key);
84     }
85
86     /**
87      * @param path pointer to element
88      * @return extracted {@link NodeKey} and wrapped in {@link NodeRef}
89      */
90     public static NodeRef createNodeRef(InstanceIdentifier<?> path) {
91         final InstanceIdentifier<Node> nodePath = Preconditions.checkNotNull(path.firstIdentifierOf(Node.class));
92         return new NodeRef(nodePath);
93     }
94
95     /**
96      * @param path pointer to element
97      * @return extracted {@link NodeId}
98      */
99     public static NodeId getNodeId(InstanceIdentifier<?> path) {
100         final NodeKey nodeKey = Preconditions.checkNotNull(path.firstKeyOf(Node.class, NodeKey.class));
101         return nodeKey.getId();
102     }
103
104 }