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.collect.ImmutableList;
15 import com.google.common.util.concurrent.AsyncFunction;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
19 import java.net.InetAddress;
20 import java.nio.ByteBuffer;
21 import java.util.Collections;
23 import javax.annotation.concurrent.GuardedBy;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.protocol.pcep.PCEPSession;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.PcinitiateBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.pcinitiate.message.PcinitiateMessageBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.pcinitiate.message.pcinitiate.message.RequestsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Arguments1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Arguments2;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PcrptMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PcupdBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PlspId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.ReportedLsp1;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.ReportedLsp1Builder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.StatefulTlv1;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.StatefulTlv1Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.SymbolicPathName;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.Lsp;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.LspBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcrpt.message.pcrpt.message.Reports;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.PcupdMessageBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.UpdatesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.updates.PathBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathNameBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.LspaBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.PccSyncState;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClientBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.StatefulTlvBuilder;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 public class Stateful02TopologySessionListener extends AbstractTopologySessionListener<String, PlspId> {
69 private static final Logger LOG = LoggerFactory.getLogger(Stateful02TopologySessionListener.class);
72 * @param serverSessionManager
74 Stateful02TopologySessionListener(final ServerSessionManager serverSessionManager) {
75 super(serverSessionManager);
79 private long requestId = 1;
82 protected void onSessionUp(final PCEPSession session, final PathComputationClientBuilder pccBuilder) {
83 final InetAddress peerAddress = session.getRemoteAddress();
85 final Tlvs tlvs = session.getRemoteTlvs();
86 final Tlvs2 tlv = tlvs.getAugmentation(Tlvs2.class);
88 final Stateful stateful = tlv.getStateful();
89 if (stateful != null) {
90 pccBuilder.setReportedLsp(Collections.<ReportedLsp> emptyList());
91 pccBuilder.setStateSync(PccSyncState.InitialResync);
92 pccBuilder.setStatefulTlv(new StatefulTlvBuilder().addAugmentation(StatefulTlv1.class, new StatefulTlv1Builder(tlv).build()).build());
94 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
97 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
102 protected synchronized boolean onMessage(final WriteTransaction trans, final Message message) {
103 if (!(message instanceof PcrptMessage)) {
107 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcrpt.message.PcrptMessage rpt = ((PcrptMessage) message).getPcrptMessage();
108 for (final Reports r : rpt.getReports()) {
109 final Lsp lsp = r.getLsp();
111 LOG.warn("PCRpt message received without LSP object.");
115 final PlspId id = lsp.getPlspId();
116 if (!lsp.isSync() && (id == null || id.getValue() == 0)) {
117 stateSynchronizationAchieved(trans);
121 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.Tlvs tlvs = lsp.getTlvs();
123 if (tlvs != null && tlvs.getSymbolicPathName() != null) {
124 name = Charsets.UTF_8.decode(ByteBuffer.wrap(tlvs.getSymbolicPathName().getPathName().getValue())).toString();
126 name = lookupLspName(id);
129 final ReportedLspBuilder rlb = new ReportedLspBuilder();
130 rlb.addAugmentation(ReportedLsp1.class, new ReportedLsp1Builder().setLsp(lsp).build());
131 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();
132 if (r.getPath() != null) {
133 pb.fieldsFrom(r.getPath());
135 // set 0 by default, as the first report does not contain LSPIdentifiersTLV
136 pb.setLspId(new LspId(0L));
137 rlb.setPath(Collections.singletonList(pb.build()));
138 boolean solicited = false;
140 if (id.getValue() != 0) {
143 final PCEPRequest req = removeRequest(name);
145 LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.isOperational());
146 rlb.setMetadata(req.getMetadata());
147 req.setResult(OperationResults.SUCCESS);
149 LOG.warn("Request ID {} not found in outstanding DB", id);
153 if (!lsp.isRemove()) {
154 updateLsp(trans, id, name, rlb, solicited, false);
155 LOG.debug("LSP {} updated", lsp);
157 removeLsp(trans, id);
158 LOG.debug("LSP {} removed", lsp);
166 private PlspId nextRequest() {
167 return new PlspId(this.requestId++);
171 public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
172 Preconditions.checkArgument(input != null && input.getName() != null & input.getNode() != null && input.getArguments() != null, "Mandatory XML tags are missing.");
173 // Make sure there is no such LSP
174 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName()).build();
175 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
177 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
179 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
180 if (rep.isPresent()) {
181 LOG.debug("Node {} already contains lsp {} at {}", input.getNode(), input.getName(), lsp);
182 return OperationResults.UNSENT.future();
185 final SymbolicPathNameBuilder name = new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(input.getName().getBytes()));
188 final RequestsBuilder rb = new RequestsBuilder();
189 rb.fieldsFrom(input.getArguments());
190 rb.setLspa(new LspaBuilder().setTlvs(
191 new TlvsBuilder().addAugmentation(
192 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2.class,
193 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2Builder().setSymbolicPathName(
194 name.build()).build()).build()).build());
196 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
197 ib.setRequests(ImmutableList.of(rb.build()));
200 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), input.getName(),
201 input.getArguments().getMetadata());
207 public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
208 Preconditions.checkArgument(input != null && input.getName() != null & input.getNode() != null, "Mandatory XML tags are missing.");
209 // Make sure the LSP exists, we need it for PLSP-ID
210 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName()).build();
211 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
213 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
215 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
216 if (!rep.isPresent()) {
217 LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
218 return OperationResults.UNSENT.future();
221 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
222 Preconditions.checkState(ra != null, "Reported LSP reported null from data-store.");
223 Lsp reportedLsp = ra.getLsp();
224 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
226 // Build the request and send it
227 final UpdatesBuilder rb = new UpdatesBuilder();
228 rb.setLsp(new LspBuilder().setRemove(Boolean.TRUE).setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).build());
230 final PcupdMessageBuilder ib = new PcupdMessageBuilder(MESSAGE_HEADER);
231 ib.setUpdates(ImmutableList.of(rb.build()));
232 return sendMessage(new PcupdBuilder().setPcupdMessage(ib.build()).build(), rep.get().getName(), null);
238 public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
239 Preconditions.checkArgument(input != null && input.getName() != null & input.getNode() != null && input.getArguments() != null, "Mandatory XML tags are missing.");
240 // Make sure the LSP exists
241 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName()).build();
242 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
244 return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
246 public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
247 if (!rep.isPresent()) {
248 LOG.warn("Node {} does not contain LSP {}", input.getNode(), input.getName());
249 return OperationResults.UNSENT.future();
252 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
253 Preconditions.checkState(ra != null, "Reported LSP reported null from data-store.");
254 Lsp reportedLsp = ra.getLsp();
255 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
256 Arguments2 args = input.getArguments().getAugmentation(Arguments2.class);
257 Preconditions.checkArgument(args != null, "Input is missing operational tag.");
258 Preconditions.checkArgument(input.getArguments().getEro() != null, "Input is missing ERO object.");
260 // Build the PCUpd request and send it
261 final UpdatesBuilder rb = new UpdatesBuilder();
262 rb.setLsp(new LspBuilder().setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).setOperational(args.isOperational()).build());
263 final PathBuilder pb = new PathBuilder();
264 pb.fieldsFrom(input.getArguments());
265 rb.setPath(pb.build());
266 final PcupdMessageBuilder ub = new PcupdMessageBuilder(MESSAGE_HEADER);
267 ub.setUpdates(ImmutableList.of(rb.build()));
268 return sendMessage(new PcupdBuilder().setPcupdMessage(ub.build()).build(), rep.get().getName(), input.getArguments().getMetadata());
274 public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
275 Preconditions.checkArgument(input != null && input.getName() != null & input.getNode() != null && input.getArguments() != null, "Mandatory XML tags are missing.");
277 final Arguments1 aa = input.getArguments().getAugmentation(Arguments1.class);
279 LOG.warn("Operational status not present in MD-SAL.");
282 op = aa.isOperational();
285 // Make sure the LSP exists
286 final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName()).build();
287 LOG.debug("Checking if LSP {} has operational state {}", lsp, op);
288 final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
290 return Futures.transform(f, new Function<Optional<ReportedLsp>, OperationResult>() {
292 public OperationResult apply(final Optional<ReportedLsp> rep) {
293 if (!rep.isPresent()) {
294 LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
295 return OperationResults.UNSENT;
298 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
300 LOG.warn("Node {} LSP {} does not contain data", input.getNode(), input.getName());
301 return OperationResults.UNSENT;
303 Lsp reportedLsp = ra.getLsp();
304 Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
305 if (reportedLsp.isOperational().equals(op)) {
306 return OperationResults.SUCCESS;
308 return OperationResults.UNSENT;