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