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 | |
| 18 | import java.io.FileInputStream; |
| 19 | import java.io.FileNotFoundException; |
| 20 | import java.text.SimpleDateFormat; |
| 21 | import java.util.Date; |
| 22 | |
| 23 | import static org.onlab.stc.Coordinator.print; |
| 24 | |
| 25 | /** |
| 26 | * Main program for executing system test coordinator. |
| 27 | */ |
| 28 | public final class Main { |
| 29 | |
| 30 | private enum Command { |
| 31 | LIST, RUN, RUN_FROM, RUN_TO |
| 32 | } |
| 33 | |
| 34 | private final String[] args; |
| 35 | private final Command command; |
| 36 | private final String scenarioFile; |
| 37 | |
| 38 | private Scenario scenario; |
| 39 | private Coordinator coordinator; |
| 40 | private Listener delegate = new Listener(); |
| 41 | |
| 42 | // Public construction forbidden |
| 43 | private Main(String[] args) { |
| 44 | this.args = args; |
| 45 | this.scenarioFile = args[0]; |
| 46 | this.command = Command.valueOf("RUN"); |
| 47 | } |
| 48 | |
| 49 | // usage: stc [<command>] [<scenario-file>] |
| 50 | // --list |
| 51 | // [--run] |
| 52 | // --run-from <step>,... |
| 53 | // --run-to <step>,... |
| 54 | |
| 55 | /** |
| 56 | * Main entry point for coordinating test scenario execution. |
| 57 | * |
| 58 | * @param args command-line arguments |
| 59 | */ |
| 60 | public static void main(String[] args) { |
| 61 | Main main = new Main(args); |
| 62 | main.run(); |
| 63 | } |
| 64 | |
| 65 | private void run() { |
| 66 | try { |
| 67 | // Load scenario |
| 68 | scenario = Scenario.loadScenario(new FileInputStream(scenarioFile)); |
| 69 | |
| 70 | // Elaborate scenario |
| 71 | Compiler compiler = new Compiler(scenario); |
| 72 | compiler.compile(); |
| 73 | |
| 74 | // Execute process flow |
| 75 | coordinator = new Coordinator(scenario, compiler.processFlow(), |
| 76 | compiler.logDir()); |
| 77 | coordinator.addListener(delegate); |
| 78 | processCommand(); |
| 79 | |
| 80 | } catch (FileNotFoundException e) { |
| 81 | print("Unable to find scenario file %s", scenarioFile); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private void processCommand() { |
| 86 | switch (command) { |
| 87 | case RUN: |
| 88 | processRun(); |
| 89 | default: |
| 90 | print("Unsupported command"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private void processRun() { |
| 95 | try { |
| 96 | coordinator.start(); |
| 97 | int exitCode = coordinator.waitFor(); |
| 98 | pause(100); // allow stdout to flush |
| 99 | System.exit(exitCode); |
| 100 | } catch (InterruptedException e) { |
| 101 | print("Unable to execute scenario %s", scenarioFile); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private void pause(int ms) { |
| 106 | try { |
| 107 | Thread.sleep(ms); |
| 108 | } catch (InterruptedException e) { |
| 109 | print("Interrupted!"); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Internal delegate to monitor the process execution. |
| 116 | */ |
| 117 | private class Listener implements StepProcessListener { |
| 118 | |
| 119 | @Override |
| 120 | public void onStart(Step step) { |
| 121 | print("%s %s started", now(), step.name()); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onCompletion(Step step, int exitCode) { |
| 126 | print("%s %s %s", now(), step.name(), exitCode == 0 ? "completed" : "failed"); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void onOutput(Step step, String line) { |
| 131 | } |
| 132 | |
| 133 | } |
| 134 | |
| 135 | private String now() { |
| 136 | return new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()); |
| 137 | } |
| 138 | |
| 139 | } |