Get rid of JSR305 annotations
[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 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;
28
29 public abstract class AbstractInstructionExecutor implements FutureCallback<Instruction> {
30
31     private static final Logger LOG = LoggerFactory.getLogger(AbstractInstructionExecutor.class);
32
33     private final SubmitInstructionInput input;
34
35     protected AbstractInstructionExecutor(final @NonNull SubmitInstructionInput input) {
36         this.input = requireNonNull(input);
37     }
38
39     public static FailureCase schedule(final InstructionScheduler scheduler, final AbstractInstructionExecutor fwd) {
40         final SubmitInstructionInput input = fwd.getInput();
41         final ListenableFuture<Instruction> listenableFuture;
42         try {
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();
47         }
48         Futures.addCallback(listenableFuture, fwd, MoreExecutors.directExecutor());
49         return null;
50     }
51
52     public final SubmitInstructionInput getInput() {
53         return this.input;
54     }
55
56     protected abstract ListenableFuture<OperationResult> invokeOperation();
57
58     @Override
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());
63         }
64     }
65
66     @Override
67     public void onFailure(final Throwable throwable) {
68         LOG.debug("Instruction {} cancelled", this.input, throwable);
69     }
70
71     private static final class InstructionCallback implements FutureCallback<OperationResult> {
72
73         private final Instruction insn;
74
75         InstructionCallback(final @NonNull Instruction insn) {
76             this.insn = requireNonNull(insn);
77         }
78
79         @Override
80         public void onSuccess(final OperationResult result) {
81             if (result != null) {
82                 final FailureType failure = result.getFailure();
83                 if (failure != null) {
84                     switch (failure) {
85                         case Failed:
86                         case NoAck:
87                             this.insn.executionCompleted(InstructionStatus.Failed, null);
88                             break;
89                         case Unsent:
90                             this.insn.executionCompleted(InstructionStatus.Cancelled, null);
91                             break;
92                         default:
93                             break;
94                     }
95                 }
96             } else {
97                 this.insn.executionCompleted(InstructionStatus.Successful, null);
98             }
99         }
100
101         @Override
102         public void onFailure(final Throwable throwable) {
103             this.insn.executionCompleted(InstructionStatus.Failed, null);
104         }
105     }
106 }