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 | |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | import org.apache.commons.configuration.ConfigurationException; |
| 22 | import org.apache.commons.configuration.PropertiesConfiguration; |
| 23 | import org.onlab.stc.Coordinator.Status; |
| 24 | |
| 25 | import java.io.File; |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 26 | import java.util.List; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 31 | import static org.onlab.stc.Coordinator.Status.*; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 32 | import static org.onlab.stc.Coordinator.print; |
| 33 | |
| 34 | /** |
| 35 | * Maintains state of scenario execution. |
| 36 | */ |
| 37 | class ScenarioStore { |
| 38 | |
| 39 | private final ProcessFlow processFlow; |
| 40 | private final File storeFile; |
Thomas Vachuska | a48e3d1 | 2015-06-02 09:43:29 -0700 | [diff] [blame] | 41 | private final File logDir; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 42 | |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 43 | private final List<StepEvent> events = Lists.newArrayList(); |
| 44 | private final Map<String, Status> statusMap = Maps.newConcurrentMap(); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 45 | |
Thomas Vachuska | d542cc4 | 2015-09-11 16:15:36 -0700 | [diff] [blame] | 46 | private long startTime = Long.MAX_VALUE; |
| 47 | private long endTime = Long.MIN_VALUE; |
| 48 | |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Creates a new scenario store for the specified process flow. |
| 51 | * |
| 52 | * @param processFlow scenario process flow |
| 53 | * @param logDir scenario log directory |
| 54 | * @param name scenario name |
| 55 | */ |
| 56 | ScenarioStore(ProcessFlow processFlow, File logDir, String name) { |
| 57 | this.processFlow = processFlow; |
Thomas Vachuska | a48e3d1 | 2015-06-02 09:43:29 -0700 | [diff] [blame] | 58 | this.logDir = logDir; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 59 | this.storeFile = new File(logDir, name + ".stc"); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 60 | load(); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Resets status of all steps to waiting and clears all events. |
| 65 | */ |
| 66 | void reset() { |
| 67 | events.clear(); |
| 68 | statusMap.clear(); |
| 69 | processFlow.getVertexes().forEach(step -> statusMap.put(step.name(), WAITING)); |
| 70 | try { |
Thomas Vachuska | a48e3d1 | 2015-06-02 09:43:29 -0700 | [diff] [blame] | 71 | removeLogs(); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 72 | PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile); |
| 73 | cfg.clear(); |
| 74 | cfg.save(); |
Thomas Vachuska | d542cc4 | 2015-09-11 16:15:36 -0700 | [diff] [blame] | 75 | startTime = Long.MAX_VALUE; |
| 76 | endTime = Long.MIN_VALUE; |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 77 | } catch (ConfigurationException e) { |
| 78 | print("Unable to store file %s", storeFile); |
| 79 | } |
| 80 | |
| 81 | } |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Returns set of all test steps. |
| 85 | * |
| 86 | * @return set of steps |
| 87 | */ |
| 88 | Set<Step> getSteps() { |
| 89 | return processFlow.getVertexes(); |
| 90 | } |
| 91 | |
| 92 | /** |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 93 | * Returns a chronological list of step or group records. |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 94 | * |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 95 | * @return list of events |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 96 | */ |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 97 | synchronized List<StepEvent> getEvents() { |
| 98 | return ImmutableList.copyOf(events); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 102 | * Returns the status record of the specified test step. |
| 103 | * |
| 104 | * @param step test step or group |
| 105 | * @return step status record |
| 106 | */ |
| 107 | Status getStatus(Step step) { |
| 108 | return checkNotNull(statusMap.get(step.name()), "Step %s not found", step.name()); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Marks the specified test step as being in progress. |
| 113 | * |
| 114 | * @param step test step or group |
| 115 | */ |
| 116 | synchronized void markStarted(Step step) { |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 117 | add(new StepEvent(step.name(), IN_PROGRESS, step.command())); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 118 | save(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Marks the specified test step as being complete. |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 123 | * |
| 124 | * @param step test step or group |
| 125 | * @param status new step status |
| 126 | */ |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 127 | synchronized void markComplete(Step step, Status status) { |
Thomas Vachuska | 0ec6ff4 | 2015-07-17 11:00:02 -0700 | [diff] [blame] | 128 | add(new StepEvent(step.name(), status, null)); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 129 | save(); |
| 130 | } |
| 131 | |
| 132 | /** |
Thomas Vachuska | 1b403a5 | 2015-08-26 11:30:48 -0700 | [diff] [blame] | 133 | * Returns true if all steps in the store have been marked as completed |
| 134 | * regardless of the completion status. |
| 135 | * |
| 136 | * @return true if all steps completed one way or another |
| 137 | */ |
| 138 | synchronized boolean isComplete() { |
| 139 | return !statusMap.values().stream().anyMatch(s -> s == WAITING || s == IN_PROGRESS); |
| 140 | } |
| 141 | |
| 142 | /** |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 143 | * Indicates whether there are any failures. |
| 144 | * |
| 145 | * @return true if there are failed steps |
| 146 | */ |
| 147 | boolean hasFailures() { |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 148 | for (Status status : statusMap.values()) { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 149 | if (status == FAILED) { |
| 150 | return true; |
| 151 | } |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | /** |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 157 | * Registers a new step record. |
| 158 | * |
| 159 | * @param event step event |
| 160 | */ |
| 161 | private synchronized void add(StepEvent event) { |
| 162 | events.add(event); |
| 163 | statusMap.put(event.name(), event.status()); |
Thomas Vachuska | d542cc4 | 2015-09-11 16:15:36 -0700 | [diff] [blame] | 164 | startTime = Math.min(startTime, event.time()); |
| 165 | endTime = Math.max(endTime, event.time()); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 169 | * Loads the states from disk. |
| 170 | */ |
| 171 | private void load() { |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 172 | try { |
| 173 | PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile); |
| 174 | cfg.getKeys().forEachRemaining(prop -> add(StepEvent.fromString(cfg.getString(prop)))); |
| 175 | cfg.save(); |
| 176 | } catch (ConfigurationException e) { |
Thomas Vachuska | e827c0e | 2015-08-04 16:53:18 -0700 | [diff] [blame] | 177 | print("Unable to load file %s", storeFile); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 178 | } |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Saves the states to disk. |
| 183 | */ |
| 184 | private void save() { |
| 185 | try { |
| 186 | PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 187 | events.forEach(event -> cfg.setProperty("T" + event.time(), event.toString())); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 188 | cfg.save(); |
| 189 | } catch (ConfigurationException e) { |
| 190 | print("Unable to store file %s", storeFile); |
| 191 | } |
| 192 | } |
| 193 | |
Thomas Vachuska | a48e3d1 | 2015-06-02 09:43:29 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Removes all scenario log files. |
| 196 | */ |
| 197 | private void removeLogs() { |
| 198 | File[] logFiles = logDir.listFiles(); |
| 199 | if (logFiles != null && logFiles.length > 0) { |
| 200 | for (File file : logFiles) { |
| 201 | if (!file.delete()) { |
| 202 | print("Unable to delete log file %s", file); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Thomas Vachuska | d542cc4 | 2015-09-11 16:15:36 -0700 | [diff] [blame] | 208 | /** |
| 209 | * Returns the scenario run start time. |
| 210 | * |
| 211 | * @return start time in mills since start of epoch |
| 212 | */ |
| 213 | public long startTime() { |
| 214 | return startTime; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Returns the scenario run end time or current time if the scenario |
| 219 | * is still running. |
| 220 | * |
| 221 | * @return end time (or current time) in mills since start of epoch |
| 222 | */ |
| 223 | public long endTime() { |
| 224 | return endTime > 0 ? endTime : System.currentTimeMillis(); |
| 225 | } |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 226 | } |