4a70ebb59d6e9e8823ffde2084582c4a2b8dcdfd
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / Stateful02TopologySessionListener.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.bgpcep.pcep.topology.provider;
9
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;
21
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;
63
64 public class Stateful02TopologySessionListener extends AbstractTopologySessionListener<String, PlspId> {
65     private static final Logger LOG = LoggerFactory.getLogger(Stateful02TopologySessionListener.class);
66
67     /**
68      * @param serverSessionManager
69      */
70     Stateful02TopologySessionListener(final ServerSessionManager serverSessionManager) {
71         super(serverSessionManager);
72     }
73
74     @GuardedBy("this")
75     private long requestId = 1;
76
77     @Override
78     protected void onSessionUp(final PCEPSession session, final PathComputationClientBuilder pccBuilder) {
79         final InetAddress peerAddress = session.getRemoteAddress();
80
81         final Tlvs tlvs = session.getRemoteTlvs();
82         final Tlvs2 tlv = tlvs.getAugmentation(Tlvs2.class);
83         if (tlv != null) {
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());
89             } else {
90                 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
91             }
92         } else {
93             LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
94         }
95     }
96
97     @Override
98     protected synchronized boolean onMessage(final MessageContext ctx, final Message message) {
99         if (!(message instanceof PcrptMessage)) {
100             return true;
101         }
102
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();
106             if (lsp == null) {
107                 LOG.warn("PCRpt message received without LSP object.");
108                 return true;
109             }
110
111             final PlspId id = lsp.getPlspId();
112             if (!lsp.isSync() && (id == null || id.getValue() == 0)) {
113                 stateSynchronizationAchieved(ctx);
114                 continue;
115             }
116
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();
118             final String name;
119             if (tlvs != null && tlvs.getSymbolicPathName() != null) {
120                 name = Charsets.UTF_8.decode(ByteBuffer.wrap(tlvs.getSymbolicPathName().getPathName().getValue())).toString();
121             } else {
122                 name = lookupLspName(id);
123             }
124
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());
130             }
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;
135
136             if (id.getValue() != 0) {
137                 solicited = true;
138
139                 final PCEPRequest req = removeRequest(name);
140                 if (req != null) {
141                     LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.isOperational());
142                     rlb.setMetadata(req.getMetadata());
143                     ctx.resolveRequest(req);
144                 } else {
145                     LOG.warn("Request ID {} not found in outstanding DB", id);
146                 }
147             }
148
149             if (!lsp.isRemove()) {
150                 updateLsp(ctx, id, name, rlb, solicited, false);
151                 LOG.debug("LSP {} updated", lsp);
152             } else {
153                 removeLsp(ctx, id);
154                 LOG.debug("LSP {} removed", lsp);
155             }
156         }
157
158         return false;
159     }
160
161     @GuardedBy("this")
162     private PlspId nextRequest() {
163         return new PlspId(this.requestId++);
164     }
165
166     @Override
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);
172
173         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
174             @Override
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();
179                 }
180
181                 final SymbolicPathNameBuilder name = new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(input.getName().getBytes()));
182
183                 // Build the request
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());
191
192                 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
193                 ib.setRequests(Collections.singletonList(rb.build()));
194
195                 // Send the message
196                 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), input.getName(),
197                         input.getArguments().getMetadata());
198             }
199         });
200     }
201
202     @Override
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);
208
209         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
210             @Override
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();
215                 }
216
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.");
221
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());
225
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);
229             }
230         });
231     }
232
233     @Override
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);
239
240         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
241             @Override
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();
246                 }
247
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.");
255
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());
265             }
266         });
267     }
268
269     @Override
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.");
272         final Boolean op;
273         final Arguments1 aa = input.getArguments().getAugmentation(Arguments1.class);
274         if (aa == null) {
275             LOG.warn("Operational status not present in MD-SAL.");
276             op = null;
277         } else {
278             op = aa.isOperational();
279         }
280
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);
285
286         return Futures.transform(f, new Function<Optional<ReportedLsp>, OperationResult>() {
287             @Override
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;
292                 }
293
294                 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
295                 if (ra == null) {
296                     LOG.warn("Node {} LSP {} does not contain data", input.getNode(), input.getName());
297                     return OperationResults.UNSENT;
298                 }
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;
303                 } else {
304                     return OperationResults.UNSENT;
305                 }
306             }
307         });
308     }
309 }