2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.bgpcep.programming.impl;
10 import io.netty.util.Timeout;
11 import io.netty.util.Timer;
12 import io.netty.util.TimerTask;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.TimeUnit;
24 import org.opendaylight.bgpcep.programming.NanotimeUtil;
25 import org.opendaylight.bgpcep.programming.spi.ExecutionResult;
26 import org.opendaylight.bgpcep.programming.spi.Instruction;
27 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
28 import org.opendaylight.bgpcep.programming.spi.SchedulerException;
29 import org.opendaylight.bgpcep.programming.spi.SuccessfulRpcResult;
30 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
31 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
32 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.CancelInstructionInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.CancelInstructionOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.CancelInstructionOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.CleanInstructionsInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.CleanInstructionsOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.CleanInstructionsOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.DeadOnArrival;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.DuplicateInstructionId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionQueue;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionQueueBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionStatus;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionStatusChangedBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.Nanotime;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.ProgrammingService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.SubmitInstructionInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.UnknownInstruction;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.UnknownPreconditionId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.queue.InstructionKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.queue.InstructionsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.queue.InstructionsKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.status.changed.Details;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.submit.instruction.output.result.failure._case.FailureBuilder;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.opendaylight.yangtools.yang.common.RpcResult;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 import com.google.common.base.Preconditions;
62 import com.google.common.util.concurrent.FutureCallback;
63 import com.google.common.util.concurrent.Futures;
64 import com.google.common.util.concurrent.ListenableFuture;
65 import com.google.common.util.concurrent.ListeningExecutorService;
66 import com.google.common.util.concurrent.SettableFuture;
68 public final class ProgrammingServiceImpl implements AutoCloseable, InstructionScheduler, ProgrammingService {
69 private static final Logger LOG = LoggerFactory.getLogger(ProgrammingServiceImpl.class);
71 private final Map<InstructionId, InstructionImpl> insns = new HashMap<>();
72 private final InstanceIdentifier<InstructionQueue> qid;
73 private final NotificationProviderService notifs;
74 private final ListeningExecutorService executor;
75 private final DataProviderService dataProvider;
76 private final Timer timer;
78 private final class InstructionPusher implements QueueInstruction {
79 private final InstructionsBuilder builder = new InstructionsBuilder();
81 InstructionPusher(final InstructionId id, final Nanotime deadline) {
82 builder.setDeadline(deadline);
84 builder.setKey(new InstructionsKey(id));
85 builder.setStatus(InstructionStatus.Queued);
89 public void instructionUpdated(final InstructionStatus status, final Details details) {
90 if (!status.equals(builder.getStatus())) {
91 builder.setStatus(status);
93 final DataModificationTransaction t = dataProvider.beginTransaction();
94 t.putOperationalData(InstanceIdentifier.builder(qid).child(
95 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.queue.Instruction.class,
96 new InstructionKey(builder.getId())).build(), builder.build());
100 notifs.publish(new InstructionStatusChangedBuilder().setId(builder.getId()).setStatus(status).setDetails(details).build());
104 public void instructionRemoved() {
105 final DataModificationTransaction t = dataProvider.beginTransaction();
106 t.removeOperationalData(InstanceIdentifier.builder(qid).child(
107 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.queue.Instruction.class,
108 new InstructionKey(builder.getId())).build());
113 public ProgrammingServiceImpl(final DataProviderService dataProvider, final NotificationProviderService notifs, final ListeningExecutorService executor, final Timer timer) {
114 this.dataProvider = Preconditions.checkNotNull(dataProvider);
115 this.notifs = Preconditions.checkNotNull(notifs);
116 this.executor = Preconditions.checkNotNull(executor);
117 this.timer = Preconditions.checkNotNull(timer);
118 qid = InstanceIdentifier.builder(InstructionQueue.class).toInstance();
120 final DataModificationTransaction t = dataProvider.beginTransaction();
121 Preconditions.checkState(t.readOperationalData(qid) == null, "Conflicting instruction queue found");
123 t.putOperationalData(qid, new InstructionQueueBuilder().setInstruction(Collections.<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.queue.Instruction>emptyList()).build());
128 public ListenableFuture<RpcResult<CancelInstructionOutput>> cancelInstruction(final CancelInstructionInput input) {
129 return this.executor.submit(new Callable<RpcResult<CancelInstructionOutput>>() {
131 public RpcResult<CancelInstructionOutput> call() {
132 return realCancelInstruction(input);
138 public ListenableFuture<RpcResult<CleanInstructionsOutput>> cleanInstructions(final CleanInstructionsInput input) {
139 return this.executor.submit(new Callable<RpcResult<CleanInstructionsOutput>>() {
141 public RpcResult<CleanInstructionsOutput> call() {
142 return realCleanInstructions(input);
147 private synchronized RpcResult<CancelInstructionOutput> realCancelInstruction(final CancelInstructionInput input) {
148 final InstructionImpl i = this.insns.get(input.getId());
150 LOG.debug("Instruction {} not present in the graph", input.getId());
152 final CancelInstructionOutput out = new CancelInstructionOutputBuilder().setFailure(UnknownInstruction.class).build();
153 return SuccessfulRpcResult.create(out);
156 return SuccessfulRpcResult.create(new CancelInstructionOutputBuilder().setFailure(i.tryCancel(null)).build());
160 private synchronized RpcResult<CleanInstructionsOutput> realCleanInstructions(final CleanInstructionsInput input) {
161 final List<InstructionId> failed = new ArrayList<>();
163 for (final InstructionId id : input.getId()) {
164 // Find the instruction
165 final InstructionImpl i = this.insns.get(id);
167 LOG.debug("Instruction {} not present in the graph", input.getId());
173 switch (i.getStatus()) {
182 LOG.debug("Instruction {} cannot be cleaned because of it's in state {}", id, i.getStatus());
187 // The instruction is in a terminal state, we need to just unlink
188 // it from its dependencies and dependants
192 LOG.debug("Instruction {} cleaned successfully", id);
195 final CleanInstructionsOutputBuilder ob = new CleanInstructionsOutputBuilder();
196 if (!failed.isEmpty()) {
197 ob.setUnflushed(failed);
200 return SuccessfulRpcResult.create(ob.build());
204 public synchronized ListenableFuture<Instruction> scheduleInstruction(final SubmitInstructionInput input) throws SchedulerException {
205 final InstructionId id = input.getId();
206 if (this.insns.get(id) != null) {
207 LOG.info("Instruction ID {} already present", id);
208 throw new SchedulerException("Instruction ID currently in use", new FailureBuilder().setType(DuplicateInstructionId.class).build());
211 // First things first: check the deadline
212 final Nanotime now = NanotimeUtil.currentTime();
213 final BigInteger left = input.getDeadline().getValue().subtract(now.getValue());
215 if (left.compareTo(BigInteger.ZERO) <= 0) {
216 LOG.debug("Instruction {} deadline has already passed by {}ns", id, left);
217 throw new SchedulerException("Instruction arrived after specified deadline", new FailureBuilder().setType(DeadOnArrival.class).build());
220 // Resolve dependencies
221 final List<InstructionImpl> dependencies = new ArrayList<>();
222 for (final InstructionId pid : input.getPreconditions()) {
223 final InstructionImpl i = this.insns.get(pid);
225 LOG.info("Instruction {} depends on {}, which is not a known instruction", id, pid);
226 throw new SchedulerException("Unknown dependency ID specified", new FailureBuilder().setType(UnknownPreconditionId.class).build());
232 // Check if all dependencies are non-failed
233 final List<InstructionId> unmet = new ArrayList<>();
234 for (final InstructionImpl d : dependencies) {
235 switch (d.getStatus()) {
239 unmet.add(d.getId());
250 * Some dependencies have failed, declare the request dead-on-arrival
251 * and fail the operation.
253 if (!unmet.isEmpty()) {
254 throw new SchedulerException("Instruction's dependencies are already unsuccessful", new FailureBuilder().setType(DeadOnArrival.class).setFailedPreconditions(unmet).build());
258 * All pre-flight checks done are at this point, the following
259 * steps can only fail in catastrophic scenarios (OOM and the
263 // Schedule a timeout for the instruction
264 final Timeout t = this.timer.newTimeout(new TimerTask() {
266 public void run(final Timeout timeout) {
267 timeoutInstruction(input.getId());
269 }, left.longValue(), TimeUnit.NANOSECONDS);
271 // Put it into the instruction list
272 final SettableFuture<Instruction> ret = SettableFuture.create();
273 final InstructionImpl i = new InstructionImpl(new InstructionPusher(id, input.getDeadline()), ret, id, dependencies, t);
274 this.insns.put(id, i);
276 // Attach it into its dependencies
277 for (final InstructionImpl d : dependencies) {
282 * All done. The next part is checking whether the instruction can
283 * run, which we can figure out after sending out the acknowledgement.
284 * This task should be ingress-weighed, so we reinsert it into the
285 * same execution service.
287 this.executor.submit(new Runnable() {
290 tryScheduleInstruction(i);
297 private synchronized void timeoutInstruction(final InstructionId id) {
298 final InstructionImpl i = this.insns.get(id);
300 LOG.warn("Instruction {} timed out, but not found in the queue", id);
307 private synchronized void tryScheduleDependants(final InstructionImpl i) {
308 // Walk all dependants and try to schedule them
309 final Iterator<InstructionImpl> it =i.getDependants();
310 while (it.hasNext()) {
311 tryScheduleInstruction(it.next());
315 private synchronized void tryScheduleInstruction(final InstructionImpl i) {
316 final ListenableFuture<ExecutionResult<Details>> f = i.ready();
318 Futures.addCallback(f, new FutureCallback<ExecutionResult<Details>>() {
320 public void onSuccess(final ExecutionResult<Details> result) {
321 tryScheduleDependants(i);
325 public void onFailure(final Throwable t) {
326 LOG.error("Instruction {} failed to execute", i.getId(), t);
334 public synchronized void close() {
336 for (InstructionImpl i : insns.values()) {
340 final DataModificationTransaction t = dataProvider.beginTransaction();
341 t.removeOperationalData(qid);