Wipe operational state
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / ServerSessionManager.java
index afc7172f89ee03f5af6a32431cdc4aec6eb8217a..eb25a305e4aed216aa9292a2b8049e8449dc248a 100644 (file)
@@ -15,8 +15,10 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
+import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeMXBean;
+import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistration;
+import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistrator;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
@@ -32,8 +34,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.topology.pcep.type.TopologyPcepBuilder;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
@@ -47,7 +47,7 @@ import org.slf4j.LoggerFactory;
 /**
  *
  */
-final class ServerSessionManager implements SessionListenerFactory<PCEPSessionListener>, AutoCloseable, TopologySessionRPCs {
+final class ServerSessionManager implements SessionListenerFactory<PCEPSessionListener>, AutoCloseable, TopologySessionRPCs, PCEPTopologyProviderRuntimeMXBean {
     private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
     private static final long DEFAULT_HOLD_STATE_NANOS = TimeUnit.MINUTES.toNanos(5);
 
@@ -56,6 +56,7 @@ final class ServerSessionManager implements SessionListenerFactory<PCEPSessionLi
     private final TopologySessionListenerFactory listenerFactory;
     private final InstanceIdentifier<Topology> topology;
     private final DataBroker broker;
+    private Optional<PCEPTopologyProviderRuntimeRegistration> runtimeRootRegistration = Optional.absent();
 
     public ServerSessionManager(final DataBroker broker, final InstanceIdentifier<Topology> topology,
             final TopologySessionListenerFactory listenerFactory) throws ReadFailedException, TransactionCommitFailedException {
@@ -63,21 +64,13 @@ final class ServerSessionManager implements SessionListenerFactory<PCEPSessionLi
         this.topology = Preconditions.checkNotNull(topology);
         this.listenerFactory = Preconditions.checkNotNull(listenerFactory);
 
-
-        // Make sure the topology does not exist
-        final ReadWriteTransaction tx = broker.newReadWriteTransaction();
-        final Optional<?> c = tx.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
-        Preconditions.checkArgument(!c.isPresent(), "Topology %s already exists", topology);
-
-        // create empty network-topology if not exists
-        tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(NetworkTopology.class).build(), new NetworkTopologyBuilder().build());
         // Now create the base topology
         final TopologyKey k = InstanceIdentifier.keyOf(topology);
+        final WriteTransaction tx = broker.newWriteOnlyTransaction();
         tx.put(LogicalDatastoreType.OPERATIONAL, topology, new TopologyBuilder().setKey(k).setTopologyId(k.getTopologyId()).setTopologyTypes(
                 new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
                         new TopologyTypes1Builder().setTopologyPcep(new TopologyPcepBuilder().build()).build()).build()).setNode(
-                                new ArrayList<Node>()).build());
-
+                                new ArrayList<Node>()).build(), true);
         tx.submit().checkedGet();
     }
 
@@ -115,56 +108,45 @@ final class ServerSessionManager implements SessionListenerFactory<PCEPSessionLi
         return this.listenerFactory.createTopologySessionListener(this);
     }
 
-    @Override
-    public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
+    private synchronized TopologySessionListener checkSessionPresence(final NodeId nodeId) {
         // Get the listener corresponding to the node
-        final TopologySessionListener l = this.nodes.get(input.getNode());
+        final TopologySessionListener l = this.nodes.get(nodeId);
         if (l == null) {
-            LOG.debug("Session for node {} not found", input.getNode());
-            return OperationResults.UNSENT.future();
+            LOG.debug("Session for node {} not found", nodeId);
+            return null;
         }
+        return l;
+    }
 
-        return l.addLsp(input);
+    @Override
+    public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
+        final TopologySessionListener l = checkSessionPresence(input.getNode());
+        return (l != null) ? l.addLsp(input) : OperationResults.UNSENT.future();
     }
 
     @Override
     public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
-        // Get the listener corresponding to the node
-        final TopologySessionListener l = this.nodes.get(input.getNode());
-        if (l == null) {
-            LOG.debug("Session for node {} not found", input.getNode());
-            return OperationResults.UNSENT.future();
-        }
-
-        return l.removeLsp(input);
+        final TopologySessionListener l = checkSessionPresence(input.getNode());
+        return (l != null) ? l.removeLsp(input) : OperationResults.UNSENT.future();
     }
 
     @Override
     public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
-        // Get the listener corresponding to the node
-        final TopologySessionListener l = this.nodes.get(input.getNode());
-        if (l == null) {
-            LOG.debug("Session for node {} not found", input.getNode());
-            return OperationResults.UNSENT.future();
-        }
-
-        return l.updateLsp(input);
+        final TopologySessionListener l = checkSessionPresence(input.getNode());
+        return (l != null) ? l.updateLsp(input) : OperationResults.UNSENT.future();
     }
 
     @Override
     public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
-        // Get the listener corresponding to the node
-        final TopologySessionListener l = this.nodes.get(input.getNode());
-        if (l == null) {
-            LOG.debug("Session for node {} not found", input.getNode());
-            return OperationResults.UNSENT.future();
-        }
-
-        return l.ensureLspOperational(input);
+        final TopologySessionListener l = checkSessionPresence(input.getNode());
+        return (l != null) ? l.ensureLspOperational(input) : OperationResults.UNSENT.future();
     }
 
     @Override
     public void close() throws TransactionCommitFailedException {
+        if (this.runtimeRootRegistration.isPresent()) {
+            this.runtimeRootRegistration.get().close();
+        }
         for (final TopologySessionListener sessionListener : this.nodes.values()) {
             sessionListener.close();
         }
@@ -175,4 +157,12 @@ final class ServerSessionManager implements SessionListenerFactory<PCEPSessionLi
         t.delete(LogicalDatastoreType.OPERATIONAL, this.topology);
         t.submit().checkedGet();
     }
+
+    public void registerRuntimeRootRegistartion(final PCEPTopologyProviderRuntimeRegistrator runtimeRootRegistrator) {
+        this.runtimeRootRegistration = Optional.of(runtimeRootRegistrator.register(this));
+    }
+
+    public Optional<PCEPTopologyProviderRuntimeRegistration> getRuntimeRootRegistration() {
+        return this.runtimeRootRegistration;
+    }
 }