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.pcep.topology.spi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.bgpcep.programming.spi.Instruction;
18 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
19 import org.opendaylight.bgpcep.programming.spi.SchedulerException;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionStatus;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.SubmitInstructionInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.submit.instruction.output.result.FailureCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.submit.instruction.output.result.FailureCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.OperationResult;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public abstract class AbstractInstructionExecutor implements FutureCallback<Instruction> {
30 private static final Logger LOG = LoggerFactory.getLogger(AbstractInstructionExecutor.class);
32 private final SubmitInstructionInput input;
34 protected AbstractInstructionExecutor(@Nonnull final SubmitInstructionInput input) {
35 this.input = requireNonNull(input);
38 public static FailureCase schedule(final InstructionScheduler scheduler, final AbstractInstructionExecutor fwd) {
39 final SubmitInstructionInput input = fwd.getInput();
40 final ListenableFuture<Instruction> listenableFuture;
42 listenableFuture = scheduler.scheduleInstruction(input);
43 } catch (final SchedulerException e) {
44 LOG.info("Instuction {} failed to schedule", input, e);
45 return new FailureCaseBuilder().setFailure(e.getFailure()).build();
47 Futures.addCallback(listenableFuture, fwd, MoreExecutors.directExecutor());
51 public final SubmitInstructionInput getInput() {
55 protected abstract ListenableFuture<OperationResult> invokeOperation();
58 public void onSuccess(final Instruction insn) {
59 if (insn != null && insn.checkedExecutionStart()) {
60 final ListenableFuture<OperationResult> listenableFuture = invokeOperation();
61 Futures.addCallback(listenableFuture, new InstructionCallback(insn), MoreExecutors.directExecutor());
66 public void onFailure(final Throwable throwable) {
67 LOG.debug("Instruction {} cancelled", this.input, throwable);
70 private static final class InstructionCallback implements FutureCallback<OperationResult> {
72 private final Instruction insn;
74 InstructionCallback(@Nonnull final Instruction insn) {
75 this.insn = requireNonNull(insn);
79 public void onSuccess(final OperationResult result) {
80 if (result != null && result.getFailure() != null) {
81 switch (result.getFailure()) {
84 this.insn.executionCompleted(InstructionStatus.Failed, null);
87 this.insn.executionCompleted(InstructionStatus.Cancelled, null);
93 this.insn.executionCompleted(InstructionStatus.Successful, null);
98 public void onFailure(final Throwable throwable) {
99 this.insn.executionCompleted(InstructionStatus.Failed, null);