Add new revision for pcep types model
[bgpcep.git] / pcep / topology / topology-spi / src / main / java / org / opendaylight / bgpcep / pcep / topology / spi / AbstractInstructionExecutor.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.bgpcep.pcep.topology.spi;
9
10 import static java.util.Objects.requireNonNull;
11
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;
27
28 public abstract class AbstractInstructionExecutor implements FutureCallback<Instruction> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(AbstractInstructionExecutor.class);
31
32     private final SubmitInstructionInput input;
33
34     protected AbstractInstructionExecutor(@Nonnull final SubmitInstructionInput input) {
35         this.input = requireNonNull(input);
36     }
37
38     public static FailureCase schedule(final InstructionScheduler scheduler, final AbstractInstructionExecutor fwd) {
39         final SubmitInstructionInput input = fwd.getInput();
40         final ListenableFuture<Instruction> listenableFuture;
41         try {
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();
46         }
47         Futures.addCallback(listenableFuture, fwd, MoreExecutors.directExecutor());
48         return null;
49     }
50
51     public final SubmitInstructionInput getInput() {
52         return this.input;
53     }
54
55     protected abstract ListenableFuture<OperationResult> invokeOperation();
56
57     @Override
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());
62         }
63     }
64
65     @Override
66     public void onFailure(final Throwable throwable) {
67         LOG.debug("Instruction {} cancelled", this.input, throwable);
68     }
69
70     private static final class InstructionCallback implements FutureCallback<OperationResult> {
71
72         private final Instruction insn;
73
74         InstructionCallback(@Nonnull final Instruction insn) {
75             this.insn = requireNonNull(insn);
76         }
77
78         @Override
79         public void onSuccess(final OperationResult result) {
80             if (result != null && result.getFailure() != null) {
81                 switch (result.getFailure()) {
82                     case Failed:
83                     case NoAck:
84                         this.insn.executionCompleted(InstructionStatus.Failed, null);
85                         break;
86                     case Unsent:
87                         this.insn.executionCompleted(InstructionStatus.Cancelled, null);
88                         break;
89                     default:
90                         break;
91                 }
92             } else {
93                 this.insn.executionCompleted(InstructionStatus.Successful, null);
94             }
95         }
96
97         @Override
98         public void onFailure(final Throwable throwable) {
99             this.insn.executionCompleted(InstructionStatus.Failed, null);
100         }
101     }
102 }