7eb3dee1e2aa64c289f1ef9d61caf84ad5f5b5eb
[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.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;
18
19 import java.net.InetAddress;
20 import java.nio.ByteBuffer;
21 import java.util.Collections;
22
23 import javax.annotation.concurrent.GuardedBy;
24
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;
67
68 public class Stateful02TopologySessionListener extends AbstractTopologySessionListener<String, PlspId> {
69     private static final Logger LOG = LoggerFactory.getLogger(Stateful02TopologySessionListener.class);
70
71     /**
72      * @param serverSessionManager
73      */
74     Stateful02TopologySessionListener(final ServerSessionManager serverSessionManager) {
75         super(serverSessionManager);
76     }
77
78     @GuardedBy("this")
79     private long requestId = 1;
80
81     @Override
82     protected void onSessionUp(final PCEPSession session, final PathComputationClientBuilder pccBuilder) {
83         final InetAddress peerAddress = session.getRemoteAddress();
84
85         final Tlvs tlvs = session.getRemoteTlvs();
86         final Tlvs2 tlv = tlvs.getAugmentation(Tlvs2.class);
87         if (tlv != null) {
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());
93             } else {
94                 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
95             }
96         } else {
97             LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
98         }
99     }
100
101     @Override
102     protected synchronized boolean onMessage(final WriteTransaction trans, final Message message) {
103         if (!(message instanceof PcrptMessage)) {
104             return true;
105         }
106
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();
110             if (lsp == null) {
111                 LOG.warn("PCRpt message received without LSP object.");
112                 return true;
113             }
114
115             final PlspId id = lsp.getPlspId();
116             if (!lsp.isSync() && (id == null || id.getValue() == 0)) {
117                 stateSynchronizationAchieved(trans);
118                 continue;
119             }
120
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();
122             final String name;
123             if (tlvs != null && tlvs.getSymbolicPathName() != null) {
124                 name = Charsets.UTF_8.decode(ByteBuffer.wrap(tlvs.getSymbolicPathName().getPathName().getValue())).toString();
125             } else {
126                 name = lookupLspName(id);
127             }
128
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());
134             }
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;
139
140             if (id.getValue() != 0) {
141                 solicited = true;
142
143                 final PCEPRequest req = removeRequest(name);
144                 if (req != null) {
145                     LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.isOperational());
146                     rlb.setMetadata(req.getMetadata());
147                     req.setResult(OperationResults.SUCCESS);
148                 } else {
149                     LOG.warn("Request ID {} not found in outstanding DB", id);
150                 }
151             }
152
153             if (!lsp.isRemove()) {
154                 updateLsp(trans, id, name, rlb, solicited, false);
155                 LOG.debug("LSP {} updated", lsp);
156             } else {
157                 removeLsp(trans, id);
158                 LOG.debug("LSP {} removed", lsp);
159             }
160         }
161
162         return false;
163     }
164
165     @GuardedBy("this")
166     private PlspId nextRequest() {
167         return new PlspId(this.requestId++);
168     }
169
170     @Override
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);
176
177         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
178             @Override
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();
183                 }
184
185                 final SymbolicPathNameBuilder name = new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(input.getName().getBytes()));
186
187                 // Build the request
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());
195
196                 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
197                 ib.setRequests(ImmutableList.of(rb.build()));
198
199                 // Send the message
200                 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), input.getName(),
201                         input.getArguments().getMetadata());
202             }
203         });
204     }
205
206     @Override
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);
212
213         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
214             @Override
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();
219                 }
220
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.");
225
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());
229
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);
233             }
234         });
235     }
236
237     @Override
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);
243
244         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
245             @Override
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();
250                 }
251
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.");
259
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());
269             }
270         });
271     }
272
273     @Override
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.");
276         final Boolean op;
277         final Arguments1 aa = input.getArguments().getAugmentation(Arguments1.class);
278         if (aa == null) {
279             LOG.warn("Operational status not present in MD-SAL.");
280             op = null;
281         } else {
282             op = aa.isOperational();
283         }
284
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);
289
290         return Futures.transform(f, new Function<Optional<ReportedLsp>, OperationResult>() {
291             @Override
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;
296                 }
297
298                 final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
299                 if (ra == null) {
300                     LOG.warn("Node {} LSP {} does not contain data", input.getNode(), input.getName());
301                     return OperationResults.UNSENT;
302                 }
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;
307                 } else {
308                     return OperationResults.UNSENT;
309                 }
310             }
311         });
312     }
313 }