2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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.unimgr.mef.netvirt;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.unimgr.api.UnimgrDataTreeChangeListener;
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.common.base.Optional;
24 @SuppressWarnings("rawtypes")
25 public class DataWaitListener<D extends DataObject> extends UnimgrDataTreeChangeListener<D> {
26 private static final Logger Log = LoggerFactory.getLogger(DataWaitListener.class);
27 InstanceIdentifier<D> objectIdentifierId;
28 private ListenerRegistration<DataWaitListener> dataWaitListenerRegistration;
29 Boolean dataAvailable = false;
30 private int maxRetries;
31 LogicalDatastoreType logicalDatastoreType;
32 DataWaitGetter<D> getData;
33 private final long waitMillisec = 1000;
35 public DataWaitListener(final DataBroker dataBroker, final InstanceIdentifier<D> objectIdentifierId,
36 int maxRetiries, LogicalDatastoreType logicalDatastoreType, final DataWaitGetter<D> getData) {
38 this.objectIdentifierId = objectIdentifierId;
39 this.maxRetries = maxRetiries;
40 this.logicalDatastoreType = logicalDatastoreType;
41 this.getData = getData;
45 @SuppressWarnings("unchecked")
46 public void registerListener() {
48 final DataTreeIdentifier<D> dataTreeIid = new DataTreeIdentifier<D>(logicalDatastoreType,
50 dataWaitListenerRegistration = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
51 Log.info("DataWaitListener created and registered");
52 } catch (final Exception e) {
53 Log.error("DataWaitListener DataChange listener registration failed !", e);
54 throw new IllegalStateException("DataWaitListener registration Listener failed.", e);
59 public void close() throws Exception {
60 dataWaitListenerRegistration.close();
64 public void add(DataTreeModification<D> newDataObject) {
65 if (newDataObject.getRootPath() != null && newDataObject.getRootNode() != null) {
66 Log.info("data {} created", newDataObject.getRootNode().getIdentifier());
68 synchronized (dataAvailable) {
69 dataAvailable.notifyAll();
74 public void remove(DataTreeModification<D> removedDataObject) {
78 public void update(DataTreeModification<D> modifiedDataObject) {
79 if (modifiedDataObject.getRootPath() != null && modifiedDataObject.getRootNode() != null) {
80 Log.info("data {} updated", modifiedDataObject.getRootNode().getIdentifier());
82 synchronized (dataAvailable) {
83 dataAvailable.notifyAll();
87 private boolean dataAvailable() {
88 if (getData() != null) {
94 public boolean waitForData() {
95 return waitForData(maxRetries);
98 public Object getData() {
99 Optional<D> objectInstance = MdsalUtils.read(dataBroker, logicalDatastoreType, objectIdentifierId);
100 if (!objectInstance.isPresent()) {
101 Log.debug("Data for {} doesn't exist, waiting more", objectIdentifierId);
104 return getData.get(objectInstance.get());
107 public boolean waitForData(int retry) {
108 synchronized (dataAvailable) {
109 dataAvailable = dataAvailable();
110 if (dataAvailable == true) {
112 } else if (retry <= 0) {
116 dataAvailable.wait(waitMillisec);
117 } catch (InterruptedException e1) {
120 return waitForData(--retry);