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 | |
| 18 | import org.apache.commons.configuration.ConfigurationException; |
| 19 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 20 | import org.apache.commons.configuration.XMLConfiguration; |
| 21 | |
| 22 | import java.io.InputStream; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | import static com.google.common.base.Preconditions.checkState; |
| 26 | |
| 27 | /** |
| 28 | * Representation of a re-usable test scenario. |
| 29 | */ |
| 30 | public final class Scenario { |
| 31 | |
| 32 | private static final String SCENARIO = "scenario"; |
| 33 | private static final String NAME = "[@name]"; |
| 34 | private static final String DESCRIPTION = "[@description]"; |
| 35 | |
| 36 | private final String name; |
| 37 | private final String description; |
| 38 | private final HierarchicalConfiguration definition; |
| 39 | |
| 40 | // Creates a new scenario from the specified definition. |
| 41 | private Scenario(String name, String description, HierarchicalConfiguration definition) { |
| 42 | this.name = checkNotNull(name, "Name cannot be null"); |
| 43 | this.description = checkNotNull(description, "Description cannot be null"); |
| 44 | this.definition = checkNotNull(definition, "Definition cannot be null"); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Loads a new scenario from the specified hierarchical configuration. |
| 49 | * |
| 50 | * @param definition scenario definition |
| 51 | * @return loaded scenario |
| 52 | */ |
| 53 | public static Scenario loadScenario(HierarchicalConfiguration definition) { |
| 54 | String name = definition.getString(NAME); |
| 55 | String description = definition.getString(DESCRIPTION, ""); |
| 56 | checkState(name != null, "Scenario name must be specified"); |
| 57 | return new Scenario(name, description, definition); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Loads a new scenario from the specified input stream. |
| 62 | * |
| 63 | * @param stream scenario definition stream |
| 64 | * @return loaded scenario |
| 65 | */ |
| 66 | public static Scenario loadScenario(InputStream stream) { |
| 67 | XMLConfiguration cfg = new XMLConfiguration(); |
| 68 | cfg.setAttributeSplittingDisabled(true); |
| 69 | cfg.setDelimiterParsingDisabled(true); |
| 70 | cfg.setRootElementName(SCENARIO); |
| 71 | try { |
| 72 | cfg.load(stream); |
| 73 | return loadScenario(cfg); |
| 74 | } catch (ConfigurationException e) { |
| 75 | throw new IllegalArgumentException("Unable to load scenario from the stream", e); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the scenario name. |
| 81 | * |
| 82 | * @return scenario name |
| 83 | */ |
| 84 | public String name() { |
| 85 | return name; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the scenario description. |
| 90 | * |
| 91 | * @return scenario description |
| 92 | */ |
| 93 | public String description() { |
| 94 | return description; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the scenario definition. |
| 99 | * |
| 100 | * @return scenario definition |
| 101 | */ |
| 102 | public HierarchicalConfiguration definition() { |
| 103 | return definition; |
| 104 | } |
| 105 | |
| 106 | } |