079086d09ec73867cac554c51c9a38647e20f343
[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 com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.SettableFuture;
16 import io.netty.util.Timeout;
17 import io.netty.util.Timer;
18 import io.netty.util.TimerTask;
19 import java.math.BigInteger;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.concurrent.Callable;
27 import java.util.concurrent.TimeUnit;
28 import org.opendaylight.bgpcep.programming.NanotimeUtil;
29 import org.opendaylight.bgpcep.programming.spi.ExecutionResult;
30 import org.opendaylight.bgpcep.programming.spi.Instruction;
31 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
32 import org.opendaylight.bgpcep.programming.spi.SchedulerException;
33 import org.opendaylight.bgpcep.programming.spi.SuccessfulRpcResult;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
36 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
37 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CancelInstructionInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CancelInstructionOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CancelInstructionOutputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CleanInstructionsInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CleanInstructionsOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.CleanInstructionsOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.DeadOnArrival;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.DuplicateInstructionId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionStatus;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionStatusChangedBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionsQueue;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionsQueueBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionsQueueKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.Nanotime;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.ProgrammingService;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.SubmitInstructionInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.UnknownInstruction;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.UnknownPreconditionId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.queue.InstructionBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.queue.InstructionKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.status.changed.Details;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.submit.instruction.output.result.failure._case.FailureBuilder;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
63 import org.opendaylight.yangtools.yang.common.RpcResult;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 public final class ProgrammingServiceImpl implements AutoCloseable, InstructionScheduler, ProgrammingService {
68     private static final Logger LOG = LoggerFactory.getLogger(ProgrammingServiceImpl.class);
69
70     private final Map<InstructionId, InstructionImpl> insns = new HashMap<>();
71     private final InstanceIdentifier<InstructionsQueue> qid;
72     private final NotificationProviderService notifs;
73     private final ListeningExecutorService executor;
74     private final DataBroker dataProvider;
75     private final Timer timer;
76
77     private final class InstructionPusher implements QueueInstruction {
78         private final InstructionBuilder builder = new InstructionBuilder();
79
80         InstructionPusher(final InstructionId id, final Nanotime deadline) {
81             this.builder.setDeadline(deadline);
82             this.builder.setId(id);
83             this.builder.setKey(new InstructionKey(id));
84             this.builder.setStatus(InstructionStatus.Queued);
85         }
86
87         @Override
88         public void instructionUpdated(final InstructionStatus status, final Details details) {
89             if (!status.equals(this.builder.getStatus())) {
90                 this.builder.setStatus(status);
91
92                 final WriteTransaction t = ProgrammingServiceImpl.this.dataProvider.newWriteOnlyTransaction();
93                 t.put(LogicalDatastoreType.OPERATIONAL,
94                         ProgrammingServiceImpl.this.qid.child(
95                                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.queue.Instruction.class,
96                                 new InstructionKey(this.builder.getId())), this.builder.build());
97                 t.submit();
98             }
99
100             ProgrammingServiceImpl.this.notifs.publish(new InstructionStatusChangedBuilder().setId(this.builder.getId()).setStatus(status).setDetails(details).build());
101         }
102
103         @Override
104         public void instructionRemoved() {
105             final WriteTransaction t = ProgrammingServiceImpl.this.dataProvider.newWriteOnlyTransaction();
106             t.delete(LogicalDatastoreType.OPERATIONAL, ProgrammingServiceImpl.this.qid.child(
107                     org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.queue.Instruction.class,
108                     new InstructionKey(this.builder.getId())));
109             t.submit();
110         }
111     }
112
113     public ProgrammingServiceImpl(final DataBroker dataProvider, final NotificationProviderService notifs,
114             final ListeningExecutorService executor, final Timer timer, final InstructionsQueueKey instructionsQueueKey) {
115         this.dataProvider = Preconditions.checkNotNull(dataProvider);
116         this.notifs = Preconditions.checkNotNull(notifs);
117         this.executor = Preconditions.checkNotNull(executor);
118         this.timer = Preconditions.checkNotNull(timer);
119         this.qid = KeyedInstanceIdentifier.builder(InstructionsQueue.class, instructionsQueueKey).build();
120
121         final WriteTransaction t = dataProvider.newWriteOnlyTransaction();
122         t.put(LogicalDatastoreType.OPERATIONAL, this.qid,
123                 new InstructionsQueueBuilder().setKey(instructionsQueueKey).setInstruction(
124                         Collections.<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.queue.Instruction> emptyList()).build());
125         t.submit();
126     }
127
128     @Override
129     public ListenableFuture<RpcResult<CancelInstructionOutput>> cancelInstruction(final CancelInstructionInput input) {
130         return this.executor.submit(new Callable<RpcResult<CancelInstructionOutput>>() {
131             @Override
132             public RpcResult<CancelInstructionOutput> call() {
133                 return realCancelInstruction(input);
134             }
135         });
136     }
137
138     @Override
139     public ListenableFuture<RpcResult<CleanInstructionsOutput>> cleanInstructions(final CleanInstructionsInput input) {
140         return this.executor.submit(new Callable<RpcResult<CleanInstructionsOutput>>() {
141             @Override
142             public RpcResult<CleanInstructionsOutput> call() {
143                 return realCleanInstructions(input);
144             }
145         });
146     }
147
148     private synchronized RpcResult<CancelInstructionOutput> realCancelInstruction(final CancelInstructionInput input) {
149         final InstructionImpl i = this.insns.get(input.getId());
150         if (i == null) {
151             LOG.debug("Instruction {} not present in the graph", input.getId());
152
153             final CancelInstructionOutput out = new CancelInstructionOutputBuilder().setFailure(UnknownInstruction.class).build();
154             return SuccessfulRpcResult.create(out);
155         }
156
157         return SuccessfulRpcResult.create(new CancelInstructionOutputBuilder().setFailure(i.tryCancel(null)).build());
158     }
159
160     private synchronized RpcResult<CleanInstructionsOutput> realCleanInstructions(final CleanInstructionsInput input) {
161         final List<InstructionId> failed = new ArrayList<>();
162
163         for (final InstructionId id : input.getId()) {
164             // Find the instruction
165             final InstructionImpl i = this.insns.get(id);
166             if (i == null) {
167                 LOG.debug("Instruction {} not present in the graph", input.getId());
168                 failed.add(id);
169                 continue;
170             }
171
172             // Check its status
173             switch (i.getStatus()) {
174             case Cancelled:
175             case Failed:
176             case Successful:
177                 break;
178             case Executing:
179             case Queued:
180             case Scheduled:
181             case Unknown:
182                 LOG.debug("Instruction {} cannot be cleaned because of it's in state {}", id, i.getStatus());
183                 failed.add(id);
184                 continue;
185             default:
186                 break;
187             }
188
189             // The instruction is in a terminal state, we need to just unlink
190             // it from its dependencies and dependents
191             i.clean();
192
193             this.insns.remove(id);
194             LOG.debug("Instruction {} cleaned successfully", id);
195         }
196
197         final CleanInstructionsOutputBuilder ob = new CleanInstructionsOutputBuilder();
198         ob.setUnflushed(failed);
199
200         return SuccessfulRpcResult.create(ob.build());
201     }
202
203     private List<InstructionImpl> checkDependencies(final SubmitInstructionInput input) throws SchedulerException {
204         final List<InstructionImpl> dependencies = collectDependencies(input);
205         // Check if all dependencies are non-failed
206         final List<InstructionId> unmet = checkIfUnfailed(dependencies);
207         /*
208          *  Some dependencies have failed, declare the request dead-on-arrival
209          *  and fail the operation.
210          */
211         if (!unmet.isEmpty()) {
212             throw new SchedulerException("Instruction's dependencies are already unsuccessful", new FailureBuilder().setType(
213                     DeadOnArrival.class).setFailedPreconditions(unmet).build());
214         }
215         return dependencies;
216     }
217
218     private List<InstructionImpl> collectDependencies(final SubmitInstructionInput input) throws SchedulerException {
219         final List<InstructionImpl> dependencies = new ArrayList<>();
220         for (final InstructionId pid : input.getPreconditions()) {
221             final InstructionImpl i = this.insns.get(pid);
222             if (i == null) {
223                 LOG.info("Instruction {} depends on {}, which is not a known instruction", input.getId(), pid);
224                 throw new SchedulerException("Unknown dependency ID specified", new FailureBuilder().setType(UnknownPreconditionId.class).build());
225             }
226             dependencies.add(i);
227         }
228         return dependencies;
229     }
230
231     private List<InstructionId> checkIfUnfailed(final List<InstructionImpl> dependencies) {
232         final List<InstructionId> unmet = new ArrayList<>();
233         for (final InstructionImpl d : dependencies) {
234             switch (d.getStatus()) {
235             case Cancelled:
236             case Failed:
237             case Unknown:
238                 unmet.add(d.getId());
239                 break;
240             case Executing:
241             case Queued:
242             case Scheduled:
243             case Successful:
244                 break;
245             default:
246                 break;
247             }
248         }
249         return unmet;
250     }
251
252     @Override
253     public synchronized ListenableFuture<Instruction> scheduleInstruction(final SubmitInstructionInput input) throws SchedulerException {
254         final InstructionId id = input.getId();
255         if (this.insns.get(id) != null) {
256             LOG.info("Instruction ID {} already present", id);
257             throw new SchedulerException("Instruction ID currently in use", new FailureBuilder().setType(DuplicateInstructionId.class).build());
258         }
259
260         // First things first: check the deadline
261         final Nanotime now = NanotimeUtil.currentTime();
262         final BigInteger left = input.getDeadline().getValue().subtract(now.getValue());
263
264         if (left.compareTo(BigInteger.ZERO) <= 0) {
265             LOG.debug("Instruction {} deadline has already passed by {}ns", id, left);
266             throw new SchedulerException("Instruction arrived after specified deadline", new FailureBuilder().setType(DeadOnArrival.class).build());
267         }
268
269         // Resolve dependencies
270         final List<InstructionImpl> dependencies = checkDependencies(input);
271
272         /*
273          * All pre-flight checks done are at this point, the following
274          * steps can only fail in catastrophic scenarios (OOM and the
275          * like).
276          */
277
278         // Schedule a timeout for the instruction
279         final Timeout t = this.timer.newTimeout(new TimerTask() {
280             @Override
281             public void run(final Timeout timeout) {
282                 timeoutInstruction(input.getId());
283             }
284         }, left.longValue(), TimeUnit.NANOSECONDS);
285
286         // Put it into the instruction list
287         final SettableFuture<Instruction> ret = SettableFuture.create();
288         final InstructionImpl i = new InstructionImpl(new InstructionPusher(id, input.getDeadline()), ret, id, dependencies, t);
289         this.insns.put(id, i);
290
291         // Attach it into its dependencies
292         for (final InstructionImpl d : dependencies) {
293             d.addDependant(i);
294         }
295
296         /*
297          * All done. The next part is checking whether the instruction can
298          * run, which we can figure out after sending out the acknowledgement.
299          * This task should be ingress-weighed, so we reinsert it into the
300          * same execution service.
301          */
302         this.executor.submit(new Runnable() {
303             @Override
304             public void run() {
305                 tryScheduleInstruction(i);
306             }
307         });
308
309         return ret;
310     }
311
312     private synchronized void timeoutInstruction(final InstructionId id) {
313         final InstructionImpl i = this.insns.get(id);
314         if (i == null) {
315             LOG.warn("Instruction {} timed out, but not found in the queue", id);
316             return;
317         }
318
319         i.timeout();
320     }
321
322     private synchronized void tryScheduleDependants(final InstructionImpl i) {
323         // Walk all dependants and try to schedule them
324         final Iterator<InstructionImpl> it = i.getDependants();
325         while (it.hasNext()) {
326             tryScheduleInstruction(it.next());
327         }
328     }
329
330     private synchronized void tryScheduleInstruction(final InstructionImpl i) {
331         final ListenableFuture<ExecutionResult<Details>> f = i.ready();
332         if (f != null) {
333             Futures.addCallback(f, new FutureCallback<ExecutionResult<Details>>() {
334                 @Override
335                 public void onSuccess(final ExecutionResult<Details> result) {
336                     tryScheduleDependants(i);
337                 }
338
339                 @Override
340                 public void onFailure(final Throwable t) {
341                     LOG.error("Instruction {} failed to execute", i.getId(), t);
342                 }
343             });
344         }
345
346     }
347
348     @Override
349     public synchronized void close() {
350         try {
351             for (final InstructionImpl i : this.insns.values()) {
352                 i.tryCancel(null);
353             }
354             // Workaround for BUG-2283
355             final WriteTransaction t = this.dataProvider.newWriteOnlyTransaction();
356             t.delete(LogicalDatastoreType.OPERATIONAL, this.qid);
357             t.submit().checkedGet();
358         } catch (final Exception e) {
359             LOG.error("Failed to shutdown Instruction Queue", e);
360         }
361     }
362 }