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 static java.util.Objects.requireNonNull;
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;
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;
77 public final class ProgrammingServiceImpl implements ClusterSingletonService, InstructionScheduler,
79 private static final Logger LOG = LoggerFactory.getLogger(ProgrammingServiceImpl.class);
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;
92 private RpcRegistration<ProgrammingService> reg;
94 private ServiceRegistration<?> serviceRegistration;
96 private final class InstructionPusher implements QueueInstruction {
97 private final InstructionBuilder builder = new InstructionBuilder();
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);
107 public void instructionUpdated(final InstructionStatus status, final Details details) {
108 if (!status.equals(this.builder.getStatus())) {
109 this.builder.setStatus(status);
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>() {
119 public void onSuccess(final CommitInfo result) {
120 LOG.debug("Instruction Queue {} updated", ProgrammingServiceImpl.this.qid);
124 public void onFailure(final Throwable trw) {
125 LOG.error("Failed to update Instruction Queue {}", ProgrammingServiceImpl.this.qid, trw);
127 }, MoreExecutors.directExecutor());
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);
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>() {
147 public void onSuccess(final CommitInfo result) {
148 LOG.debug("Instruction Queue {} removed", ProgrammingServiceImpl.this.qid);
152 public void onFailure(final Throwable trw) {
153 LOG.error("Failed to remove Instruction Queue {}", ProgrammingServiceImpl.this.qid, trw);
155 }, MoreExecutors.directExecutor());
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);
176 public synchronized void instantiateServiceInstance() {
177 LOG.info("Instruction Queue service {} instantiated", this.sgi.getValue());
178 this.reg = this.rpcProviderRegistry.addRpcImplementation(ProgrammingService.class, this);
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>() {
185 public void onSuccess(final CommitInfo result) {
186 LOG.debug("Instruction Queue {} added", ProgrammingServiceImpl.this.qid);
190 public void onFailure(final Throwable trw) {
191 LOG.error("Failed to add Instruction Queue {}", ProgrammingServiceImpl.this.qid, trw);
193 }, MoreExecutors.directExecutor());
197 public ServiceGroupIdentifier getIdentifier() {
202 public ListenableFuture<RpcResult<CancelInstructionOutput>> cancelInstruction(final CancelInstructionInput input) {
203 return this.executor.submit(() -> realCancelInstruction(input));
207 public ListenableFuture<RpcResult<CleanInstructionsOutput>> cleanInstructions(final CleanInstructionsInput input) {
208 return this.executor.submit(() -> realCleanInstructions(input));
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());
216 final CancelInstructionOutput out = new CancelInstructionOutputBuilder()
217 .setFailure(UnknownInstruction.class).build();
218 return SuccessfulRpcResult.create(out);
221 return SuccessfulRpcResult.create(new CancelInstructionOutputBuilder()
222 .setFailure(instruction.tryCancel(null)).build());
225 private synchronized RpcResult<CleanInstructionsOutput> realCleanInstructions(final CleanInstructionsInput input) {
226 final List<InstructionId> failed = new ArrayList<>();
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());
238 switch (instruction.getStatus()) {
247 LOG.debug("Instruction {} cannot be cleaned because of it's in state {}",
248 id, instruction.getStatus());
255 // The instruction is in a terminal state, we need to just unlink
256 // it from its dependencies and dependents
259 this.insns.remove(id);
260 LOG.debug("Instruction {} cleaned successfully", id);
263 final CleanInstructionsOutputBuilder ob = new CleanInstructionsOutputBuilder();
264 ob.setUnflushed(failed);
266 return SuccessfulRpcResult.create(ob.build());
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);
274 * Some dependencies have failed, declare the request dead-on-arrival
275 * and fail the operation.
277 if (!unmet.isEmpty()) {
278 throw new SchedulerException("Instruction's dependencies are already unsuccessful", new FailureBuilder()
279 .setType(DeadOnArrival.class).setFailedPreconditions(unmet).build());
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());
293 dependencies.add(instruction);
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()) {
305 unmet.add(d.getId());
320 public synchronized ListenableFuture<Instruction> scheduleInstruction(final SubmitInstructionInput input) throws
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());
329 // First things first: check the deadline
330 final Nanotime now = NanotimeUtil.currentTime();
331 final BigInteger left = input.getDeadline().getValue().subtract(now.getValue());
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());
339 // Resolve dependencies
340 final List<InstructionImpl> dependencies = checkDependencies(input);
343 * All pre-flight checks done are at this point, the following
344 * steps can only fail in catastrophic scenarios (OOM and the
348 // Schedule a timeout for the instruction
349 final Timeout t = this.timer.newTimeout(timeout -> timeoutInstruction(input.getId()), left.longValue(),
350 TimeUnit.NANOSECONDS);
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,
356 this.insns.put(id, instruction);
358 // Attach it into its dependencies
359 for (final InstructionImpl d : dependencies) {
360 d.addDependant(instruction);
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.
369 this.executor.submit(() -> tryScheduleInstruction(instruction));
375 public String getInstructionID() {
376 return this.instructionId;
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);
386 instruction.timeout();
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());
397 private synchronized void tryScheduleInstruction(final InstructionImpl instruction) {
398 final ListenableFuture<ExecutionResult<Details>> f = instruction.ready();
400 Futures.addCallback(f, new FutureCallback<ExecutionResult<Details>>() {
402 public void onSuccess(final ExecutionResult<Details> result) {
403 tryScheduleDependants(instruction);
407 public void onFailure(final Throwable trw) {
408 LOG.error("Instruction {} failed to execute", instruction.getId(), trw);
410 }, MoreExecutors.directExecutor());
416 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
417 LOG.info("Closing Instruction Queue service {}", this.sgi.getValue());
419 if (this.reg != null) {
423 for (final InstructionImpl instruction : this.insns.values()) {
424 instruction.tryCancel(null);
426 // Workaround for BUG-2283
427 final WriteTransaction wt = this.dataProvider.newWriteOnlyTransaction();
428 wt.delete(LogicalDatastoreType.OPERATIONAL, this.qid);
430 final FluentFuture<? extends CommitInfo> future = wt.commit();
431 future.addCallback(new FutureCallback<CommitInfo>() {
433 public void onSuccess(final CommitInfo result) {
434 LOG.debug("Instruction Queue {} removed", ProgrammingServiceImpl.this.qid);
438 public void onFailure(final Throwable trw) {
439 LOG.error("Failed to shutdown Instruction Queue {}", ProgrammingServiceImpl.this.qid, trw);
441 }, MoreExecutors.directExecutor());
447 @SuppressWarnings("checkstyle:IllegalCatch")
448 public synchronized void close() {
449 if (this.csspReg != null) {
451 this.csspReg.close();
452 } catch (final Exception e) {
453 LOG.error("Failed to close Instruction Scheduler service", e);
456 if (this.serviceRegistration != null) {
457 this.serviceRegistration.unregister();
458 this.serviceRegistration = null;
462 void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
463 this.serviceRegistration = serviceRegistration;