blob: 5c3e2f7aa07b390cc06b9e9bc2b54388ec7a9cfd [file] [log] [blame]
Jian Li15468822016-04-15 16:28:11 -07001/*
2 * Copyright 2016-present 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.onosproject.ui.chart;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.ui.RequestHandler;
20
Jian Li1aa07822016-04-19 17:58:02 -070021import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.List;
24
Jian Li15468822016-04-15 16:28:11 -070025/**
26 * Message handler specifically for the chart views.
27 */
28public abstract class ChartRequestHandler extends RequestHandler {
29
Simon Huntb0582492016-09-20 18:26:38 -070030 protected static final String LABEL = "label";
31 private static final String ANNOTS = "annots";
32
Jian Li15468822016-04-15 16:28:11 -070033 private final String respType;
34 private final String nodeName;
Jian Li1077dd72016-04-26 11:10:20 -070035
Jian Li15468822016-04-15 16:28:11 -070036 /**
37 * Constructs a chart model handler for a specific graph view. When chart
38 * requests come in, the handler will generate the appropriate chart data
39 * points and send back the response to the client.
40 *
41 * @param reqType type of the request event
42 * @param respType type of the response event
43 * @param nodeName name of JSON node holding data point
44 */
45 public ChartRequestHandler(String reqType, String respType, String nodeName) {
46 super(reqType);
47 this.respType = respType;
48 this.nodeName = nodeName;
49 }
50
51 @Override
52 public void process(long sid, ObjectNode payload) {
53 ChartModel cm = createChartModel();
54 populateChart(cm, payload);
55
56 ObjectNode rootNode = MAPPER.createObjectNode();
57 rootNode.set(nodeName, ChartUtils.generateDataPointArrayNode(cm));
Jian Li1077dd72016-04-26 11:10:20 -070058 rootNode.set(ANNOTS, ChartUtils.generateAnnotObjectNode(cm));
Simon Huntb0582492016-09-20 18:26:38 -070059 sendMessage(respType, rootNode);
Jian Li15468822016-04-15 16:28:11 -070060 }
61
62 /**
63 * Creates the chart model using {@link #getSeries()}
64 * to initialize it, ready to be populated.
65 * <p>
66 * This default implementation returns a chart model for all series.
67 * </p>
68 *
69 * @return an empty chart model
70 */
71 protected ChartModel createChartModel() {
Jian Li1aa07822016-04-19 17:58:02 -070072 List<String> series = new ArrayList<>();
73 series.addAll(Arrays.asList(getSeries()));
74 series.add(LABEL);
Simon Huntb0582492016-09-20 18:26:38 -070075 String[] array = new String[series.size()];
76 return new ChartModel(series.toArray(array));
Jian Li15468822016-04-15 16:28:11 -070077 }
78
79 /**
80 * Subclasses should return the array of series with which to initialize
81 * their chart model.
82 *
83 * @return the series name
84 */
85 protected abstract String[] getSeries();
86
87 /**
88 * Subclasses should populate the chart model by adding
89 * {@link ChartModel.DataPoint datapoints}.
90 * <pre>
91 * cm.addDataPoint()
92 * .data(SERIES_ONE, ...)
93 * .data(SERIES_TWO, ...)
94 * ... ;
95 * </pre>
96 * The request payload is provided in case there are request filtering
97 * parameters.
98 *
99 * @param cm the chart model
100 * @param payload request payload
101 */
102 protected abstract void populateChart(ChartModel cm, ObjectNode payload);
103}