blob: 4fa03affdfbe2ac39b3781a4e0ba8621d52327ac [file] [log] [blame]
Thomas Vachuskaf9c84362015-04-15 11:20:45 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskaf9c84362015-04-15 11:20:45 -07003 *
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 */
16package org.onlab.stc;
17
18import com.google.common.base.MoreObjects;
19import org.onlab.graph.Vertex;
20
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Representation of a test step.
27 */
28public class Step implements Vertex {
29
30 protected final String name;
31 protected final String command;
Thomas Vachuska4bfccd542015-05-30 00:35:25 -070032 protected final String env;
33 protected final String cwd;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070034 protected final Group group;
Thomas Vachuska664f29e2015-12-08 12:58:16 -080035 protected final int delay;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070036
37 /**
38 * Creates a new test step.
39 *
Thomas Vachuska4bfccd542015-05-30 00:35:25 -070040 * @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 Vachuska664f29e2015-12-08 12:58:16 -080045 * @param delay seconds to delay before executing
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070046 */
Thomas Vachuska664f29e2015-12-08 12:58:16 -080047 public Step(String name, String command, String env, String cwd, Group group, int delay) {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070048 this.name = checkNotNull(name, "Name cannot be null");
49 this.group = group;
Thomas Vachuska664f29e2015-12-08 12:58:16 -080050 this.delay = delay;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070051
Thomas Vachuska4bfccd542015-05-30 00:35:25 -070052 // 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 Vachuskaf9c84362015-04-15 11:20:45 -070057 }
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 Vachuska4bfccd542015-05-30 00:35:25 -070078 * 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 Vachuskaf9c84362015-04-15 11:20:45 -070096 * 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 Vachuska664f29e2015-12-08 12:58:16 -0800104 /**
105 * Returns the start delay in seconds.
106 *
107 * @return number of seconds
108 */
109 public int delay() {
110 return delay;
111 }
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700112
113 @Override
114 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700115 return name.hashCode();
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700116 }
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 Vachuska4bfccd542015-05-30 00:35:25 -0700135 .add("env", env)
136 .add("cwd", cwd)
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700137 .add("group", group)
Thomas Vachuska664f29e2015-12-08 12:58:16 -0800138 .add("delay", delay)
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700139 .toString();
140 }
141}