BUG-48: more implementation
[bgpcep.git] / programming / impl / src / main / java / org / opendaylight / bgpcep / programming / impl / Instruction.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.programming.impl;
9
10 import io.netty.util.Timeout;
11 import io.netty.util.concurrent.Future;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.bgpcep.programming.spi.ExecutionResult;
17 import org.opendaylight.bgpcep.programming.spi.InstructionExecutor;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionStatus;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.status.changed.Details;
21
22 import com.google.common.base.Preconditions;
23
24 final class Instruction {
25         private final List<Instruction> dependants = new ArrayList<>();
26         private final InstructionExecutor executor;
27         private final List<Instruction> dependencies;
28         private final InstructionId id;
29         private volatile InstructionStatus status = InstructionStatus.Queued;
30         private Timeout timeout;
31
32         Instruction(final InstructionId id, final InstructionExecutor executor, final List<Instruction> dependencies, final Timeout timeout) {
33                 this.id = Preconditions.checkNotNull(id);
34                 this.executor = Preconditions.checkNotNull(executor);
35                 this.dependencies = Preconditions.checkNotNull(dependencies);
36                 this.timeout = Preconditions.checkNotNull(timeout);
37         }
38
39         InstructionId getId() {
40                 return id;
41         }
42
43         InstructionStatus getStatus() {
44                 return status;
45         }
46
47         Future<ExecutionResult<Details>> execute() {
48                 return executor.execute();
49         }
50
51         void setStatus(final InstructionStatus status) {
52                 this.status = status;
53         }
54
55         synchronized void cancel() {
56                 if (timeout != null) {
57                         timeout.cancel();
58                         timeout = null;
59                 }
60         }
61
62         synchronized void completed() {
63                 timeout = null;
64         }
65
66         synchronized void addDependant(final Instruction d) {
67                 dependants.add(d);
68         }
69
70         List<Instruction> getDependencies() {
71                 return dependencies;
72         }
73
74         List<Instruction> getDependants() {
75                 return dependants;
76         }
77 }