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