Bump upstreams to 2022.09
[bgpcep.git] / programming / impl / src / main / java / org / opendaylight / bgpcep / programming / impl / ProgrammingServiceImpl.java
1 /*
2  * Copyright (c) 2013 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.programming.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import com.google.common.util.concurrent.SettableFuture;
19 import io.netty.util.Timeout;
20 import io.netty.util.Timer;
21 import java.math.BigInteger;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.concurrent.TimeUnit;
30 import org.checkerframework.checker.lock.qual.GuardedBy;
31 import org.opendaylight.bgpcep.programming.NanotimeUtil;
32 import org.opendaylight.bgpcep.programming.spi.ExecutionResult;
33 import org.opendaylight.bgpcep.programming.spi.Instruction;
34 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
35 import org.opendaylight.bgpcep.programming.spi.SchedulerException;
36 import org.opendaylight.bgpcep.programming.spi.SuccessfulRpcResult;
37 import org.opendaylight.mdsal.binding.api.DataBroker;
38 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
39 import org.opendaylight.mdsal.binding.api.RpcProviderService;
40 import org.opendaylight.mdsal.binding.api.WriteTransaction;
41 import org.opendaylight.mdsal.common.api.CommitInfo;
42 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
43 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
44 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
45 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
46 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CancelInstructionInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CancelInstructionOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CancelInstructionOutputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CleanInstructionsInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CleanInstructionsOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CleanInstructionsOutputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.DeadOnArrival;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.DuplicateInstructionId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionStatus;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionStatusChangedBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionsQueue;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionsQueueBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionsQueueKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.Nanotime;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.ProgrammingService;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.SubmitInstructionInput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.UnknownInstruction;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.UnknownPreconditionId;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.queue.InstructionBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.queue.InstructionKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.status.changed.Details;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.submit.instruction.output.result.failure._case.FailureBuilder;
70 import org.opendaylight.yangtools.concepts.ObjectRegistration;
71 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
72 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
73 import org.opendaylight.yangtools.yang.common.RpcResult;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76
77 final class ProgrammingServiceImpl implements ClusterSingletonService, InstructionScheduler, ProgrammingService {
78     private static final Logger LOG = LoggerFactory.getLogger(ProgrammingServiceImpl.class);
79
80     private final Map<InstructionId, InstructionImpl> insns = new HashMap<>();
81     private final InstanceIdentifier<InstructionsQueue> qid;
82     private final NotificationPublishService notifs;
83     private final ListeningExecutorService executor;
84     private final DataBroker dataProvider;
85     private final Timer timer;
86     private final String instructionId;
87     private final ServiceGroupIdentifier sgi;
88     private final ClusterSingletonServiceRegistration csspReg;
89     private final RpcProviderService rpcProviderRegistry;
90     @GuardedBy("this")
91     private ObjectRegistration<ProgrammingService> reg;
92
93     private final class InstructionPusher implements QueueInstruction {
94         private final InstructionBuilder builder = new InstructionBuilder();
95
96         InstructionPusher(final InstructionId id, final Nanotime deadline) {
97             builder.setDeadline(deadline);
98             builder.setId(id);
99             builder.withKey(new InstructionKey(id));
100             builder.setStatus(InstructionStatus.Queued);
101         }
102
103         @Override
104         public void instructionUpdated(final InstructionStatus status, final Details details) {
105             if (!status.equals(builder.getStatus())) {
106                 builder.setStatus(status);
107
108                 final WriteTransaction wt = dataProvider.newWriteOnlyTransaction();
109                 wt.put(LogicalDatastoreType.OPERATIONAL,
110                         qid.child(
111                                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming
112                                         .rev150720.instruction.queue.Instruction.class,
113                                 new InstructionKey(builder.getId())), builder.build());
114                 wt.commit().addCallback(new FutureCallback<CommitInfo>() {
115                     @Override
116                     public void onSuccess(final CommitInfo result) {
117                         LOG.debug("Instruction Queue {} updated", qid);
118                     }
119
120                     @Override
121                     public void onFailure(final Throwable trw) {
122                         LOG.error("Failed to update Instruction Queue {}", qid, trw);
123                     }
124                 }, MoreExecutors.directExecutor());
125             }
126
127             try {
128                 notifs.putNotification(new InstructionStatusChangedBuilder()
129                         .setId(builder.getId()).setStatus(status).setDetails(details).build());
130             } catch (final InterruptedException e) {
131                 LOG.debug("Failed to publish notification", e);
132             }
133         }
134
135         @Override
136         public void instructionRemoved() {
137             final WriteTransaction wt = dataProvider.newWriteOnlyTransaction();
138             wt.delete(LogicalDatastoreType.OPERATIONAL, qid.child(
139                     org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction
140                             .queue.Instruction.class,
141                     new InstructionKey(builder.getId())));
142             wt.commit().addCallback(new FutureCallback<CommitInfo>() {
143                 @Override
144                 public void onSuccess(final CommitInfo result) {
145                     LOG.debug("Instruction Queue {} removed", qid);
146                 }
147
148                 @Override
149                 public void onFailure(final Throwable trw) {
150                     LOG.error("Failed to remove Instruction Queue {}", qid, trw);
151                 }
152             }, MoreExecutors.directExecutor());
153         }
154     }
155
156     ProgrammingServiceImpl(final DataBroker dataProvider, final NotificationPublishService notifs,
157             final ListeningExecutorService executor, final RpcProviderService rpcProviderRegistry,
158             final ClusterSingletonServiceProvider cssp, final Timer timer, final String instructionId) {
159         this.dataProvider = requireNonNull(dataProvider);
160         this.instructionId = requireNonNull(instructionId);
161         this.notifs = requireNonNull(notifs);
162         this.executor = requireNonNull(executor);
163         this.rpcProviderRegistry = requireNonNull(rpcProviderRegistry);
164         this.timer = requireNonNull(timer);
165         qid = KeyedInstanceIdentifier.builder(InstructionsQueue.class,
166                 new InstructionsQueueKey(this.instructionId)).build();
167         sgi = ServiceGroupIdentifier.create(this.instructionId + "-service-group");
168         LOG.info("Creating Programming Service {}.", sgi.getName());
169         csspReg = cssp.registerClusterSingletonService(this);
170     }
171
172     @Override
173     public synchronized void instantiateServiceInstance() {
174         LOG.info("Instruction Queue service {} instantiated", sgi.getName());
175         reg = rpcProviderRegistry.registerRpcImplementation(ProgrammingService.class, this);
176
177         final WriteTransaction wt = dataProvider.newWriteOnlyTransaction();
178         wt.put(LogicalDatastoreType.OPERATIONAL, qid, new InstructionsQueueBuilder()
179                 .withKey(new InstructionsQueueKey(instructionId)).setInstruction(Map.of()).build());
180         wt.commit().addCallback(new FutureCallback<CommitInfo>() {
181             @Override
182             public void onSuccess(final CommitInfo result) {
183                 LOG.debug("Instruction Queue {} added", qid);
184             }
185
186             @Override
187             public void onFailure(final Throwable trw) {
188                 LOG.error("Failed to add Instruction Queue {}", qid, trw);
189             }
190         }, MoreExecutors.directExecutor());
191     }
192
193     @Override
194     public ServiceGroupIdentifier getIdentifier() {
195         return sgi;
196     }
197
198     @Override
199     public ListenableFuture<RpcResult<CancelInstructionOutput>> cancelInstruction(final CancelInstructionInput input) {
200         return executor.submit(() -> realCancelInstruction(input));
201     }
202
203     @Override
204     public ListenableFuture<RpcResult<CleanInstructionsOutput>> cleanInstructions(final CleanInstructionsInput input) {
205         return executor.submit(() -> realCleanInstructions(input));
206     }
207
208     private synchronized RpcResult<CancelInstructionOutput> realCancelInstruction(final CancelInstructionInput input) {
209         final InstructionImpl instruction = insns.get(input.getId());
210         if (instruction == null) {
211             LOG.debug("Instruction {} not present in the graph", input.getId());
212
213             final CancelInstructionOutput out = new CancelInstructionOutputBuilder()
214                     .setFailure(UnknownInstruction.VALUE).build();
215             return SuccessfulRpcResult.create(out);
216         }
217
218         return SuccessfulRpcResult.create(new CancelInstructionOutputBuilder()
219                 .setFailure(instruction.tryCancel(null)).build());
220     }
221
222     private synchronized RpcResult<CleanInstructionsOutput> realCleanInstructions(final CleanInstructionsInput input) {
223         final Set<InstructionId> failed = new HashSet<>();
224
225         for (final InstructionId id : input.getId()) {
226             // Find the instruction
227             final InstructionImpl instruction = insns.get(id);
228             if (instruction == null) {
229                 LOG.debug("Instruction {} not present in the graph", input.getId());
230                 failed.add(id);
231                 continue;
232             }
233
234             // Check its status
235             switch (instruction.getStatus()) {
236                 case Cancelled:
237                 case Failed:
238                 case Successful:
239                     break;
240                 case Executing:
241                 case Queued:
242                 case Scheduled:
243                 case Unknown:
244                     LOG.debug("Instruction {} cannot be cleaned because of it's in state {}",
245                             id, instruction.getStatus());
246                     failed.add(id);
247                     continue;
248                 default:
249                     break;
250             }
251
252             // The instruction is in a terminal state, we need to just unlink
253             // it from its dependencies and dependents
254             instruction.clean();
255
256             insns.remove(id);
257             LOG.debug("Instruction {} cleaned successfully", id);
258         }
259
260         final CleanInstructionsOutputBuilder ob = new CleanInstructionsOutputBuilder();
261         ob.setUnflushed(failed);
262
263         return SuccessfulRpcResult.create(ob.build());
264     }
265
266     private List<InstructionImpl> checkDependencies(final SubmitInstructionInput input) throws SchedulerException {
267         final List<InstructionImpl> dependencies = collectDependencies(input);
268         // Check if all dependencies are non-failed
269         final Set<InstructionId> unmet = checkIfUnfailed(dependencies);
270         /*
271          *  Some dependencies have failed, declare the request dead-on-arrival
272          *  and fail the operation.
273          */
274         if (!unmet.isEmpty()) {
275             throw new SchedulerException("Instruction's dependencies are already unsuccessful", new FailureBuilder()
276                     .setType(DeadOnArrival.VALUE).setFailedPreconditions(unmet).build());
277         }
278         return dependencies;
279     }
280
281     private List<InstructionImpl> collectDependencies(final SubmitInstructionInput input) throws SchedulerException {
282         final List<InstructionImpl> dependencies = new ArrayList<>();
283         for (final InstructionId pid : input.getPreconditions()) {
284             final InstructionImpl instruction = insns.get(pid);
285             if (instruction == null) {
286                 LOG.info("Instruction {} depends on {}, which is not a known instruction", input.getId(), pid);
287                 throw new SchedulerException("Unknown dependency ID specified",
288                         new FailureBuilder().setType(UnknownPreconditionId.VALUE).build());
289             }
290             dependencies.add(instruction);
291         }
292         return dependencies;
293     }
294
295     private static Set<InstructionId> checkIfUnfailed(final List<InstructionImpl> dependencies) {
296         final Set<InstructionId> unmet = new HashSet<>();
297         for (final InstructionImpl d : dependencies) {
298             switch (d.getStatus()) {
299                 case Cancelled:
300                 case Failed:
301                 case Unknown:
302                     unmet.add(d.getId());
303                     break;
304                 case Executing:
305                 case Queued:
306                 case Scheduled:
307                 case Successful:
308                     break;
309                 default:
310                     break;
311             }
312         }
313         return unmet;
314     }
315
316     @Override
317     public synchronized ListenableFuture<Instruction> scheduleInstruction(final SubmitInstructionInput input) throws
318             SchedulerException {
319         final InstructionId id = input.getId();
320         if (insns.get(id) != null) {
321             LOG.info("Instruction ID {} already present", id);
322             throw new SchedulerException("Instruction ID currently in use",
323                     new FailureBuilder().setType(DuplicateInstructionId.VALUE).build());
324         }
325
326         // First things first: check the deadline
327         final Nanotime now = NanotimeUtil.currentTime();
328         final BigInteger left = input.getDeadline().getValue().toJava().subtract(now.getValue().toJava());
329
330         if (left.compareTo(BigInteger.ZERO) <= 0) {
331             LOG.debug("Instruction {} deadline has already passed by {}ns", id, left);
332             throw new SchedulerException("Instruction arrived after specified deadline",
333                     new FailureBuilder().setType(DeadOnArrival.VALUE).build());
334         }
335
336         // Resolve dependencies
337         final List<InstructionImpl> dependencies = checkDependencies(input);
338
339         /*
340          * All pre-flight checks done are at this point, the following
341          * steps can only fail in catastrophic scenarios (OOM and the
342          * like).
343          */
344
345         // Schedule a timeout for the instruction
346         final Timeout t = timer.newTimeout(timeout -> timeoutInstruction(input.getId()), left.longValue(),
347                 TimeUnit.NANOSECONDS);
348
349         // Put it into the instruction list
350         final SettableFuture<Instruction> ret = SettableFuture.create();
351         final InstructionImpl instruction = new InstructionImpl(new InstructionPusher(id, input.getDeadline()), ret, id,
352                 dependencies, t);
353         insns.put(id, instruction);
354
355         // Attach it into its dependencies
356         for (final InstructionImpl d : dependencies) {
357             d.addDependant(instruction);
358         }
359
360         /*
361          * All done. The next part is checking whether the instruction can
362          * run, which we can figure out after sending out the acknowledgement.
363          * This task should be ingress-weighed, so we reinsert it into the
364          * same execution service.
365          */
366         executor.submit(() -> tryScheduleInstruction(instruction));
367
368         return ret;
369     }
370
371     @Override
372     public String getInstructionID() {
373         return instructionId;
374     }
375
376     private synchronized void timeoutInstruction(final InstructionId id) {
377         final InstructionImpl instruction = insns.get(id);
378         if (instruction == null) {
379             LOG.warn("Instruction {} timed out, but not found in the queue", id);
380             return;
381         }
382
383         instruction.timeout();
384     }
385
386     private synchronized void tryScheduleDependants(final InstructionImpl instruction) {
387         // Walk all dependants and try to schedule them
388         final Iterator<InstructionImpl> it = instruction.getDependants();
389         while (it.hasNext()) {
390             tryScheduleInstruction(it.next());
391         }
392     }
393
394     private synchronized void tryScheduleInstruction(final InstructionImpl instruction) {
395         final ListenableFuture<ExecutionResult<Details>> f = instruction.ready();
396         if (f != null) {
397             Futures.addCallback(f, new FutureCallback<ExecutionResult<Details>>() {
398                 @Override
399                 public void onSuccess(final ExecutionResult<Details> result) {
400                     tryScheduleDependants(instruction);
401                 }
402
403                 @Override
404                 public void onFailure(final Throwable trw) {
405                     LOG.error("Instruction {} failed to execute", instruction.getId(), trw);
406                 }
407             }, MoreExecutors.directExecutor());
408         }
409
410     }
411
412     @Override
413     public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
414         LOG.info("Closing Instruction Queue service {}", sgi.getName());
415
416         if (reg != null) {
417             reg.close();
418             reg = null;
419         }
420         for (final InstructionImpl instruction : insns.values()) {
421             instruction.tryCancel(null);
422         }
423         // Workaround for BUG-2283
424         final WriteTransaction wt = dataProvider.newWriteOnlyTransaction();
425         wt.delete(LogicalDatastoreType.OPERATIONAL, qid);
426
427         final FluentFuture<? extends CommitInfo> future = wt.commit();
428         future.addCallback(new FutureCallback<CommitInfo>() {
429             @Override
430             public void onSuccess(final CommitInfo result) {
431                 LOG.debug("Instruction Queue {} removed", qid);
432             }
433
434             @Override
435             public void onFailure(final Throwable trw) {
436                 LOG.error("Failed to shutdown Instruction Queue {}", qid, trw);
437             }
438         }, MoreExecutors.directExecutor());
439
440         return future;
441     }
442
443     @Override
444     public synchronized void close() {
445         csspReg.close();
446     }
447 }