itm and mdsal porting
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / impl / ITMManager.java
index 87f8a8b1960053098b97466cda4df65ae808129c..0c784b08556b12ae96973e3faa6a52e0cdca3397 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -37,7 +37,10 @@ import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
 
 import com.google.common.base.Optional;
-
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.TunnelMonitorEnabled;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.TunnelMonitorInterval;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.TunnelMonitorEnabledBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.TunnelMonitorIntervalBuilder;
 
 public class ITMManager implements AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(ITMManager.class);
@@ -64,5 +67,56 @@ public class ITMManager implements AutoCloseable {
     public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
         this.notificationPublishService = notificationPublishService;
     }
+    protected void initTunnelMonitorDataInConfigDS() {
+        new Thread() {
+            public void run() {
+                boolean readSucceeded = false;
+                InstanceIdentifier<TunnelMonitorEnabled> monitorPath = InstanceIdentifier.builder(TunnelMonitorEnabled.class).build();
+                while (!readSucceeded) {
+                    try {
+                        Optional<TunnelMonitorEnabled> storedTunnelMonitor = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, monitorPath, broker);
+                        // Store default values only when tunnel monitor data is not initialized
+                        if (!storedTunnelMonitor.isPresent()) {
+                            TunnelMonitorEnabled monitorEnabled =
+                                    new TunnelMonitorEnabledBuilder().setEnabled(ITMConstants.DEFAULT_MONITOR_ENABLED).build();
+                            ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, monitorPath, monitorEnabled, broker, ItmUtils.DEFAULT_CALLBACK);
+
+                            InstanceIdentifier<TunnelMonitorInterval> intervalPath = InstanceIdentifier.builder(TunnelMonitorInterval.class).build();
+                            TunnelMonitorInterval monitorInteval =
+                                    new TunnelMonitorIntervalBuilder().setInterval(ITMConstants.DEFAULT_MONITOR_INTERVAL).build();
+                            ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, intervalPath, monitorInteval, broker, ItmUtils.DEFAULT_CALLBACK);
+                        }
+                        readSucceeded = true;
+                    } catch (Exception e) {
+                        LOG.warn("Unable to read monitor enabled info; retrying after some delay");
+                        try {
+                            Thread.sleep(1000);
+                        } catch (InterruptedException ie) {
+                            return;
+                        }
+                    }
+                }
+            }
+        }.start();
+    }
 
+    protected boolean getTunnelMonitorEnabledFromConfigDS() {
+        boolean tunnelMonitorEnabled = true;
+        InstanceIdentifier<TunnelMonitorEnabled> path = InstanceIdentifier.builder(TunnelMonitorEnabled.class).build();
+        Optional<TunnelMonitorEnabled> storedTunnelMonitor = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
+        if (storedTunnelMonitor.isPresent()) {
+            tunnelMonitorEnabled = storedTunnelMonitor.get().isEnabled();
+        }
+        return tunnelMonitorEnabled;
+    }
+
+    protected int getTunnelMonitorIntervalFromConfigDS() {
+        int tunnelMonitorInterval = ITMConstants.DEFAULT_MONITOR_INTERVAL;
+        InstanceIdentifier<TunnelMonitorInterval> path = InstanceIdentifier.builder(TunnelMonitorInterval.class).build();
+        Optional<TunnelMonitorInterval> storedTunnelMonitor = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
+        if (storedTunnelMonitor.isPresent()) {
+            tunnelMonitorInterval = storedTunnelMonitor.get().getInterval();
+        }
+        return tunnelMonitorInterval;
+    }
 }