Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 46 | /** |
| 47 | * Creates a new scenario store for the specified process flow. |
| 48 | * |
| 49 | * @param processFlow scenario process flow |
| 50 | * @param logDir scenario log directory |
| 51 | * @param name scenario name |
| 52 | */ |
| 53 | ScenarioStore(ProcessFlow processFlow, File logDir, String name) { |
| 54 | this.processFlow = processFlow; |
Thomas Vachuska | a48e3d1 | 2015-06-02 09:43:29 -0700 | [diff] [blame^] | 55 | this.logDir = logDir; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 56 | this.storeFile = new File(logDir, name + ".stc"); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 57 | load(); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Resets status of all steps to waiting and clears all events. |
| 62 | */ |
| 63 | void reset() { |
| 64 | events.clear(); |
| 65 | statusMap.clear(); |
| 66 | processFlow.getVertexes().forEach(step -> statusMap.put(step.name(), WAITING)); |
| 67 | try { |
Thomas Vachuska | a48e3d1 | 2015-06-02 09:43:29 -0700 | [diff] [blame^] | 68 | removeLogs(); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 69 | PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile); |
| 70 | cfg.clear(); |
| 71 | cfg.save(); |
| 72 | } catch (ConfigurationException e) { |
| 73 | print("Unable to store file %s", storeFile); |
| 74 | } |
| 75 | |
| 76 | } |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Returns set of all test steps. |
| 80 | * |
| 81 | * @return set of steps |
| 82 | */ |
| 83 | Set<Step> getSteps() { |
| 84 | return processFlow.getVertexes(); |
| 85 | } |
| 86 | |
| 87 | /** |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 88 | * Returns a chronological list of step or group records. |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 89 | * |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 90 | * @return list of events |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 91 | */ |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 92 | synchronized List<StepEvent> getEvents() { |
| 93 | return ImmutableList.copyOf(events); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 97 | * Returns the status record of the specified test step. |
| 98 | * |
| 99 | * @param step test step or group |
| 100 | * @return step status record |
| 101 | */ |
| 102 | Status getStatus(Step step) { |
| 103 | return checkNotNull(statusMap.get(step.name()), "Step %s not found", step.name()); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Marks the specified test step as being in progress. |
| 108 | * |
| 109 | * @param step test step or group |
| 110 | */ |
| 111 | synchronized void markStarted(Step step) { |
| 112 | add(new StepEvent(step.name(), IN_PROGRESS)); |
| 113 | save(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Marks the specified test step as being complete. |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 118 | * |
| 119 | * @param step test step or group |
| 120 | * @param status new step status |
| 121 | */ |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 122 | synchronized void markComplete(Step step, Status status) { |
| 123 | add(new StepEvent(step.name(), status)); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 124 | save(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Indicates whether there are any failures. |
| 129 | * |
| 130 | * @return true if there are failed steps |
| 131 | */ |
| 132 | boolean hasFailures() { |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 133 | for (Status status : statusMap.values()) { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 134 | if (status == FAILED) { |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | /** |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 142 | * Registers a new step record. |
| 143 | * |
| 144 | * @param event step event |
| 145 | */ |
| 146 | private synchronized void add(StepEvent event) { |
| 147 | events.add(event); |
| 148 | statusMap.put(event.name(), event.status()); |
| 149 | } |
| 150 | |
| 151 | /** |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 152 | * Loads the states from disk. |
| 153 | */ |
| 154 | private void load() { |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 155 | try { |
| 156 | PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile); |
| 157 | cfg.getKeys().forEachRemaining(prop -> add(StepEvent.fromString(cfg.getString(prop)))); |
| 158 | cfg.save(); |
| 159 | } catch (ConfigurationException e) { |
| 160 | print("Unable to store file %s", storeFile); |
| 161 | } |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Saves the states to disk. |
| 166 | */ |
| 167 | private void save() { |
| 168 | try { |
| 169 | PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile); |
Thomas Vachuska | 50ec1af | 2015-06-02 00:42:52 -0700 | [diff] [blame] | 170 | events.forEach(event -> cfg.setProperty("T" + event.time(), event.toString())); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 171 | cfg.save(); |
| 172 | } catch (ConfigurationException e) { |
| 173 | print("Unable to store file %s", storeFile); |
| 174 | } |
| 175 | } |
| 176 | |
Thomas Vachuska | a48e3d1 | 2015-06-02 09:43:29 -0700 | [diff] [blame^] | 177 | /** |
| 178 | * Removes all scenario log files. |
| 179 | */ |
| 180 | private void removeLogs() { |
| 181 | File[] logFiles = logDir.listFiles(); |
| 182 | if (logFiles != null && logFiles.length > 0) { |
| 183 | for (File file : logFiles) { |
| 184 | if (!file.delete()) { |
| 185 | print("Unable to delete log file %s", file); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 191 | } |