blob: 614afb1a732694c78e701a7648b3f00510840fce [file] [log] [blame]
Thomas Vachuskaf9c84362015-04-15 11:20:45 -07001/*
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 */
16package org.onlab.stc;
17
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070020import com.google.common.collect.Maps;
21import org.apache.commons.configuration.ConfigurationException;
22import org.apache.commons.configuration.PropertiesConfiguration;
23import org.onlab.stc.Coordinator.Status;
24
25import java.io.File;
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070026import java.util.List;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070027import java.util.Map;
28import java.util.Set;
29
30import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070031import static org.onlab.stc.Coordinator.Status.*;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070032import static org.onlab.stc.Coordinator.print;
33
34/**
35 * Maintains state of scenario execution.
36 */
37class ScenarioStore {
38
39 private final ProcessFlow processFlow;
40 private final File storeFile;
41
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070042 private final List<StepEvent> events = Lists.newArrayList();
43 private final Map<String, Status> statusMap = Maps.newConcurrentMap();
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070044
45 /**
46 * Creates a new scenario store for the specified process flow.
47 *
48 * @param processFlow scenario process flow
49 * @param logDir scenario log directory
50 * @param name scenario name
51 */
52 ScenarioStore(ProcessFlow processFlow, File logDir, String name) {
53 this.processFlow = processFlow;
54 this.storeFile = new File(logDir, name + ".stc");
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070055 load();
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070056 }
57
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070058 /**
59 * Resets status of all steps to waiting and clears all events.
60 */
61 void reset() {
62 events.clear();
63 statusMap.clear();
64 processFlow.getVertexes().forEach(step -> statusMap.put(step.name(), WAITING));
65 try {
66 PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile);
67 cfg.clear();
68 cfg.save();
69 } catch (ConfigurationException e) {
70 print("Unable to store file %s", storeFile);
71 }
72
73 }
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070074
75 /**
76 * Returns set of all test steps.
77 *
78 * @return set of steps
79 */
80 Set<Step> getSteps() {
81 return processFlow.getVertexes();
82 }
83
84 /**
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070085 * Returns a chronological list of step or group records.
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070086 *
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070087 * @return list of events
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070088 */
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070089 synchronized List<StepEvent> getEvents() {
90 return ImmutableList.copyOf(events);
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070091 }
92
93 /**
Thomas Vachuska50ec1af2015-06-02 00:42:52 -070094 * Returns the status record of the specified test step.
95 *
96 * @param step test step or group
97 * @return step status record
98 */
99 Status getStatus(Step step) {
100 return checkNotNull(statusMap.get(step.name()), "Step %s not found", step.name());
101 }
102
103 /**
104 * Marks the specified test step as being in progress.
105 *
106 * @param step test step or group
107 */
108 synchronized void markStarted(Step step) {
109 add(new StepEvent(step.name(), IN_PROGRESS));
110 save();
111 }
112
113 /**
114 * Marks the specified test step as being complete.
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700115 *
116 * @param step test step or group
117 * @param status new step status
118 */
Thomas Vachuska50ec1af2015-06-02 00:42:52 -0700119 synchronized void markComplete(Step step, Status status) {
120 add(new StepEvent(step.name(), status));
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700121 save();
122 }
123
124 /**
125 * Indicates whether there are any failures.
126 *
127 * @return true if there are failed steps
128 */
129 boolean hasFailures() {
Thomas Vachuska50ec1af2015-06-02 00:42:52 -0700130 for (Status status : statusMap.values()) {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700131 if (status == FAILED) {
132 return true;
133 }
134 }
135 return false;
136 }
137
138 /**
Thomas Vachuska50ec1af2015-06-02 00:42:52 -0700139 * Registers a new step record.
140 *
141 * @param event step event
142 */
143 private synchronized void add(StepEvent event) {
144 events.add(event);
145 statusMap.put(event.name(), event.status());
146 }
147
148 /**
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700149 * Loads the states from disk.
150 */
151 private void load() {
Thomas Vachuska50ec1af2015-06-02 00:42:52 -0700152 try {
153 PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile);
154 cfg.getKeys().forEachRemaining(prop -> add(StepEvent.fromString(cfg.getString(prop))));
155 cfg.save();
156 } catch (ConfigurationException e) {
157 print("Unable to store file %s", storeFile);
158 }
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700159 }
160
161 /**
162 * Saves the states to disk.
163 */
164 private void save() {
165 try {
166 PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile);
Thomas Vachuska50ec1af2015-06-02 00:42:52 -0700167 events.forEach(event -> cfg.setProperty("T" + event.time(), event.toString()));
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700168 cfg.save();
169 } catch (ConfigurationException e) {
170 print("Unable to store file %s", storeFile);
171 }
172 }
173
174}