Break ServerSessionManager into multiple components
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / ServerSessionManager.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.bgpcep.pcep.topology.provider;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15
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;
40
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;
46
47 /**
48  *
49  */
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);
53
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;
58
59         public ServerSessionManager(final DataProviderService dataProvider, final InstanceIdentifier<Topology> topology) {
60                 this.dataProvider = Preconditions.checkNotNull(dataProvider);
61                 this.topology = Preconditions.checkNotNull(topology);
62
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);
66
67                 // Now create the base topology
68                 final TopologyKey k = InstanceIdentifier.keyOf(topology);
69                 final DataModificationTransaction t = dataProvider.beginTransaction();
70                 t.putOperationalData(
71                                 topology,
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());
76
77                 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(t.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
78                         @Override
79                         public void onSuccess(final RpcResult<TransactionStatus> result) {
80                                 LOG.trace("Topology {} created successfully", topology);
81                         }
82
83                         @Override
84                         public void onFailure(final Throwable t) {
85                                 LOG.error("Failed to create topology {}", topology);
86                         }
87                 });
88         }
89
90         public void releaseNodeState(final TopologyNodeState nodeState) {
91                 LOG.debug("Node {} unbound", nodeState.getNodeId());
92                 this.nodes.remove(nodeState.getNodeId());
93                 nodeState.released();
94         }
95
96         synchronized TopologyNodeState takeNodeState(final NodeId id, final TopologySessionListener sessionListener) {
97                 LOG.debug("Node {} bound to listener {}", id, sessionListener);
98
99                 TopologyNodeState ret = this.state.get(id);
100                 if (ret == null) {
101                         ret = new TopologyNodeState(id, DEFAULT_HOLD_STATE_NANOS);
102                         this.state.put(id, ret);
103                 }
104
105                 this.nodes.put(id, sessionListener);
106                 ret.taken();
107                 return ret;
108         }
109
110         @Override
111         public PCEPSessionListener getSessionListener() {
112                 return new TopologySessionListener(this);
113         }
114
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());
118                 if (l == null) {
119                         LOG.debug("Session for node {} not found", input.getNode());
120                         return OperationResults.UNSENT.future();
121                 }
122
123                 return l.addLsp(input);
124         }
125
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());
129                 if (l == null) {
130                         LOG.debug("Session for node {} not found", input.getNode());
131                         return OperationResults.UNSENT.future();
132                 }
133
134                 return l.removeLsp(input);
135         }
136
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());
140                 if (l == null) {
141                         LOG.debug("Session for node {} not found", input.getNode());
142                         return OperationResults.UNSENT.future();
143                 }
144
145                 return l.updateLsp(input);
146         }
147
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());
151                 if (l == null) {
152                         LOG.debug("Session for node {} not found", input.getNode());
153                         return OperationResults.UNSENT.future();
154                 }
155
156                 return l.ensureLspOperational(input);
157         }
158
159         InstanceIdentifier<Topology> getTopology() {
160                 return topology;
161         }
162
163         DataModificationTransaction beginTransaction() {
164                 return dataProvider.beginTransaction();
165         }
166
167         @SuppressWarnings("unchecked")
168         <T extends DataObject> T readOperationalData(final InstanceIdentifier<T> id) {
169                 return (T)dataProvider.readOperationalData(id);
170         }
171
172         @Override
173         public void close() throws InterruptedException, ExecutionException {
174                 final DataModificationTransaction t = this.dataProvider.beginTransaction();
175                 t.removeOperationalData(this.topology);
176                 t.commit().get();
177         }
178 }