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 org.opendaylight.controller.config.yang.pcep.topology.provider.PeerCapabilities;
21 import org.opendaylight.protocol.pcep.PCEPSession;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.PcinitiateBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Stateful1;
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.topology.pcep.rev131024.AddLspArgs;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.LspId;
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 protected void onSessionUp(final PCEPSession session, final PathComputationClientBuilder pccBuilder) {
76 final InetAddress peerAddress = session.getRemoteAddress();
78 final Tlvs tlvs = session.getRemoteTlvs();
79 final Tlvs2 tlv = tlvs.getAugmentation(Tlvs2.class);
81 final Stateful stateful = tlv.getStateful();
82 if (stateful != null) {
83 getSessionListenerState().setPeerCapabilities(getCapabilities(stateful));
84 pccBuilder.setReportedLsp(Collections.<ReportedLsp> emptyList());
85 pccBuilder.setStateSync(PccSyncState.InitialResync);
86 pccBuilder.setStatefulTlv(new StatefulTlvBuilder().addAugmentation(StatefulTlv1.class, new StatefulTlv1Builder(tlv).build()).build());
88 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
91 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
96 protected synchronized boolean onMessage(final MessageContext ctx, final Message message) {
97 if (!(message instanceof PcrptMessage)) {
101 getSessionListenerState().updateLastReceivedRptMsg();
102 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcrpt.message.PcrptMessage rpt = ((PcrptMessage) message).getPcrptMessage();
103 for (final Reports r : rpt.getReports()) {
104 final Lsp lsp = r.getLsp();
106 LOG.warn("PCRpt message received without LSP object.");
110 final PlspId id = lsp.getPlspId();
111 if (!lsp.isSync() && (id == null || id.getValue() == 0)) {
112 stateSynchronizationAchieved(ctx);
116 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.Tlvs tlvs = lsp.getTlvs();
117 final String name = (tlvs != null && tlvs.getSymbolicPathName() != null) ?
118 Charsets.UTF_8.decode(ByteBuffer.wrap(tlvs.getSymbolicPathName().getPathName().getValue())).toString()
121 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();
122 // set 0 by default, as the first report does not contain LSPIdentifiersTLV
123 pb.setLspId(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId(0L));
124 if (r.getPath() != null) {
125 pb.fieldsFrom(r.getPath());
128 final ReportedLspBuilder rlb = new ReportedLspBuilder();
129 rlb.addAugmentation(ReportedLsp1.class, new ReportedLsp1Builder().setLsp(lsp).build());
130 rlb.setPath(Collections.singletonList(pb.build()));
131 boolean solicited = false;
133 if (id.getValue() != 0) {
135 final PCEPRequest req = removeRequest(name);
137 LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.isOperational());
138 rlb.setMetadata(req.getMetadata());
139 ctx.resolveRequest(req);
141 LOG.warn("Request ID {} not found in outstanding DB", id);
145 if (!lsp.isRemove()) {
146 updateLsp(ctx, id, name, rlb, solicited, false);
147 LOG.debug("LSP {} updated", lsp);
150 LOG.debug("LSP {} removed", lsp);
158 public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
159 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, MISSING_XML_TAG);
160 // Make sure there is no such LSP
161 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
162 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
164 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
166 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
167 if (rep.isPresent()) {
168 LOG.debug("Node {} already contains lsp {} at {}", input.getNode(), input.getName(), lsp);
169 return OperationResults.UNSENT.future();
172 final SymbolicPathNameBuilder name = new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(input.getName().getBytes(Charsets.UTF_8)));
175 final RequestsBuilder rb = new RequestsBuilder();
176 rb.fieldsFrom(input.getArguments());
177 rb.setLspa(new LspaBuilder().setTlvs(
178 new TlvsBuilder().addAugmentation(
179 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2.class,
180 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2Builder().setSymbolicPathName(
181 name.build()).build()).build()).build());
183 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
184 ib.setRequests(Collections.singletonList(rb.build()));
187 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), input.getName(),
188 input.getArguments().getMetadata());
194 public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
195 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null, MISSING_XML_TAG);
196 // Make sure the LSP exists, we need it for PLSP-ID
197 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
198 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
200 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
202 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
203 final Lsp reportedLsp = validateReportedLsp(rep, input);
204 if (reportedLsp == null) {
205 LOG.warn("Reported LSP does not contain LSP object.");
206 return OperationResults.UNSENT.future();
208 // Build the request and send it
209 final UpdatesBuilder rb = new UpdatesBuilder();
210 rb.setLsp(new LspBuilder().setRemove(Boolean.TRUE).setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).build());
211 final PcupdMessageBuilder ib = new PcupdMessageBuilder(MESSAGE_HEADER);
212 ib.setUpdates(Collections.singletonList(rb.build()));
213 return sendMessage(new PcupdBuilder().setPcupdMessage(ib.build()).build(), rep.get().getName(), null);
219 public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
220 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, MISSING_XML_TAG);
221 // Make sure the LSP exists
222 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
223 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
225 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
227 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
228 final Lsp reportedLsp = validateReportedLsp(rep, input);
229 if (reportedLsp == null) {
230 return OperationResults.UNSENT.future();
232 final Arguments2 args = input.getArguments().getAugmentation(Arguments2.class);
233 Preconditions.checkArgument(args != null, "Input is missing operational tag.");
234 Preconditions.checkArgument(input.getArguments().getEro() != null, "Input is missing ERO object.");
235 // Build the PCUpd request and send it
236 final UpdatesBuilder rb = new UpdatesBuilder();
237 rb.setLsp(new LspBuilder().setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).setOperational(args.isOperational()).build());
238 final PathBuilder pb = new PathBuilder();
239 pb.fieldsFrom(input.getArguments());
240 rb.setPath(pb.build());
241 final PcupdMessageBuilder ub = new PcupdMessageBuilder(MESSAGE_HEADER);
242 ub.setUpdates(Collections.singletonList(rb.build()));
243 return sendMessage(new PcupdBuilder().setPcupdMessage(ub.build()).build(), rep.get().getName(), input.getArguments().getMetadata());
249 public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
250 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, MISSING_XML_TAG);
252 final Arguments1 aa = input.getArguments().getAugmentation(Arguments1.class);
254 LOG.warn("Operational status not present in MD-SAL.");
257 op = aa.isOperational();
260 // Make sure the LSP exists
261 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
262 LOG.debug("Checking if LSP {} has operational state {}", lsp, op);
263 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
265 return Futures.transform(f, new Function<Optional<ReportedLsp>, OperationResult>() {
267 public OperationResult apply(final Optional<ReportedLsp> rep) {
268 final Lsp reportedLsp = validateReportedLsp(rep, input);
269 if (reportedLsp == null) {
270 return OperationResults.UNSENT;
272 return reportedLsp.isOperational().equals(op) ? OperationResults.SUCCESS : OperationResults.UNSENT;
278 protected Lsp validateReportedLsp(final Optional<ReportedLsp> rep, final LspId input) {
279 if (!rep.isPresent()) {
280 LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
283 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
285 LOG.warn("Reported LSP reported null from data-store.");
288 final Lsp reportedLsp = ra.getLsp();
289 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
293 private static PeerCapabilities getCapabilities(final Stateful stateful) {
294 final PeerCapabilities capa = new PeerCapabilities();
295 capa.setStateful(true);
296 if (stateful.isLspUpdateCapability() != null) {
297 capa.setActive(stateful.isLspUpdateCapability());
299 final Stateful1 stateful1 = stateful.getAugmentation(Stateful1.class);
300 if (stateful1 != null && stateful1.isInitiation() != null) {
301 capa.setInstantiation(stateful1.isInitiation());