blob: 371e0d240f257559fb8fcb9be196bbaf0f7c970a [file] [log] [blame]
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -07003 *
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.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.Maps;
22import org.onlab.stc.MonitorLayout.Box;
23
24import java.io.FileWriter;
25import java.io.IOException;
26import java.io.PrintWriter;
27import java.util.Map;
28
29import static org.onlab.stc.Coordinator.Status.IN_PROGRESS;
30
31/**
32 * Scenario test monitor.
33 */
34public class Monitor implements StepProcessListener {
35
36 private final ObjectMapper mapper = new ObjectMapper();
37
38 private final Coordinator coordinator;
39 private final Compiler compiler;
40 private final MonitorLayout layout;
41
42 private MonitorDelegate delegate;
43
44 private Map<Step, Box> boxes = Maps.newHashMap();
45
46 /**
47 * Creates a new shared process flow monitor.
48 *
49 * @param coordinator process flow coordinator
50 * @param compiler scenario compiler
51 */
52 Monitor(Coordinator coordinator, Compiler compiler) {
53 this.coordinator = coordinator;
54 this.compiler = compiler;
55 this.layout = new MonitorLayout(compiler);
56 coordinator.addListener(this);
57 }
58
59 /**
60 * Sets the process monitor delegate.
61 *
62 * @param delegate process monitor delegate
63 */
64 void setDelegate(MonitorDelegate delegate) {
65 this.delegate = delegate;
66 }
67
68 /**
69 * Notifies the process monitor delegate with the specified event.
70 *
71 * @param event JSON event data
72 */
73 public void notify(ObjectNode event) {
74 if (delegate != null) {
75 delegate.notify(event);
76 }
77 }
78
79 /**
80 * Returns the scenario process flow as JSON data.
81 *
82 * @return scenario process flow data
83 */
84 ObjectNode scenarioData() {
85 ObjectNode root = mapper.createObjectNode();
86 ArrayNode steps = mapper.createArrayNode();
87 ArrayNode requirements = mapper.createArrayNode();
88
89 ProcessFlow pf = compiler.processFlow();
90 pf.getVertexes().forEach(step -> add(step, steps));
91 pf.getEdges().forEach(requirement -> add(requirement, requirements));
92
93 root.set("steps", steps);
94 root.set("requirements", requirements);
95
96 try (FileWriter fw = new FileWriter("/tmp/data.json");
97 PrintWriter pw = new PrintWriter(fw)) {
98 pw.println(root.toString());
99 } catch (IOException e) {
100 e.printStackTrace();
101 }
102 return root;
103 }
104
105
106 private void add(Step step, ArrayNode steps) {
107 Box box = layout.get(step);
108 ObjectNode sn = mapper.createObjectNode()
109 .put("name", step.name())
110 .put("isGroup", step instanceof Group)
111 .put("status", status(coordinator.getStatus(step)))
112 .put("tier", box.tier())
113 .put("depth", box.depth());
114 if (step.group() != null) {
115 sn.put("group", step.group().name());
116 }
117 steps.add(sn);
118 }
119
120 private String status(Coordinator.Status status) {
121 return status.toString().toLowerCase();
122 }
123
124 private void add(Dependency requirement, ArrayNode requirements) {
125 ObjectNode rn = mapper.createObjectNode();
126 rn.put("src", requirement.src().name())
127 .put("dst", requirement.dst().name())
128 .put("isSoft", requirement.isSoft());
129 requirements.add(rn);
130 }
131
132 @Override
133 public void onStart(Step step, String command) {
134 notify(event(step, status(IN_PROGRESS)));
135 }
136
137 @Override
138 public void onCompletion(Step step, Coordinator.Status status) {
139 notify(event(step, status(status)));
140 }
141
142 @Override
143 public void onOutput(Step step, String line) {
144
145 }
146
147 private ObjectNode event(Step step, String status) {
148 ObjectNode event = mapper.createObjectNode()
149 .put("name", step.name())
150 .put("status", status);
151 return event;
152 }
153
154}