Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -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 | 50ec1af | 2015-06-02 00:42:52 -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 org.onlab.stc.Coordinator.Status; |
| 19 | |
| 20 | import static java.lang.Long.parseLong; |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 21 | import static org.onlab.stc.Coordinator.Status.valueOf; |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Represents an event of execution of a scenario step or group. |
| 25 | */ |
| 26 | public class StepEvent { |
| 27 | |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 28 | private static final String SEP = "~"; |
| 29 | |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 30 | private final String name; |
| 31 | private final long time; |
| 32 | private final Status status; |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 33 | private final String command; |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Creates a new step record. |
| 37 | * |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 38 | * @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 Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 42 | */ |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 43 | public StepEvent(String name, long time, Status status, String command) { |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 44 | this.name = name; |
| 45 | this.time = time; |
| 46 | this.status = status; |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 47 | this.command = command; |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates a new step record for non-running status. |
| 52 | * |
Thomas Vachuska | e827c0e | 2015-08-04 16:53:18 -0700 | [diff] [blame] | 53 | * @param name test step or group name |
| 54 | * @param status status |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 55 | * @param command step command |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 56 | */ |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 57 | public StepEvent(String name, Status status, String command) { |
| 58 | this(name, System.currentTimeMillis(), status, command); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 59 | } |
| 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 Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Returns the step command. |
| 90 | * |
| 91 | * @return step command |
| 92 | */ |
| 93 | public String command() { |
| 94 | return command; |
| 95 | } |
| 96 | |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 97 | |
| 98 | @Override |
| 99 | public String toString() { |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 100 | return name + SEP + time + SEP + status + SEP + command; |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 101 | } |
| 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 Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 110 | String[] fields = string.split("~"); |
Thomas Vachuska | e827c0e | 2015-08-04 16:53:18 -0700 | [diff] [blame] | 111 | 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 Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 115 | } |
| 116 | } |