Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onlab.stc; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.graph.Vertex; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
| 26 | * Representation of a test step. |
| 27 | */ |
| 28 | public class Step implements Vertex { |
| 29 | |
| 30 | protected final String name; |
| 31 | protected final String command; |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 32 | protected final String env; |
| 33 | protected final String cwd; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 34 | protected final Group group; |
Thomas Vachuska | 664f29e | 2015-12-08 12:58:16 -0800 | [diff] [blame] | 35 | protected final int delay; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Creates a new test step. |
| 39 | * |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 40 | * @param name step name |
| 41 | * @param command step command to execute |
| 42 | * @param env path to file to be sourced into the environment |
| 43 | * @param cwd path to current working directory for the step |
| 44 | * @param group optional group to which this step belongs |
Thomas Vachuska | 664f29e | 2015-12-08 12:58:16 -0800 | [diff] [blame] | 45 | * @param delay seconds to delay before executing |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 46 | */ |
Thomas Vachuska | 664f29e | 2015-12-08 12:58:16 -0800 | [diff] [blame] | 47 | public Step(String name, String command, String env, String cwd, Group group, int delay) { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 48 | this.name = checkNotNull(name, "Name cannot be null"); |
| 49 | this.group = group; |
Thomas Vachuska | 664f29e | 2015-12-08 12:58:16 -0800 | [diff] [blame] | 50 | this.delay = delay; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 51 | |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 52 | // Set the command, environment and cwd |
| 53 | // If one is not given use the value from the enclosing group |
| 54 | this.command = command != null ? command : group != null && group.command != null ? group.command : null; |
| 55 | this.env = env != null ? env : group != null && group.env != null ? group.env : null; |
| 56 | this.cwd = cwd != null ? cwd : group != null && group.cwd != null ? group.cwd : null; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the step name. |
| 61 | * |
| 62 | * @return step name |
| 63 | */ |
| 64 | public String name() { |
| 65 | return name; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the step command string. |
| 70 | * |
| 71 | * @return command string |
| 72 | */ |
| 73 | public String command() { |
| 74 | return command; |
| 75 | } |
| 76 | |
| 77 | /** |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 78 | * Returns the step environment script path. |
| 79 | * |
| 80 | * @return env script path |
| 81 | */ |
| 82 | public String env() { |
| 83 | return env; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the step current working directory path. |
| 88 | * |
| 89 | * @return current working dir path |
| 90 | */ |
| 91 | public String cwd() { |
| 92 | return cwd; |
| 93 | } |
| 94 | |
| 95 | /** |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 96 | * Returns the enclosing group; null if none. |
| 97 | * |
| 98 | * @return enclosing group or null |
| 99 | */ |
| 100 | public Group group() { |
| 101 | return group; |
| 102 | } |
| 103 | |
Thomas Vachuska | 664f29e | 2015-12-08 12:58:16 -0800 | [diff] [blame] | 104 | /** |
| 105 | * Returns the start delay in seconds. |
| 106 | * |
| 107 | * @return number of seconds |
| 108 | */ |
| 109 | public int delay() { |
| 110 | return delay; |
| 111 | } |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 112 | |
| 113 | @Override |
| 114 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 115 | return name.hashCode(); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean equals(Object obj) { |
| 120 | if (this == obj) { |
| 121 | return true; |
| 122 | } |
| 123 | if (obj instanceof Step) { |
| 124 | final Step other = (Step) obj; |
| 125 | return Objects.equals(this.name, other.name); |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | return MoreObjects.toStringHelper(this) |
| 133 | .add("name", name) |
| 134 | .add("command", command) |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 135 | .add("env", env) |
| 136 | .add("cwd", cwd) |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 137 | .add("group", group) |
Thomas Vachuska | 664f29e | 2015-12-08 12:58:16 -0800 | [diff] [blame] | 138 | .add("delay", delay) |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 139 | .toString(); |
| 140 | } |
| 141 | } |