blob: 22ca4522ccf95cd05c99d3a76e9ef47dcc98e17f [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
18import com.google.common.collect.Maps;
19import org.apache.commons.configuration.ConfigurationException;
20import org.apache.commons.configuration.PropertiesConfiguration;
21import org.onlab.stc.Coordinator.Status;
22
23import java.io.File;
24import java.util.Map;
25import java.util.Set;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28import static org.onlab.stc.Coordinator.Status.FAILED;
29import static org.onlab.stc.Coordinator.Status.WAITING;
30import static org.onlab.stc.Coordinator.print;
31
32/**
33 * Maintains state of scenario execution.
34 */
35class ScenarioStore {
36
37 private final ProcessFlow processFlow;
38 private final File storeFile;
39
40 private final Map<Step, Status> stepStatus = Maps.newConcurrentMap();
41
42 /**
43 * Creates a new scenario store for the specified process flow.
44 *
45 * @param processFlow scenario process flow
46 * @param logDir scenario log directory
47 * @param name scenario name
48 */
49 ScenarioStore(ProcessFlow processFlow, File logDir, String name) {
50 this.processFlow = processFlow;
51 this.storeFile = new File(logDir, name + ".stc");
52 processFlow.getVertexes().forEach(step -> stepStatus.put(step, WAITING));
53 }
54
55
56 /**
57 * Returns set of all test steps.
58 *
59 * @return set of steps
60 */
61 Set<Step> getSteps() {
62 return processFlow.getVertexes();
63 }
64
65 /**
66 * Returns the status of the specified test step.
67 *
68 * @param step test step or group
69 * @return step status
70 */
71 Status getStatus(Step step) {
72 return checkNotNull(stepStatus.get(step), "Step %s not found", step.name());
73 }
74
75 /**
76 * Updates the status of the specified test step.
77 *
78 * @param step test step or group
79 * @param status new step status
80 */
81 void updateStatus(Step step, Status status) {
82 stepStatus.put(step, status);
83 save();
84 }
85
86 /**
87 * Indicates whether there are any failures.
88 *
89 * @return true if there are failed steps
90 */
91 boolean hasFailures() {
92 for (Status status : stepStatus.values()) {
93 if (status == FAILED) {
94 return true;
95 }
96 }
97 return false;
98 }
99
100 /**
101 * Loads the states from disk.
102 */
103 private void load() {
104 // FIXME: implement this
105 }
106
107 /**
108 * Saves the states to disk.
109 */
110 private void save() {
111 try {
112 PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile);
113 stepStatus.forEach((step, status) -> cfg.setProperty(step.name(), status));
114 cfg.save();
115 } catch (ConfigurationException e) {
116 print("Unable to store file %s", storeFile);
117 }
118 }
119
120}