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.MoreExecutors;
17 import com.google.common.util.concurrent.SettableFuture;
18 import io.netty.util.Timeout;
19 import io.netty.util.Timer;
20 import java.math.BigInteger;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
28 import java.util.concurrent.Executor;
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;
77 final class ProgrammingServiceImpl implements ClusterSingletonService, InstructionScheduler, ProgrammingService {
78 private static final Logger LOG = LoggerFactory.getLogger(ProgrammingServiceImpl.class);
80 private final Map<InstructionId, InstructionImpl> insns = new HashMap<>();
81 private final InstanceIdentifier<InstructionsQueue> qid;
82 private final NotificationPublishService notifs;
83 private final Executor 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;
91 private ObjectRegistration<ProgrammingService> reg;
93 private final class InstructionPusher implements QueueInstruction {
94 private final InstructionBuilder builder = new InstructionBuilder();
96 InstructionPusher(final InstructionId id, final Nanotime deadline) {
97 builder.setDeadline(deadline);
99 builder.withKey(new InstructionKey(id));
100 builder.setStatus(InstructionStatus.Queued);
104 public void instructionUpdated(final InstructionStatus status, final Details details) {
105 if (!status.equals(builder.getStatus())) {
106 builder.setStatus(status);
108 final WriteTransaction wt = dataProvider.newWriteOnlyTransaction();
109 wt.put(LogicalDatastoreType.OPERATIONAL,
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>() {
116 public void onSuccess(final CommitInfo result) {
117 LOG.debug("Instruction Queue {} updated", qid);
121 public void onFailure(final Throwable trw) {
122 LOG.error("Failed to update Instruction Queue {}", qid, trw);
124 }, MoreExecutors.directExecutor());
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);
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>() {
144 public void onSuccess(final CommitInfo result) {
145 LOG.debug("Instruction Queue {} removed", qid);
149 public void onFailure(final Throwable trw) {
150 LOG.error("Failed to remove Instruction Queue {}", qid, trw);
152 }, MoreExecutors.directExecutor());
156 ProgrammingServiceImpl(final DataBroker dataProvider, final NotificationPublishService notifs,
157 final Executor 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);
173 public synchronized void instantiateServiceInstance() {
174 LOG.info("Instruction Queue service {} instantiated", sgi.getName());
175 reg = rpcProviderRegistry.registerRpcImplementation(ProgrammingService.class, this);
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>() {
182 public void onSuccess(final CommitInfo result) {
183 LOG.debug("Instruction Queue {} added", qid);
187 public void onFailure(final Throwable trw) {
188 LOG.error("Failed to add Instruction Queue {}", qid, trw);
190 }, MoreExecutors.directExecutor());
194 public ServiceGroupIdentifier getIdentifier() {
199 public ListenableFuture<RpcResult<CancelInstructionOutput>> cancelInstruction(final CancelInstructionInput input) {
200 return Futures.submit(() -> realCancelInstruction(input), executor);
204 public ListenableFuture<RpcResult<CleanInstructionsOutput>> cleanInstructions(final CleanInstructionsInput input) {
205 return Futures.submit(() -> realCleanInstructions(input), executor);
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());
213 final CancelInstructionOutput out = new CancelInstructionOutputBuilder()
214 .setFailure(UnknownInstruction.VALUE).build();
215 return SuccessfulRpcResult.create(out);
218 return SuccessfulRpcResult.create(new CancelInstructionOutputBuilder()
219 .setFailure(instruction.tryCancel(null)).build());
222 private synchronized RpcResult<CleanInstructionsOutput> realCleanInstructions(final CleanInstructionsInput input) {
223 final Set<InstructionId> failed = new HashSet<>();
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());
235 switch (instruction.getStatus()) {
244 LOG.debug("Instruction {} cannot be cleaned because of it's in state {}",
245 id, instruction.getStatus());
252 // The instruction is in a terminal state, we need to just unlink
253 // it from its dependencies and dependents
257 LOG.debug("Instruction {} cleaned successfully", id);
260 final CleanInstructionsOutputBuilder ob = new CleanInstructionsOutputBuilder();
261 ob.setUnflushed(failed);
263 return SuccessfulRpcResult.create(ob.build());
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);
271 * Some dependencies have failed, declare the request dead-on-arrival
272 * and fail the operation.
274 if (!unmet.isEmpty()) {
275 throw new SchedulerException("Instruction's dependencies are already unsuccessful", new FailureBuilder()
276 .setType(DeadOnArrival.VALUE).setFailedPreconditions(unmet).build());
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());
290 dependencies.add(instruction);
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()) {
302 unmet.add(d.getId());
317 public synchronized ListenableFuture<Instruction> scheduleInstruction(final SubmitInstructionInput input) throws
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());
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());
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());
336 // Resolve dependencies
337 final List<InstructionImpl> dependencies = checkDependencies(input);
340 * All pre-flight checks done are at this point, the following
341 * steps can only fail in catastrophic scenarios (OOM and the
345 // Schedule a timeout for the instruction
346 final Timeout t = timer.newTimeout(timeout -> timeoutInstruction(input.getId()), left.longValue(),
347 TimeUnit.NANOSECONDS);
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,
353 insns.put(id, instruction);
355 // Attach it into its dependencies
356 for (final InstructionImpl d : dependencies) {
357 d.addDependant(instruction);
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.
366 executor.execute(() -> tryScheduleInstruction(instruction));
372 public String getInstructionID() {
373 return instructionId;
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);
383 instruction.timeout();
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());
394 private synchronized void tryScheduleInstruction(final InstructionImpl instruction) {
395 final ListenableFuture<ExecutionResult<Details>> f = instruction.ready();
397 Futures.addCallback(f, new FutureCallback<ExecutionResult<Details>>() {
399 public void onSuccess(final ExecutionResult<Details> result) {
400 tryScheduleDependants(instruction);
404 public void onFailure(final Throwable trw) {
405 LOG.error("Instruction {} failed to execute", instruction.getId(), trw);
407 }, MoreExecutors.directExecutor());
413 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
414 LOG.info("Closing Instruction Queue service {}", sgi.getName());
420 for (final InstructionImpl instruction : insns.values()) {
421 instruction.tryCancel(null);
423 // Workaround for BUG-2283
424 final WriteTransaction wt = dataProvider.newWriteOnlyTransaction();
425 wt.delete(LogicalDatastoreType.OPERATIONAL, qid);
427 final FluentFuture<? extends CommitInfo> future = wt.commit();
428 future.addCallback(new FutureCallback<CommitInfo>() {
430 public void onSuccess(final CommitInfo result) {
431 LOG.debug("Instruction Queue {} removed", qid);
435 public void onFailure(final Throwable trw) {
436 LOG.error("Failed to shutdown Instruction Queue {}", qid, trw);
438 }, MoreExecutors.directExecutor());
444 public synchronized void close() {