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 static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.AsyncFunction;
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.net.InetAddress;
19 import java.nio.ByteBuffer;
20 import java.nio.charset.StandardCharsets;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
25 import java.util.Optional;
26 import java.util.concurrent.atomic.AtomicBoolean;
27 import java.util.concurrent.atomic.AtomicLong;
28 import javax.annotation.Nonnull;
29 import javax.annotation.concurrent.GuardedBy;
30 import org.opendaylight.protocol.pcep.PCEPSession;
31 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
32 import org.opendaylight.protocol.pcep.spi.PSTUtil;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.PathComputationClient1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.PathComputationClient1Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.lsp.db.version.tlv.LspDbVersion;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.Lsp1;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.PcinitiateBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.Srp1;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.Srp1Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.Stateful1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.pcinitiate.message.PcinitiateMessageBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.pcinitiate.message.pcinitiate.message.Requests;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.pcinitiate.message.pcinitiate.message.RequestsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Arguments1;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Arguments2;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Arguments3;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.OperationalStatus;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Path1;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Path1Builder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.PcrptMessage;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.PcupdBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.PlspId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.SrpIdNumber;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.StatefulTlv1;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.StatefulTlv1Builder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.SymbolicPathName;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Tlvs1;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.object.Lsp;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.object.LspBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.object.lsp.TlvsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcerr.pcerr.message.error.type.StatefulCase;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcerr.pcerr.message.error.type.stateful._case.stateful.Srps;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcrpt.message.pcrpt.message.Reports;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcupd.message.PcupdMessageBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcupd.message.pcupd.message.UpdatesBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcupd.message.pcupd.message.updates.PathBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.srp.object.Srp;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.srp.object.SrpBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.stateful.capability.tlv.Stateful;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.symbolic.path.name.tlv.SymbolicPathNameBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.PcerrMessage;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.EroBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.setup.type.tlv.PathSetupType;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.AddLspArgs;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.EnsureLspOperationalInput;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.LspId;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.Node1;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.OperationResult;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.PccSyncState;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.RemoveLspArgs;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.TriggerSyncArgs;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.UpdateLspArgs;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.PathComputationClient;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.PathComputationClientBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.ReportedLsp;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.StatefulTlvBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.reported.lsp.Path;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
92 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
93 import org.slf4j.Logger;
94 import org.slf4j.LoggerFactory;
96 class Stateful07TopologySessionListener extends AbstractTopologySessionListener<SrpIdNumber, PlspId> {
97 private static final Logger LOG = LoggerFactory.getLogger(Stateful07TopologySessionListener.class);
99 private final AtomicLong requestId = new AtomicLong(1L);
102 private final List<PlspId> staleLsps = new ArrayList<>();
104 private final AtomicBoolean statefulCapability = new AtomicBoolean(false);
105 private final AtomicBoolean lspUpdateCapability = new AtomicBoolean(false);
106 private final AtomicBoolean initiationCapability = new AtomicBoolean(false);
109 * Creates a new stateful topology session listener for given server session manager.
111 Stateful07TopologySessionListener(final ServerSessionManager serverSessionManager) {
112 super(serverSessionManager);
115 private static LspDbVersion geLspDbVersionTlv(final Lsp lsp) {
116 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.object
117 .lsp.Tlvs tlvs = lsp.getTlvs();
118 if (tlvs != null && tlvs.augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
119 .controller.pcep.sync.optimizations.rev181109.Tlvs1.class) != null) {
120 return tlvs.augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller
121 .pcep.sync.optimizations.rev181109.Tlvs1.class).getLspDbVersion();
127 protected void onSessionUp(final PCEPSession session, final PathComputationClientBuilder pccBuilder) {
128 final InetAddress peerAddress = session.getRemoteAddress();
130 final Tlvs tlvs = session.getRemoteTlvs();
131 if (tlvs != null && tlvs.augmentation(Tlvs1.class) != null) {
132 final Stateful stateful = tlvs.augmentation(Tlvs1.class).getStateful();
133 if (stateful != null) {
134 setStatefulCapabilities(stateful);
135 pccBuilder.setReportedLsp(Collections.emptyList());
136 if (isSynchronized()) {
137 pccBuilder.setStateSync(PccSyncState.Synchronized);
138 } else if (isTriggeredInitialSynchro()) {
139 pccBuilder.setStateSync(PccSyncState.TriggeredInitialSync);
140 } else if (isIncrementalSynchro()) {
141 pccBuilder.setStateSync(PccSyncState.IncrementalSync);
143 pccBuilder.setStateSync(PccSyncState.InitialResync);
145 pccBuilder.setStatefulTlv(new StatefulTlvBuilder().addAugmentation(StatefulTlv1.class,
146 new StatefulTlv1Builder(tlvs.augmentation(Tlvs1.class)).build()).build());
148 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
151 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
156 public synchronized ListenableFuture<OperationResult> triggerSync(final TriggerSyncArgs input) {
157 if (isTriggeredInitialSynchro() && !isSynchronized()) {
158 return triggerSynchronization(input);
159 } else if (isSessionSynchronized() && isTriggeredReSyncEnabled()) {
160 Preconditions.checkArgument(input != null && input.getNode() != null, MISSING_XML_TAG);
161 if (input.getName() == null) {
162 return triggerResyncronization(input);
164 return triggerLspSyncronization(input);
167 return OperationResults.UNSENT.future();
170 private ListenableFuture<OperationResult> triggerLspSyncronization(final TriggerSyncArgs input) {
171 LOG.trace("Trigger Lsp Resynchronization {}", input);
173 // Make sure the LSP exists
174 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
175 final FluentFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
177 return OperationResults.createUnsent(PCEPErrors.LSP_INTERNAL_ERROR).future();
179 return Futures.transformAsync(f, new ResyncLspFunction(input), MoreExecutors.directExecutor());
182 private ListenableFuture<OperationResult> triggerResyncronization(final TriggerSyncArgs input) {
183 LOG.trace("Trigger Resynchronization {}", input);
185 updatePccState(PccSyncState.PcepTriggeredResync);
186 final PcupdMessageBuilder pcupdMessageBuilder = new PcupdMessageBuilder(MESSAGE_HEADER);
187 final SrpIdNumber srpIdNumber = createUpdateMessageSync(pcupdMessageBuilder);
188 final Message msg = new PcupdBuilder().setPcupdMessage(pcupdMessageBuilder.build()).build();
189 return sendMessage(msg, srpIdNumber, null);
192 private ListenableFuture<OperationResult> triggerSynchronization(final TriggerSyncArgs input) {
193 LOG.trace("Trigger Initial Synchronization {}", input);
194 final PcupdMessageBuilder pcupdMessageBuilder = new PcupdMessageBuilder(MESSAGE_HEADER);
195 final SrpIdNumber srpIdNumber = createUpdateMessageSync(pcupdMessageBuilder);
196 final Message msg = new PcupdBuilder().setPcupdMessage(pcupdMessageBuilder.build()).build();
197 return sendMessage(msg, srpIdNumber, null);
200 private SrpIdNumber createUpdateMessageSync(final PcupdMessageBuilder pcupdMessageBuilder) {
201 final UpdatesBuilder updBuilder = new UpdatesBuilder();
202 // LSP mandatory in Upd
203 final Lsp lsp = new LspBuilder().setPlspId(new PlspId(0L)).setSync(Boolean.TRUE).build();
204 // SRP Mandatory in Upd
205 final SrpBuilder srpBuilder = new SrpBuilder();
206 // not sue whether use 0 instead of nextRequest() or do not insert srp == SRP-ID-number = 0
207 srpBuilder.setOperationId(nextRequest());
208 final Srp srp = srpBuilder.build();
209 //ERO Mandatory in Upd
210 final PathBuilder pb = new PathBuilder();
211 pb.setEro(new EroBuilder().build());
213 updBuilder.setPath(pb.build());
214 updBuilder.setLsp(lsp).setSrp(srp).setPath(pb.build());
216 pcupdMessageBuilder.setUpdates(Collections.singletonList(updBuilder.build()));
217 return srp.getOperationId();
220 private void markAllLspAsStale() {
221 this.staleLsps.addAll(this.lsps.keySet());
224 private boolean handleErrorMessage(final PcerrMessage message) {
225 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message
226 .PcerrMessage errMsg = message.getPcerrMessage();
227 if (errMsg.getErrorType() instanceof StatefulCase) {
228 final StatefulCase stat = (StatefulCase) errMsg.getErrorType();
229 for (final Srps srps : stat.getStateful().getSrps()) {
230 final SrpIdNumber id = srps.getSrp().getOperationId();
231 if (id.getValue() != 0) {
232 final PCEPRequest req = removeRequest(id);
234 req.done(OperationResults.createFailed(errMsg.getErrors()));
236 LOG.warn("Request ID {} not found in outstanding DB", id);
241 LOG.warn("Unhandled PCErr message {}.", errMsg);
247 private boolean isSolicited(final Srp srp, final Lsp lsp, final MessageContext ctx, final ReportedLspBuilder rlb) {
251 final SrpIdNumber id = srp.getOperationId();
252 if (id.getValue() == 0) {
255 switch (lsp.getOperational()) {
259 if (!isTriggeredSyncInProcess()) {
260 final PCEPRequest req = removeRequest(id);
262 LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.getOperational());
263 rlb.setMetadata(req.getMetadata());
264 ctx.resolveRequest(req);
266 LOG.warn("Request ID {} not found in outstanding DB", id);
272 // These are transitive states, so we don't have to do anything, as they will be followed
281 private boolean manageNextReport(final Reports report, final MessageContext ctx) {
282 final Lsp lsp = report.getLsp();
283 final PlspId plspid = lsp.getPlspId();
284 final Srp srp = report.getSrp();
286 if (!lsp.isSync() && (plspid == null || plspid.getValue() == 0)) {
288 if (isTriggeredSyncInProcess()) {
292 final SrpIdNumber id = srp.getOperationId();
293 if (id.getValue() == 0) {
296 final PCEPRequest req = removeRequest(id);
297 ctx.resolveRequest(req);
299 stateSynchronizationAchieved(ctx);
302 final ReportedLspBuilder rlb = new ReportedLspBuilder();
303 boolean solicited = false;
304 solicited = isSolicited(srp, lsp, ctx, rlb);
306 // if remove flag is set in SRP object, remove the tunnel immediately
307 if (solicited && srp.augmentation(Srp1.class) != null) {
308 final Srp1 initiatedSrp = srp.augmentation(Srp1.class);
309 if (initiatedSrp.isRemove()) {
310 super.removeLsp(ctx, plspid);
314 rlb.setPath(Collections.singletonList(buildPath(report, srp, lsp)));
316 String name = lookupLspName(plspid);
317 if (lsp.getTlvs() != null && lsp.getTlvs().getSymbolicPathName() != null) {
318 name = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(lsp.getTlvs().getSymbolicPathName().getPathName()
319 .getValue())).toString();
321 //get LspDB from LSP and write it to pcc's node
322 final LspDbVersion lspDbVersion = geLspDbVersionTlv(lsp);
323 if (lspDbVersion != null) {
324 updatePccNode(ctx, new PathComputationClientBuilder().addAugmentation(PathComputationClient1.class,
325 new PathComputationClient1Builder().setLspDbVersion(lspDbVersion).build()).build());
327 updateLsp(ctx, plspid, name, rlb, solicited, lsp.isRemove());
328 unmarkStaleLsp(plspid);
330 LOG.debug("LSP {} updated", lsp);
334 private Path buildPath(final Reports report, final Srp srp, final Lsp lsp) {
335 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client
336 .attributes.path.computation.client.reported.lsp.PathBuilder pb = new org.opendaylight.yang.gen.v1
337 .urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation
338 .client.reported.lsp.PathBuilder();
339 if (report.getPath() != null) {
340 pb.fieldsFrom(report.getPath());
342 // LSP is mandatory (if there is none, parser will throw an exception)
343 // this is to ensure a path will be created at any rate
344 final Path1Builder p1Builder = new Path1Builder();
345 p1Builder.setLsp(report.getLsp());
346 final PathSetupType pst;
347 if (srp != null && srp.getTlvs() != null && srp.getTlvs().getPathSetupType() != null) {
348 pst = srp.getTlvs().getPathSetupType();
349 p1Builder.setPathSetupType(pst);
353 pb.addAugmentation(Path1.class, p1Builder.build());
354 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp
355 .object.lsp.Tlvs tlvs = report.getLsp().getTlvs();
357 if (tlvs.getLspIdentifiers() != null) {
358 pb.setLspId(tlvs.getLspIdentifiers().getLspId());
359 } else if (!PSTUtil.isDefaultPST(pst)) {
360 pb.setLspId(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820
361 .LspId(lsp.getPlspId().getValue()));
368 protected synchronized boolean onMessage(final MessageContext ctx, final Message message) {
369 if (message instanceof PcerrMessage) {
370 return handleErrorMessage((PcerrMessage) message);
372 if (!(message instanceof PcrptMessage)) {
375 this.listenerState.updateLastReceivedRptMsg();
376 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcrpt
377 .message.PcrptMessage rpt = ((PcrptMessage) message).getPcrptMessage();
378 for (final Reports report : rpt.getReports()) {
379 if (!manageNextReport(report, ctx)) {
386 private SrpIdNumber nextRequest() {
387 return new SrpIdNumber(this.requestId.getAndIncrement());
391 public synchronized ListenableFuture<OperationResult> addLsp(@Nonnull final AddLspArgs input) {
392 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null
393 && input.getArguments() != null, MISSING_XML_TAG);
394 LOG.trace("AddLspArgs {}", input);
395 // Make sure there is no such LSP
396 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
397 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
399 return OperationResults.createUnsent(PCEPErrors.LSP_INTERNAL_ERROR).future();
401 return Futures.transformAsync(f, new AddFunction(input, lsp), MoreExecutors.directExecutor());
405 public synchronized ListenableFuture<OperationResult> removeLsp(@Nonnull final RemoveLspArgs input) {
406 Preconditions.checkArgument(input != null && input.getName() != null
407 && input.getNode() != null, MISSING_XML_TAG);
408 LOG.trace("RemoveLspArgs {}", input);
409 // Make sure the LSP exists, we need it for PLSP-ID
410 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
411 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
413 return OperationResults.createUnsent(PCEPErrors.LSP_INTERNAL_ERROR).future();
415 return Futures.transformAsync(f, rep -> {
416 final Lsp reportedLsp = validateReportedLsp(rep, input);
417 if (reportedLsp == null) {
418 return OperationResults.createUnsent(PCEPErrors.UNKNOWN_PLSP_ID).future();
420 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
421 final Requests rb = buildRequest(rep, reportedLsp);
422 ib.setRequests(Collections.singletonList(rb));
423 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(),
424 rb.getSrp().getOperationId(), null);
425 }, MoreExecutors.directExecutor());
428 private Requests buildRequest(final Optional<ReportedLsp> rep, final Lsp reportedLsp) {
429 // Build the request and send it
430 final RequestsBuilder rb = new RequestsBuilder();
431 final SrpBuilder srpBuilder = new SrpBuilder().addAugmentation(Srp1.class, new Srp1Builder()
432 .setRemove(Boolean.TRUE).build()).setOperationId(nextRequest()).setProcessingRule(Boolean.TRUE);
433 final Optional<PathSetupType> maybePST = getPST(rep);
434 if (maybePST.isPresent()) {
435 srpBuilder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful
436 .rev181109.srp.object.srp.TlvsBuilder().setPathSetupType(maybePST.get()).build());
438 rb.setSrp(srpBuilder.build());
439 rb.setLsp(new LspBuilder().setRemove(Boolean.FALSE).setPlspId(reportedLsp.getPlspId())
440 .setDelegate(reportedLsp.isDelegate()).build());
444 private ListenableFuture<OperationResult> redelegate(final Lsp reportedLsp, final Srp srp, final Lsp lsp,
445 final UpdateLspArgs input) {
446 // the D bit that was reported decides the type of PCE message sent
447 requireNonNull(reportedLsp.isDelegate());
449 if (reportedLsp.isDelegate()) {
450 // we already have delegation, send update
451 final UpdatesBuilder rb = new UpdatesBuilder();
454 final PathBuilder pb = new PathBuilder();
455 pb.fieldsFrom(input.getArguments());
456 rb.setPath(pb.build());
457 final PcupdMessageBuilder ub = new PcupdMessageBuilder(MESSAGE_HEADER);
458 ub.setUpdates(Collections.singletonList(rb.build()));
459 msg = new PcupdBuilder().setPcupdMessage(ub.build()).build();
461 final Lsp1 lspCreateFlag = reportedLsp.augmentation(Lsp1.class);
462 // we only retake delegation for PCE initiated tunnels
463 if (lspCreateFlag != null && !lspCreateFlag.isCreate()) {
464 LOG.warn("Unable to retake delegation of PCC-initiated tunnel: {}", reportedLsp);
465 return OperationResults.createUnsent(PCEPErrors.UPDATE_REQ_FOR_NON_LSP).future();
467 // we want to revoke delegation, different type of message
468 // is sent because of specification by Siva
469 // this message is also sent, when input delegate bit is set to 0
470 // generating an error in PCC
471 final List<Requests> reqs = new ArrayList<>();
472 reqs.add(new RequestsBuilder().setSrp(srp).setLsp(lsp).build());
473 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder();
474 ib.setRequests(reqs);
475 msg = new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build();
477 return sendMessage(msg, srp.getOperationId(), input.getArguments().getMetadata());
481 public synchronized ListenableFuture<OperationResult> updateLsp(@Nonnull final UpdateLspArgs input) {
482 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null
483 && input.getArguments() != null, MISSING_XML_TAG);
484 LOG.trace("UpdateLspArgs {}", input);
485 // Make sure the LSP exists
486 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
487 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
489 return OperationResults.createUnsent(PCEPErrors.LSP_INTERNAL_ERROR).future();
491 return Futures.transformAsync(f, new UpdateFunction(input), MoreExecutors.directExecutor());
495 public synchronized ListenableFuture<OperationResult> ensureLspOperational(
496 @Nonnull final EnsureLspOperationalInput input) {
497 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null
498 && input.getArguments() != null, MISSING_XML_TAG);
499 final OperationalStatus op;
500 final Arguments1 aa = input.getArguments().augmentation(Arguments1.class);
502 op = aa.getOperational();
507 // Make sure the LSP exists
508 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
509 LOG.debug("Checking if LSP {} has operational state {}", lsp, op);
510 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
512 return OperationResults.createUnsent(PCEPErrors.LSP_INTERNAL_ERROR).future();
514 return listenableFuture(f, input, op);
517 private ListenableFuture<OperationResult> listenableFuture(final ListenableFuture<Optional<ReportedLsp>> future,
518 final EnsureLspOperationalInput input, final OperationalStatus op) {
519 return Futures.transform(future, rep -> {
520 if (!rep.isPresent()) {
521 LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
522 return OperationResults.UNSENT;
524 // check if at least one of the paths has the same status as requested
525 for (final Path p : rep.get().getPath()) {
526 final Path1 p1 = p.augmentation(Path1.class);
528 LOG.warn("Node {} LSP {} does not contain data", input.getNode(), input.getName());
529 return OperationResults.UNSENT;
531 if (op.equals(p1.getLsp().getOperational())) {
532 return OperationResults.SUCCESS;
535 return OperationResults.UNSENT;
536 }, MoreExecutors.directExecutor());
540 protected Lsp validateReportedLsp(final Optional<ReportedLsp> rep, final LspId input) {
541 if (!rep.isPresent()) {
542 LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
545 // it doesn't matter how many lsps there are in the path list, we only need data that is the same in each path
546 final Path1 ra = rep.get().getPath().get(0).augmentation(Path1.class);
547 Preconditions.checkState(ra != null, "Reported LSP reported null from data-store.");
548 final Lsp reportedLsp = ra.getLsp();
549 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
553 private Optional<PathSetupType> getPST(final Optional<ReportedLsp> rep) {
554 if (rep.isPresent()) {
555 final Path1 path1 = rep.get().getPath().get(0).augmentation(Path1.class);
557 final PathSetupType pst = path1.getPathSetupType();
558 if (!PSTUtil.isDefaultPST(pst)) {
559 return Optional.of(pst);
563 return Optional.empty();
567 * Recover lspData and mark any LSPs in the LSP database that were previously reported by the PCC as stale.
570 protected synchronized void loadLspData(final Node node, final Map<String, ReportedLsp> lspData,
571 final Map<PlspId, String> lsps, final boolean incrementalSynchro) {
572 //load node's lsps from DS
573 final PathComputationClient pcc = node.augmentation(Node1.class).getPathComputationClient();
574 final List<ReportedLsp> reportedLsps = pcc.getReportedLsp();
575 for (final ReportedLsp reportedLsp : reportedLsps) {
576 final String lspName = reportedLsp.getName();
577 lspData.put(lspName, reportedLsp);
578 if (!reportedLsp.getPath().isEmpty()) {
579 final Path1 path1 = reportedLsp.getPath().get(0).augmentation(Path1.class);
581 final PlspId plspId = path1.getLsp().getPlspId();
582 if (!incrementalSynchro) {
583 this.staleLsps.add(plspId);
585 lsps.put(plspId, lspName);
592 * When the PCC reports an LSP during state synchronization, if the LSP already
593 * exists in the LSP database, the PCE MUST update the LSP database and
594 * clear the stale marker from the LSP.
598 private synchronized void unmarkStaleLsp(final PlspId plspId) {
599 this.staleLsps.remove(plspId);
603 * Purge any LSPs from the LSP database that are still marked as stale.
605 * @param ctx message context
607 private synchronized void purgeStaleLsps(final MessageContext ctx) {
608 for (final PlspId plspId : this.staleLsps) {
609 removeLsp(ctx, plspId);
611 this.staleLsps.clear();
615 public boolean isInitiationCapability() {
616 return this.initiationCapability.get();
620 public boolean isStatefulCapability() {
621 return this.statefulCapability.get();
625 public boolean isLspUpdateCapability() {
626 return this.lspUpdateCapability.get();
629 private synchronized void setStatefulCapabilities(final Stateful stateful) {
630 this.statefulCapability.set(true);
631 if (stateful.isLspUpdateCapability() != null) {
632 this.lspUpdateCapability.set(stateful.isLspUpdateCapability());
634 final Stateful1 stateful1 = stateful.augmentation(Stateful1.class);
635 if (stateful1 != null && stateful1.isInitiation() != null) {
636 this.initiationCapability.set(stateful1.isInitiation());
640 private class ResyncLspFunction implements AsyncFunction<Optional<ReportedLsp>, OperationResult> {
642 private final TriggerSyncArgs input;
644 ResyncLspFunction(final TriggerSyncArgs input) {
649 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
650 final Lsp reportedLsp = validateReportedLsp(rep, this.input);
651 if (reportedLsp == null || !rep.isPresent()) {
652 return OperationResults.createUnsent(PCEPErrors.UNKNOWN_PLSP_ID).future();
655 final ReportedLsp staleLsp = rep.get();
656 if (!staleLsp.getPath().isEmpty()) {
657 final Path1 path1 = staleLsp.getPath().get(0).augmentation(Path1.class);
659 Stateful07TopologySessionListener.this.staleLsps.add(path1.getLsp().getPlspId());
662 updatePccState(PccSyncState.PcepTriggeredResync);
663 // create PCUpd with mandatory objects and LSP object set to 1
664 final SrpBuilder srpBuilder = new SrpBuilder();
665 srpBuilder.setOperationId(nextRequest());
666 srpBuilder.setProcessingRule(Boolean.TRUE);
668 final Optional<PathSetupType> maybePST = getPST(rep);
669 if (maybePST.isPresent()) {
671 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful
672 .rev181109.srp.object.srp.TlvsBuilder()
673 .setPathSetupType(maybePST.get()).build());
676 final Srp srp = srpBuilder.build();
677 final Lsp lsp = new LspBuilder().setPlspId(reportedLsp.getPlspId()).setSync(Boolean.TRUE).build();
679 final Message msg = createPcepUpd(srp, lsp);
680 return sendMessage(msg, srp.getOperationId(), null);
683 private Message createPcepUpd(final Srp srp, final Lsp lsp) {
684 final UpdatesBuilder rb = new UpdatesBuilder();
687 final PathBuilder pb = new PathBuilder();
688 rb.setPath(pb.build());
689 final PcupdMessageBuilder ub = new PcupdMessageBuilder(MESSAGE_HEADER);
690 ub.setUpdates(Collections.singletonList(rb.build()));
691 return new PcupdBuilder().setPcupdMessage(ub.build()).build();
695 private class AddFunction implements AsyncFunction<Optional<ReportedLsp>, OperationResult> {
697 private final AddLspArgs input;
698 private final InstanceIdentifier<ReportedLsp> lsp;
700 AddFunction(final AddLspArgs input, final InstanceIdentifier<ReportedLsp> lsp) {
706 public ListenableFuture<OperationResult> apply(@Nonnull final Optional<ReportedLsp> rep) {
707 if (rep.isPresent()) {
708 LOG.debug("Node {} already contains lsp {} at {}", this.input.getNode(), this.input.getName(),
710 return OperationResults.createUnsent(PCEPErrors.USED_SYMBOLIC_PATH_NAME).future();
712 if (!Stateful07TopologySessionListener.this.initiationCapability.get()) {
713 return OperationResults.createUnsent(PCEPErrors.CAPABILITY_NOT_SUPPORTED).future();
717 final RequestsBuilder rb = new RequestsBuilder();
718 final Arguments2 args = this.input.getArguments().augmentation(Arguments2.class);
719 final Lsp inputLsp = (args != null) ? args.getLsp() : null;
720 if (inputLsp == null) {
721 return OperationResults.createUnsent(PCEPErrors.LSP_MISSING).future();
724 rb.fieldsFrom(this.input.getArguments());
726 final TlvsBuilder tlvsBuilder;
727 if (inputLsp.getTlvs() != null) {
728 tlvsBuilder = new TlvsBuilder(inputLsp.getTlvs());
730 tlvsBuilder = new TlvsBuilder();
732 tlvsBuilder.setSymbolicPathName(
733 new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(this.input.getName()
734 .getBytes(StandardCharsets.UTF_8))).build());
736 final SrpBuilder srpBuilder = new SrpBuilder();
737 srpBuilder.setOperationId(nextRequest());
738 srpBuilder.setProcessingRule(Boolean.TRUE);
739 if (!PSTUtil.isDefaultPST(args.getPathSetupType())) {
741 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf
742 .stateful.rev181109.srp.object.srp.TlvsBuilder()
743 .setPathSetupType(args.getPathSetupType()).build());
745 rb.setSrp(srpBuilder.build());
747 rb.setLsp(new LspBuilder().setAdministrative(inputLsp.isAdministrative()).setDelegate(
748 inputLsp.isDelegate()).setPlspId(new PlspId(0L)).setTlvs(tlvsBuilder.build()).build());
750 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
751 ib.setRequests(Collections.singletonList(rb.build()));
754 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(),
755 rb.getSrp().getOperationId(), this.input.getArguments().getMetadata());
759 private class UpdateFunction implements AsyncFunction<Optional<ReportedLsp>, OperationResult> {
761 private final UpdateLspArgs input;
763 UpdateFunction(final UpdateLspArgs input) {
768 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
769 final Lsp reportedLsp = validateReportedLsp(rep, this.input);
770 if (reportedLsp == null) {
771 return OperationResults.createUnsent(PCEPErrors.UNKNOWN_PLSP_ID).future();
773 // create mandatory objects
774 final Arguments3 args = this.input.getArguments().augmentation(Arguments3.class);
775 final SrpBuilder srpBuilder = new SrpBuilder();
776 srpBuilder.setOperationId(nextRequest());
777 srpBuilder.setProcessingRule(Boolean.TRUE);
778 if ((args != null && args.getPathSetupType() != null)) {
779 if (!PSTUtil.isDefaultPST(args.getPathSetupType())) {
781 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful
782 .rev181109.srp.object.srp.TlvsBuilder()
783 .setPathSetupType(args.getPathSetupType()).build());
786 final Optional<PathSetupType> maybePST = getPST(rep);
787 if (maybePST.isPresent()) {
789 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful
790 .rev181109.srp.object.srp.TlvsBuilder()
791 .setPathSetupType(maybePST.get()).build());
794 final Srp srp = srpBuilder.build();
795 final Lsp inputLsp = (args != null) ? args.getLsp() : null;
796 final LspBuilder lspBuilder = new LspBuilder().setPlspId(reportedLsp.getPlspId());
797 if (inputLsp != null) {
798 lspBuilder.setDelegate(inputLsp.isDelegate() != null && inputLsp.isDelegate())
799 .setTlvs(inputLsp.getTlvs())
800 .setAdministrative(inputLsp.isAdministrative() != null && inputLsp.isAdministrative());
802 return redelegate(reportedLsp, srp, lspBuilder.build(), this.input);