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