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.protocol.pcep.PCEPSession;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.PcinitiateBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.pcinitiate.message.PcinitiateMessageBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.pcinitiate.message.pcinitiate.message.RequestsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Arguments1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Arguments2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PcrptMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PcupdBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PlspId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.ReportedLsp1;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.ReportedLsp1Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.StatefulTlv1;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.StatefulTlv1Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.SymbolicPathName;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.Lsp;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.LspBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcrpt.message.pcrpt.message.Reports;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.PcupdMessageBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.UpdatesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.updates.PathBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathNameBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.LspaBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.PccSyncState;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClientBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.StatefulTlvBuilder;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 public class Stateful02TopologySessionListener extends AbstractTopologySessionListener<String, PlspId> {
63 private static final Logger LOG = LoggerFactory.getLogger(Stateful02TopologySessionListener.class);
66 * @param serverSessionManager
68 Stateful02TopologySessionListener(final ServerSessionManager serverSessionManager) {
69 super(serverSessionManager);
73 protected void onSessionUp(final PCEPSession session, final PathComputationClientBuilder pccBuilder) {
74 final InetAddress peerAddress = session.getRemoteAddress();
76 final Tlvs tlvs = session.getRemoteTlvs();
77 final Tlvs2 tlv = tlvs.getAugmentation(Tlvs2.class);
79 final Stateful stateful = tlv.getStateful();
80 if (stateful != null) {
81 pccBuilder.setReportedLsp(Collections.<ReportedLsp> emptyList());
82 pccBuilder.setStateSync(PccSyncState.InitialResync);
83 pccBuilder.setStatefulTlv(new StatefulTlvBuilder().addAugmentation(StatefulTlv1.class, new StatefulTlv1Builder(tlv).build()).build());
85 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
88 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
93 protected synchronized boolean onMessage(final MessageContext ctx, final Message message) {
94 if (!(message instanceof PcrptMessage)) {
98 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcrpt.message.PcrptMessage rpt = ((PcrptMessage) message).getPcrptMessage();
99 for (final Reports r : rpt.getReports()) {
100 final Lsp lsp = r.getLsp();
102 LOG.warn("PCRpt message received without LSP object.");
106 final PlspId id = lsp.getPlspId();
107 if (!lsp.isSync() && (id == null || id.getValue() == 0)) {
108 stateSynchronizationAchieved(ctx);
112 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.Tlvs tlvs = lsp.getTlvs();
114 if (tlvs != null && tlvs.getSymbolicPathName() != null) {
115 name = Charsets.UTF_8.decode(ByteBuffer.wrap(tlvs.getSymbolicPathName().getPathName().getValue())).toString();
117 name = lookupLspName(id);
120 final ReportedLspBuilder rlb = new ReportedLspBuilder();
121 rlb.addAugmentation(ReportedLsp1.class, new ReportedLsp1Builder().setLsp(lsp).build());
122 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();
123 if (r.getPath() != null) {
124 pb.fieldsFrom(r.getPath());
126 // set 0 by default, as the first report does not contain LSPIdentifiersTLV
127 pb.setLspId(new LspId(0L));
128 rlb.setPath(Collections.singletonList(pb.build()));
129 boolean solicited = false;
131 if (id.getValue() != 0) {
134 final PCEPRequest req = removeRequest(name);
136 LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.isOperational());
137 rlb.setMetadata(req.getMetadata());
138 ctx.resolveRequest(req);
140 LOG.warn("Request ID {} not found in outstanding DB", id);
144 if (!lsp.isRemove()) {
145 updateLsp(ctx, id, name, rlb, solicited, false);
146 LOG.debug("LSP {} updated", lsp);
149 LOG.debug("LSP {} removed", lsp);
157 public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
158 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, "Mandatory XML tags are missing.");
159 // Make sure there is no such LSP
160 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
161 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
163 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
165 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
166 if (rep.isPresent()) {
167 LOG.debug("Node {} already contains lsp {} at {}", input.getNode(), input.getName(), lsp);
168 return OperationResults.UNSENT.future();
171 final SymbolicPathNameBuilder name = new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(input.getName().getBytes()));
174 final RequestsBuilder rb = new RequestsBuilder();
175 rb.fieldsFrom(input.getArguments());
176 rb.setLspa(new LspaBuilder().setTlvs(
177 new TlvsBuilder().addAugmentation(
178 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2.class,
179 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2Builder().setSymbolicPathName(
180 name.build()).build()).build()).build());
182 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
183 ib.setRequests(Collections.singletonList(rb.build()));
186 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), input.getName(),
187 input.getArguments().getMetadata());
193 public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
194 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null, "Mandatory XML tags are missing.");
195 // Make sure the LSP exists, we need it for PLSP-ID
196 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
197 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
199 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
201 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
202 if (!rep.isPresent()) {
203 LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
204 return OperationResults.UNSENT.future();
207 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
208 Preconditions.checkState(ra != null, "Reported LSP reported null from data-store.");
209 final Lsp reportedLsp = ra.getLsp();
210 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
212 // Build the request and send it
213 final UpdatesBuilder rb = new UpdatesBuilder();
214 rb.setLsp(new LspBuilder().setRemove(Boolean.TRUE).setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).build());
216 final PcupdMessageBuilder ib = new PcupdMessageBuilder(MESSAGE_HEADER);
217 ib.setUpdates(Collections.singletonList(rb.build()));
218 return sendMessage(new PcupdBuilder().setPcupdMessage(ib.build()).build(), rep.get().getName(), null);
224 public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
225 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, "Mandatory XML tags are missing.");
226 // Make sure the LSP exists
227 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
228 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
230 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
232 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
233 if (!rep.isPresent()) {
234 LOG.warn("Node {} does not contain LSP {}", input.getNode(), input.getName());
235 return OperationResults.UNSENT.future();
238 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
239 Preconditions.checkState(ra != null, "Reported LSP reported null from data-store.");
240 final Lsp reportedLsp = ra.getLsp();
241 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
242 final Arguments2 args = input.getArguments().getAugmentation(Arguments2.class);
243 Preconditions.checkArgument(args != null, "Input is missing operational tag.");
244 Preconditions.checkArgument(input.getArguments().getEro() != null, "Input is missing ERO object.");
246 // Build the PCUpd request and send it
247 final UpdatesBuilder rb = new UpdatesBuilder();
248 rb.setLsp(new LspBuilder().setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).setOperational(args.isOperational()).build());
249 final PathBuilder pb = new PathBuilder();
250 pb.fieldsFrom(input.getArguments());
251 rb.setPath(pb.build());
252 final PcupdMessageBuilder ub = new PcupdMessageBuilder(MESSAGE_HEADER);
253 ub.setUpdates(Collections.singletonList(rb.build()));
254 return sendMessage(new PcupdBuilder().setPcupdMessage(ub.build()).build(), rep.get().getName(), input.getArguments().getMetadata());
260 public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
261 Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, "Mandatory XML tags are missing.");
263 final Arguments1 aa = input.getArguments().getAugmentation(Arguments1.class);
265 LOG.warn("Operational status not present in MD-SAL.");
268 op = aa.isOperational();
271 // Make sure the LSP exists
272 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
273 LOG.debug("Checking if LSP {} has operational state {}", lsp, op);
274 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
276 return Futures.transform(f, new Function<Optional<ReportedLsp>, OperationResult>() {
278 public OperationResult apply(final Optional<ReportedLsp> rep) {
279 if (!rep.isPresent()) {
280 LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
281 return OperationResults.UNSENT;
284 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
286 LOG.warn("Node {} LSP {} does not contain data", input.getNode(), input.getName());
287 return OperationResults.UNSENT;
289 final Lsp reportedLsp = ra.getLsp();
290 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
291 if (reportedLsp.isOperational().equals(op)) {
292 return OperationResults.SUCCESS;
294 return OperationResults.UNSENT;