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