blob: 692073f3dc2293aade44cbd165666482729a9abb [file] [log] [blame]
Thomas Vachuska50ec1af2015-06-02 00:42:52 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska50ec1af2015-06-02 00:42:52 -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 org.onlab.stc.Coordinator.Status;
19
20import static java.lang.Long.parseLong;
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -070021import static org.onlab.stc.Coordinator.Status.valueOf;
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070022
23/**
24 * Represents an event of execution of a scenario step or group.
25 */
26public class StepEvent {
27
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -070028 private static final String SEP = "~";
29
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070030 private final String name;
31 private final long time;
32 private final Status status;
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -070033 private final String command;
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070034
35 /**
36 * Creates a new step record.
37 *
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -070038 * @param name test step or group name
39 * @param time time in millis since start of epoch
40 * @param status step completion status
41 * @param command step command
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070042 */
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -070043 public StepEvent(String name, long time, Status status, String command) {
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070044 this.name = name;
45 this.time = time;
46 this.status = status;
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -070047 this.command = command;
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070048 }
49
50 /**
51 * Creates a new step record for non-running status.
52 *
Thomas Vachuskae827c0e2015-08-04 16:53:18 -070053 * @param name test step or group name
54 * @param status status
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -070055 * @param command step command
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070056 */
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -070057 public StepEvent(String name, Status status, String command) {
58 this(name, System.currentTimeMillis(), status, command);
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070059 }
60
61 /**
62 * Returns the test step or test group name.
63 *
64 * @return step or group name
65 */
66 public String name() {
67 return name;
68 }
69
70 /**
71 * Returns the step event time.
72 *
73 * @return time in millis since start of epoch
74 */
75 public long time() {
76 return time;
77 }
78
79 /**
80 * Returns the step completion status.
81 *
82 * @return completion status
83 */
84 public Status status() {
85 return status;
86 }
87
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -070088 /**
89 * Returns the step command.
90 *
91 * @return step command
92 */
93 public String command() {
94 return command;
95 }
96
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070097
98 @Override
99 public String toString() {
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -0700100 return name + SEP + time + SEP + status + SEP + command;
Thomas Vachuska50ec1af2015-06-02 00:42:52 -0700101 }
102
103 /**
104 * Returns a record parsed from the specified string.
105 *
106 * @param string string encoding
107 * @return step record
108 */
109 public static StepEvent fromString(String string) {
Thomas Vachuska0ec6ff42015-07-17 11:00:02 -0700110 String[] fields = string.split("~");
Thomas Vachuskae827c0e2015-08-04 16:53:18 -0700111 return fields.length == 4 ?
112 new StepEvent(fields[0], parseLong(fields[1]), valueOf(fields[2]),
113 fields[3].equals("null") ? null : fields[3]) :
114 new StepEvent(fields[0], 0, Status.WAITING, null);
Thomas Vachuska50ec1af2015-06-02 00:42:52 -0700115 }
116}