ecbd986a7aa8ccc785145900174fdfe1bf333558
[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 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.topology.pcep.rev131024.AddLspArgs;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.LspId;
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;
61
62 public class Stateful02TopologySessionListener extends AbstractTopologySessionListener<String, PlspId> {
63     private static final Logger LOG = LoggerFactory.getLogger(Stateful02TopologySessionListener.class);
64
65     /**
66      * @param serverSessionManager
67      */
68     Stateful02TopologySessionListener(final ServerSessionManager serverSessionManager) {
69         super(serverSessionManager);
70     }
71
72     @Override
73     protected void onSessionUp(final PCEPSession session, final PathComputationClientBuilder pccBuilder) {
74         final InetAddress peerAddress = session.getRemoteAddress();
75
76         final Tlvs tlvs = session.getRemoteTlvs();
77         final Tlvs2 tlv = tlvs.getAugmentation(Tlvs2.class);
78         if (tlv != null) {
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());
84             } else {
85                 LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
86             }
87         } else {
88             LOG.debug("Peer {} does not advertise stateful TLV", peerAddress);
89         }
90     }
91
92     @Override
93     protected synchronized boolean onMessage(final MessageContext ctx, final Message message) {
94         if (!(message instanceof PcrptMessage)) {
95             return true;
96         }
97
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();
101             if (lsp == null) {
102                 LOG.warn("PCRpt message received without LSP object.");
103                 return true;
104             }
105
106             final PlspId id = lsp.getPlspId();
107             if (!lsp.isSync() && (id == null || id.getValue() == 0)) {
108                 stateSynchronizationAchieved(ctx);
109                 continue;
110             }
111
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();
113             final String name = (tlvs != null && tlvs.getSymbolicPathName() != null) ?
114                 Charsets.UTF_8.decode(ByteBuffer.wrap(tlvs.getSymbolicPathName().getPathName().getValue())).toString()
115                 : lookupLspName(id);
116
117             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();
118             // set 0 by default, as the first report does not contain LSPIdentifiersTLV
119             pb.setLspId(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId(0L));
120             if (r.getPath() != null) {
121                 pb.fieldsFrom(r.getPath());
122             }
123
124             final ReportedLspBuilder rlb = new ReportedLspBuilder();
125             rlb.addAugmentation(ReportedLsp1.class, new ReportedLsp1Builder().setLsp(lsp).build());
126             rlb.setPath(Collections.singletonList(pb.build()));
127             boolean solicited = false;
128
129             if (id.getValue() != 0) {
130                 solicited = true;
131                 final PCEPRequest req = removeRequest(name);
132                 if (req != null) {
133                     LOG.debug("Request {} resulted in LSP operational state {}", id, lsp.isOperational());
134                     rlb.setMetadata(req.getMetadata());
135                     ctx.resolveRequest(req);
136                 } else {
137                     LOG.warn("Request ID {} not found in outstanding DB", id);
138                 }
139             }
140
141             if (!lsp.isRemove()) {
142                 updateLsp(ctx, id, name, rlb, solicited, false);
143                 LOG.debug("LSP {} updated", lsp);
144             } else {
145                 removeLsp(ctx, id);
146                 LOG.debug("LSP {} removed", lsp);
147             }
148         }
149
150         return false;
151     }
152
153     @Override
154     public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
155         Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, MISSING_XML_TAG);
156         // Make sure there is no such LSP
157         final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
158         final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
159
160         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
161             @Override
162             public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
163                 if (rep.isPresent()) {
164                     LOG.debug("Node {} already contains lsp {} at {}", input.getNode(), input.getName(), lsp);
165                     return OperationResults.UNSENT.future();
166                 }
167
168                 final SymbolicPathNameBuilder name = new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(input.getName().getBytes(Charsets.UTF_8)));
169
170                 // Build the request
171                 final RequestsBuilder rb = new RequestsBuilder();
172                 rb.fieldsFrom(input.getArguments());
173                 rb.setLspa(new LspaBuilder().setTlvs(
174                     new TlvsBuilder().addAugmentation(
175                         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2.class,
176                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2Builder().setSymbolicPathName(
177                             name.build()).build()).build()).build());
178
179                 final PcinitiateMessageBuilder ib = new PcinitiateMessageBuilder(MESSAGE_HEADER);
180                 ib.setRequests(Collections.singletonList(rb.build()));
181
182                 // Send the message
183                 return sendMessage(new PcinitiateBuilder().setPcinitiateMessage(ib.build()).build(), input.getName(),
184                     input.getArguments().getMetadata());
185             }
186         });
187     }
188
189     @Override
190     public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
191         Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null, MISSING_XML_TAG);
192         // Make sure the LSP exists, we need it for PLSP-ID
193         final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
194         final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
195
196         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
197             @Override
198             public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
199                 final Lsp reportedLsp = validateReportedLsp(rep, input);
200                 if (reportedLsp == null) {
201                     LOG.warn("Reported LSP does not contain LSP object.");
202                     return OperationResults.UNSENT.future();
203                 }
204                 // Build the request and send it
205                 final UpdatesBuilder rb = new UpdatesBuilder();
206                 rb.setLsp(new LspBuilder().setRemove(Boolean.TRUE).setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).build());
207                 final PcupdMessageBuilder ib = new PcupdMessageBuilder(MESSAGE_HEADER);
208                 ib.setUpdates(Collections.singletonList(rb.build()));
209                 return sendMessage(new PcupdBuilder().setPcupdMessage(ib.build()).build(), rep.get().getName(), null);
210             }
211         });
212     }
213
214     @Override
215     public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
216         Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, MISSING_XML_TAG);
217         // Make sure the LSP exists
218         final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
219         final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
220
221         return Futures.transform(f, new AsyncFunction<Optional<ReportedLsp>, OperationResult>() {
222             @Override
223             public ListenableFuture<OperationResult> apply(final Optional<ReportedLsp> rep) {
224                 final Lsp reportedLsp = validateReportedLsp(rep, input);
225                 if (reportedLsp == null) {
226                     return OperationResults.UNSENT.future();
227                 }
228                 final Arguments2 args = input.getArguments().getAugmentation(Arguments2.class);
229                 Preconditions.checkArgument(args != null, "Input is missing operational tag.");
230                 Preconditions.checkArgument(input.getArguments().getEro() != null, "Input is missing ERO object.");
231                 // Build the PCUpd request and send it
232                 final UpdatesBuilder rb = new UpdatesBuilder();
233                 rb.setLsp(new LspBuilder().setPlspId(reportedLsp.getPlspId()).setDelegate(reportedLsp.isDelegate()).setOperational(args.isOperational()).build());
234                 final PathBuilder pb = new PathBuilder();
235                 pb.fieldsFrom(input.getArguments());
236                 rb.setPath(pb.build());
237                 final PcupdMessageBuilder ub = new PcupdMessageBuilder(MESSAGE_HEADER);
238                 ub.setUpdates(Collections.singletonList(rb.build()));
239                 return sendMessage(new PcupdBuilder().setPcupdMessage(ub.build()).build(), rep.get().getName(), input.getArguments().getMetadata());
240             }
241         });
242     }
243
244     @Override
245     public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
246         Preconditions.checkArgument(input != null && input.getName() != null && input.getNode() != null && input.getArguments() != null, MISSING_XML_TAG);
247         final Boolean op;
248         final Arguments1 aa = input.getArguments().getAugmentation(Arguments1.class);
249         if (aa == null) {
250             LOG.warn("Operational status not present in MD-SAL.");
251             op = null;
252         } else {
253             op = aa.isOperational();
254         }
255
256         // Make sure the LSP exists
257         final InstanceIdentifier<ReportedLsp> lsp = lspIdentifier(input.getName());
258         LOG.debug("Checking if LSP {} has operational state {}", lsp, op);
259         final ListenableFuture<Optional<ReportedLsp>> f = readOperationalData(lsp);
260
261         return Futures.transform(f, new Function<Optional<ReportedLsp>, OperationResult>() {
262             @Override
263             public OperationResult apply(final Optional<ReportedLsp> rep) {
264                 final Lsp reportedLsp = validateReportedLsp(rep, input);
265                 if (reportedLsp == null) {
266                     return OperationResults.UNSENT;
267                 }
268                 return reportedLsp.isOperational().equals(op) ? OperationResults.SUCCESS : OperationResults.UNSENT;
269             }
270         });
271     }
272
273     @Override
274     protected Lsp validateReportedLsp(final Optional<ReportedLsp> rep, final LspId input) {
275         if (!rep.isPresent()) {
276             LOG.debug("Node {} does not contain LSP {}", input.getNode(), input.getName());
277             return null;
278         }
279         final ReportedLsp1 ra = rep.get().getAugmentation(ReportedLsp1.class);
280         if (ra == null) {
281             LOG.warn("Reported LSP reported null from data-store.");
282             return null;
283         }
284         final Lsp reportedLsp = ra.getLsp();
285         Preconditions.checkState(reportedLsp != null, "Reported LSP does not contain LSP object.");
286         return reportedLsp;
287     }
288 }