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 org.eclipse.jdt.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.FailureType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.OperationResult;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public abstract class AbstractInstructionExecutor implements FutureCallback<Instruction> {
31 private static final Logger LOG = LoggerFactory.getLogger(AbstractInstructionExecutor.class);
33 private final SubmitInstructionInput input;
35 protected AbstractInstructionExecutor(final @NonNull SubmitInstructionInput input) {
36 this.input = requireNonNull(input);
39 public static FailureCase schedule(final InstructionScheduler scheduler, final AbstractInstructionExecutor fwd) {
40 final SubmitInstructionInput input = fwd.getInput();
41 final ListenableFuture<Instruction> listenableFuture;
43 listenableFuture = scheduler.scheduleInstruction(input);
44 } catch (final SchedulerException e) {
45 LOG.info("Instuction {} failed to schedule", input, e);
46 return new FailureCaseBuilder().setFailure(e.getFailure()).build();
48 Futures.addCallback(listenableFuture, fwd, MoreExecutors.directExecutor());
52 public final SubmitInstructionInput getInput() {
56 protected abstract ListenableFuture<OperationResult> invokeOperation();
59 public void onSuccess(final Instruction insn) {
60 if (insn != null && insn.checkedExecutionStart()) {
61 final ListenableFuture<OperationResult> listenableFuture = invokeOperation();
62 Futures.addCallback(listenableFuture, new InstructionCallback(insn), MoreExecutors.directExecutor());
67 public void onFailure(final Throwable throwable) {
68 LOG.debug("Instruction {} cancelled", this.input, throwable);
71 private static final class InstructionCallback implements FutureCallback<OperationResult> {
73 private final Instruction insn;
75 InstructionCallback(final @NonNull Instruction insn) {
76 this.insn = requireNonNull(insn);
80 public void onSuccess(final OperationResult result) {
82 final FailureType failure = result.getFailure();
83 if (failure != null) {
87 this.insn.executionCompleted(InstructionStatus.Failed, null);
90 this.insn.executionCompleted(InstructionStatus.Cancelled, null);
97 this.insn.executionCompleted(InstructionStatus.Successful, null);
102 public void onFailure(final Throwable throwable) {
103 this.insn.executionCompleted(InstructionStatus.Failed, null);