2 * Copyright (c) 2013 Cisco Systems, Inc. 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
8 package org.opendaylight.bgpcep.pcep.topology.provider;
10 import java.util.ArrayList;
11 import java.util.HashMap;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
17 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
18 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
19 import org.opendaylight.protocol.framework.SessionListenerFactory;
20 import org.opendaylight.protocol.pcep.PCEPSessionListener;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.topology.pcep.type.TopologyPcepBuilder;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.base.Preconditions;
42 import com.google.common.util.concurrent.FutureCallback;
43 import com.google.common.util.concurrent.Futures;
44 import com.google.common.util.concurrent.JdkFutureAdapters;
45 import com.google.common.util.concurrent.ListenableFuture;
50 final class ServerSessionManager implements SessionListenerFactory<PCEPSessionListener>, AutoCloseable {
51 private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
52 private static final long DEFAULT_HOLD_STATE_NANOS = TimeUnit.MINUTES.toNanos(5);
54 private final Map<NodeId, TopologySessionListener> nodes = new HashMap<>();
55 private final Map<NodeId, TopologyNodeState> state = new HashMap<>();
56 private final InstanceIdentifier<Topology> topology;
57 private final DataProviderService dataProvider;
59 public ServerSessionManager(final DataProviderService dataProvider, final InstanceIdentifier<Topology> topology) {
60 this.dataProvider = Preconditions.checkNotNull(dataProvider);
61 this.topology = Preconditions.checkNotNull(topology);
63 // Make sure the topology does not exist
64 final Object c = dataProvider.readOperationalData(topology);
65 Preconditions.checkArgument(c == null, "Topology %s already exists", topology);
67 // Now create the base topology
68 final TopologyKey k = InstanceIdentifier.keyOf(topology);
69 final DataModificationTransaction t = dataProvider.beginTransaction();
72 new TopologyBuilder().setKey(k).setTopologyId(k.getTopologyId()).setTopologyTypes(
73 new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
74 new TopologyTypes1Builder().setTopologyPcep(new TopologyPcepBuilder().build()).build()).build()).setNode(
75 new ArrayList<Node>()).build());
77 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(t.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
79 public void onSuccess(final RpcResult<TransactionStatus> result) {
80 LOG.trace("Topology {} created successfully", topology);
84 public void onFailure(final Throwable t) {
85 LOG.error("Failed to create topology {}", topology);
90 public void releaseNodeState(final TopologyNodeState nodeState) {
91 LOG.debug("Node {} unbound", nodeState.getNodeId());
92 this.nodes.remove(nodeState.getNodeId());
96 synchronized TopologyNodeState takeNodeState(final NodeId id, final TopologySessionListener sessionListener) {
97 LOG.debug("Node {} bound to listener {}", id, sessionListener);
99 TopologyNodeState ret = this.state.get(id);
101 ret = new TopologyNodeState(id, DEFAULT_HOLD_STATE_NANOS);
102 this.state.put(id, ret);
105 this.nodes.put(id, sessionListener);
111 public PCEPSessionListener getSessionListener() {
112 return new TopologySessionListener(this);
115 synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
116 // Get the listener corresponding to the node
117 final TopologySessionListener l = this.nodes.get(input.getNode());
119 LOG.debug("Session for node {} not found", input.getNode());
120 return OperationResults.UNSENT.future();
123 return l.addLsp(input);
126 synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
127 // Get the listener corresponding to the node
128 final TopologySessionListener l = this.nodes.get(input.getNode());
130 LOG.debug("Session for node {} not found", input.getNode());
131 return OperationResults.UNSENT.future();
134 return l.removeLsp(input);
137 synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
138 // Get the listener corresponding to the node
139 final TopologySessionListener l = this.nodes.get(input.getNode());
141 LOG.debug("Session for node {} not found", input.getNode());
142 return OperationResults.UNSENT.future();
145 return l.updateLsp(input);
148 synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
149 // Get the listener corresponding to the node
150 final TopologySessionListener l = this.nodes.get(input.getNode());
152 LOG.debug("Session for node {} not found", input.getNode());
153 return OperationResults.UNSENT.future();
156 return l.ensureLspOperational(input);
159 InstanceIdentifier<Topology> getTopology() {
163 DataModificationTransaction beginTransaction() {
164 return dataProvider.beginTransaction();
167 @SuppressWarnings("unchecked")
168 <T extends DataObject> T readOperationalData(final InstanceIdentifier<T> id) {
169 return (T)dataProvider.readOperationalData(id);
173 public void close() throws InterruptedException, ExecutionException {
174 final DataModificationTransaction t = this.dataProvider.beginTransaction();
175 t.removeOperationalData(this.topology);