2 * Copyright (c) 2014 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 com.google.common.base.Charsets;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.AsyncFunction;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.net.InetAddress;
18 import java.nio.ByteBuffer;
19 import java.util.Collections;
20 import javax.annotation.concurrent.GuardedBy;
22 import org.opendaylight.protocol.pcep.PCEPSession;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.PcinitiateBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.pcinitiate.message.PcinitiateMessageBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.pcinitiate.message.pcinitiate.message.RequestsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Arguments1;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Arguments2;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PcrptMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PcupdBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PlspId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.ReportedLsp1;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.ReportedLsp1Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.StatefulTlv1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.StatefulTlv1Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.SymbolicPathName;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.Lsp;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.LspBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcrpt.message.pcrpt.message.Reports;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.PcupdMessageBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.UpdatesBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.updates.PathBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathNameBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.LspaBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.PccSyncState;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClientBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.StatefulTlvBuilder;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
64 public class Stateful02TopologySessionListener extends AbstractTopologySessionListener<String, PlspId> {
65 private static final Logger LOG = LoggerFactory.getLogger(Stateful02TopologySessionListener.class);
68 * @param serverSessionManager
70 Stateful02TopologySessionListener(final ServerSessionManager serverSessionManager) {
71 super(serverSessionManager);
75 private long requestId = 1;
78 protected void onSessionUp(final PCEPSession session, final PathComputationClientBuilder pccBuilder) {
79 final InetAddress peerAddress = session.getRemoteAddress();
81 final Tlvs tlvs = session.getRemoteTlvs();
82 final Tlvs2 tlv = tlvs.getAugmentation(Tlvs2.class);
84 final Stateful stateful = tlv.getStateful();
85 if (stateful != null) {
86 pccBuilder.setReportedLsp(Collections.<ReportedLsp> emptyList());
87 pccBuilder.setStateSync(PccSyncState.InitialResync);
88 pccBuilder.setStatefulTlv(new StatefulTlvBuilder().addAugmentation(StatefulTlv1.class, new StatefulTlv1Builder(tlv).build()).build());
90 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
93 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
98 protected synchronized boolean onMessage(final MessageContext ctx, final Message message) {
99 if (!(message instanceof PcrptMessage)) {
103 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcrpt.message.PcrptMessage rpt = ((PcrptMessage) message).getPcrptMessage();
104 for (final Reports r : rpt.getReports()) {
105 final Lsp lsp = r.getLsp();
107 LOG.warn("PCRpt message received without LSP object.");
111 final PlspId id = lsp.getPlspId();
112 if (!lsp.isSync() && (id == null || id.getValue() == 0)) {
113 stateSynchronizationAchieved(ctx);
117 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.Tlvs tlvs = lsp.getTlvs();
119 if (tlvs != null && tlvs.getSymbolicPathName() != null) {
120 name = Charsets.UTF_8.decode(ByteBuffer.wrap(tlvs.getSymbolicPathName().getPathName().getValue())).toString();
122 name = lookupLspName(id);
125 final ReportedLspBuilder rlb = new ReportedLspBuilder();
126 rlb.addAugmentation(ReportedLsp1.class, new ReportedLsp1Builder().setLsp(lsp).build());
127 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.reported.lsp.PathBuilder pb = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.reported.lsp.PathBuilder();
128 if (r.getPath() != null) {
129 pb.fieldsFrom(r.getPath());
131 // set 0 by default, as the first report does not contain LSPIdentifiersTLV
132 pb.setLspId(new LspId(0L));
133 rlb.setPath(Collections.singletonList(pb.build()));
134 boolean solicited = false;
136 if (id.getValue() != 0) {
139 final PCEPRequest req = removeRequest(name);
141 LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.isOperational());
142 rlb.setMetadata(req.getMetadata());
143 ctx.resolveRequest(req);
145 LOG.warn("Request ID {} not found in outstanding DB", id);
149 if (!lsp.isRemove()) {
150 updateLsp(ctx, id, name, rlb, solicited, false);
151 LOG.debug("LSP {} updated", lsp);
154 LOG.debug("LSP {} removed", lsp);
162 private PlspId nextRequest() {
163 return new PlspId(this.requestId++);
167 public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
168 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, "Mandatory XML tags are missing.");
169 // Make sure there is no such LSP
170 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
171 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
173 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
175 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
176 if (rep.isPresent()) {
177 LOG.debug("Node {} already contains lsp {} at {}", input.getNode(), input.getName(), lsp);
178 return OperationResults.UNSENT.future();
181 final SymbolicPathNameBuilder name = new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(input.getName().getBytes()));
184 final RequestsBuilder rb = new RequestsBuilder();
185 rb.fieldsFrom(input.getArguments());
186 rb.setLspa(new LspaBuilder().setTlvs(
187 new TlvsBuilder().addAugmentation(
188 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2.class,
189 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2Builder().setSymbolicPathName(
190 name.build()).build()).build()).build());
192 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
193 ib.setRequests(Collections.singletonList(rb.build()));
196 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), input.getName(),
197 input.getArguments().getMetadata());
203 public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
204 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null, "Mandatory XML tags are missing.");
205 // Make sure the LSP exists, we need it for PLSP-ID
206 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
207 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
209 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
211 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
212 if (!rep.isPresent()) {
213 LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
214 return OperationResults.UNSENT.future();
217 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
218 Preconditions.checkState(ra != null, "Reported LSP reported null from data-store.");
219 final Lsp reportedLsp = ra.getLsp();
220 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
222 // Build the request and send it
223 final UpdatesBuilder rb = new UpdatesBuilder();
224 rb.setLsp(new LspBuilder().setRemove(Boolean.TRUE).setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).build());
226 final PcupdMessageBuilder ib = new PcupdMessageBuilder(MESSAGE_HEADER);
227 ib.setUpdates(Collections.singletonList(rb.build()));
228 return sendMessage(new PcupdBuilder().setPcupdMessage(ib.build()).build(), rep.get().getName(), null);
234 public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
235 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, "Mandatory XML tags are missing.");
236 // Make sure the LSP exists
237 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
238 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
240 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
242 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
243 if (!rep.isPresent()) {
244 LOG.warn("Node {} does not contain LSP {}", input.getNode(), input.getName());
245 return OperationResults.UNSENT.future();
248 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
249 Preconditions.checkState(ra != null, "Reported LSP reported null from data-store.");
250 final Lsp reportedLsp = ra.getLsp();
251 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
252 final Arguments2 args = input.getArguments().getAugmentation(Arguments2.class);
253 Preconditions.checkArgument(args != null, "Input is missing operational tag.");
254 Preconditions.checkArgument(input.getArguments().getEro() != null, "Input is missing ERO object.");
256 // Build the PCUpd request and send it
257 final UpdatesBuilder rb = new UpdatesBuilder();
258 rb.setLsp(new LspBuilder().setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).setOperational(args.isOperational()).build());
259 final PathBuilder pb = new PathBuilder();
260 pb.fieldsFrom(input.getArguments());
261 rb.setPath(pb.build());
262 final PcupdMessageBuilder ub = new PcupdMessageBuilder(MESSAGE_HEADER);
263 ub.setUpdates(Collections.singletonList(rb.build()));
264 return sendMessage(new PcupdBuilder().setPcupdMessage(ub.build()).build(), rep.get().getName(), input.getArguments().getMetadata());
270 public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
271 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, "Mandatory XML tags are missing.");
273 final Arguments1 aa = input.getArguments().getAugmentation(Arguments1.class);
275 LOG.warn("Operational status not present in MD-SAL.");
278 op = aa.isOperational();
281 // Make sure the LSP exists
282 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
283 LOG.debug("Checking if LSP {} has operational state {}", lsp, op);
284 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
286 return Futures.transform(f, new Function<Optional<ReportedLsp>, OperationResult>() {
288 public OperationResult apply(final Optional<ReportedLsp> rep) {
289 if (!rep.isPresent()) {
290 LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
291 return OperationResults.UNSENT;
294 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
296 LOG.warn("Node {} LSP {} does not contain data", input.getNode(), input.getName());
297 return OperationResults.UNSENT;
299 final Lsp reportedLsp = ra.getLsp();
300 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
301 if (reportedLsp.isOperational().equals(op)) {
302 return OperationResults.SUCCESS;
304 return OperationResults.UNSENT;