Bug-2081: PCEP statistics
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.net.InetAddress;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeMXBean;
19 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistration;
20 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistrator;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
23 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.protocol.framework.SessionListenerFactory;
28 import org.opendaylight.protocol.pcep.PCEPSession;
29 import org.opendaylight.protocol.pcep.PCEPSessionListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1Builder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.topology.pcep.type.TopologyPcepBuilder;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  *
52  */
53 final class ServerSessionManager implements SessionListenerFactory<PCEPSessionListener>, AutoCloseable, TopologySessionRPCs, PCEPTopologyProviderRuntimeMXBean {
54     private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
55     private static final long DEFAULT_HOLD_STATE_NANOS = TimeUnit.MINUTES.toNanos(5);
56
57     private final Map<NodeId, TopologySessionListener> nodes = new HashMap<>();
58     private final Map<NodeId, TopologyNodeState> state = new HashMap<>();
59     private final TopologySessionListenerFactory listenerFactory;
60     private final InstanceIdentifier<Topology> topology;
61     private final DataBroker broker;
62     private Optional<PCEPTopologyProviderRuntimeRegistration> runtimeRootRegistration = Optional.absent();
63
64     public ServerSessionManager(final DataBroker broker, final InstanceIdentifier<Topology> topology,
65             final TopologySessionListenerFactory listenerFactory) throws ReadFailedException, TransactionCommitFailedException {
66         this.broker = Preconditions.checkNotNull(broker);
67         this.topology = Preconditions.checkNotNull(topology);
68         this.listenerFactory = Preconditions.checkNotNull(listenerFactory);
69
70
71         // Make sure the topology does not exist
72         final ReadWriteTransaction tx = broker.newReadWriteTransaction();
73         final Optional<?> c = tx.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
74         Preconditions.checkArgument(!c.isPresent(), "Topology %s already exists", topology);
75
76         // create empty network-topology if not exists
77         tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(NetworkTopology.class).build(), new NetworkTopologyBuilder().build());
78         // Now create the base topology
79         final TopologyKey k = InstanceIdentifier.keyOf(topology);
80         tx.put(LogicalDatastoreType.OPERATIONAL, topology, new TopologyBuilder().setKey(k).setTopologyId(k.getTopologyId()).setTopologyTypes(
81                 new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
82                         new TopologyTypes1Builder().setTopologyPcep(new TopologyPcepBuilder().build()).build()).build()).setNode(
83                                 new ArrayList<Node>()).build());
84
85         tx.submit().checkedGet();
86     }
87
88     private static NodeId createNodeId(final InetAddress addr) {
89         return new NodeId("pcc://" + addr.getHostAddress());
90     }
91
92     synchronized void releaseNodeState(final TopologyNodeState nodeState, final PCEPSession session) {
93         LOG.debug("Node {} unbound", nodeState.getNodeId());
94         this.nodes.remove(createNodeId(session.getRemoteAddress()));
95         nodeState.released();
96     }
97
98     synchronized TopologyNodeState takeNodeState(final InetAddress address, final TopologySessionListener sessionListener) {
99         final NodeId id = createNodeId(address);
100
101         LOG.debug("Node {} requested by listener {}", id, sessionListener);
102         TopologyNodeState ret = this.state.get(id);
103
104         if (ret == null) {
105             ret = new TopologyNodeState(this.broker, this.topology, id, DEFAULT_HOLD_STATE_NANOS);
106             LOG.debug("Created topology node {} for id {} at {}", ret, id, ret.getNodeId());
107             this.state.put(id, ret);
108         }
109         // FIXME: else check for conflicting session
110
111         ret.taken();
112         this.nodes.put(id, sessionListener);
113         LOG.debug("Node {} bound to listener {}", id, sessionListener);
114         return ret;
115     }
116
117     @Override
118     public PCEPSessionListener getSessionListener() {
119         return this.listenerFactory.createTopologySessionListener(this);
120     }
121
122     @Override
123     public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
124         // Get the listener corresponding to the node
125         final TopologySessionListener l = this.nodes.get(input.getNode());
126         if (l == null) {
127             LOG.debug("Session for node {} not found", input.getNode());
128             return OperationResults.UNSENT.future();
129         }
130
131         return l.addLsp(input);
132     }
133
134     @Override
135     public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
136         // Get the listener corresponding to the node
137         final TopologySessionListener l = this.nodes.get(input.getNode());
138         if (l == null) {
139             LOG.debug("Session for node {} not found", input.getNode());
140             return OperationResults.UNSENT.future();
141         }
142
143         return l.removeLsp(input);
144     }
145
146     @Override
147     public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
148         // Get the listener corresponding to the node
149         final TopologySessionListener l = this.nodes.get(input.getNode());
150         if (l == null) {
151             LOG.debug("Session for node {} not found", input.getNode());
152             return OperationResults.UNSENT.future();
153         }
154
155         return l.updateLsp(input);
156     }
157
158     @Override
159     public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
160         // Get the listener corresponding to the node
161         final TopologySessionListener l = this.nodes.get(input.getNode());
162         if (l == null) {
163             LOG.debug("Session for node {} not found", input.getNode());
164             return OperationResults.UNSENT.future();
165         }
166
167         return l.ensureLspOperational(input);
168     }
169
170     @Override
171     public void close() throws TransactionCommitFailedException {
172         if (this.runtimeRootRegistration.isPresent()) {
173             this.runtimeRootRegistration.get().close();
174         }
175         for (final TopologySessionListener sessionListener : this.nodes.values()) {
176             sessionListener.close();
177         }
178         for (final TopologyNodeState nodeState : this.state.values()) {
179             nodeState.close();
180         }
181         final WriteTransaction t = this.broker.newWriteOnlyTransaction();
182         t.delete(LogicalDatastoreType.OPERATIONAL, this.topology);
183         t.submit().checkedGet();
184     }
185
186     public void registerRuntimeRootRegistartion(final PCEPTopologyProviderRuntimeRegistrator runtimeRootRegistrator) {
187         this.runtimeRootRegistration = Optional.of(runtimeRootRegistrator.register(this));
188     }
189
190     public Optional<PCEPTopologyProviderRuntimeRegistration> getRuntimeRootRegistration() {
191         return this.runtimeRootRegistration;
192     }
193 }