blob: 5fba98c044d65daa5788b8b78ca5d0353b9911a4 [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
21/**
22 * Message handler specifically for the chart views.
23 */
24public abstract class ChartRequestHandler extends RequestHandler {
25
26 private final String respType;
27 private final String nodeName;
28
29 /**
30 * Constructs a chart model handler for a specific graph view. When chart
31 * requests come in, the handler will generate the appropriate chart data
32 * points and send back the response to the client.
33 *
34 * @param reqType type of the request event
35 * @param respType type of the response event
36 * @param nodeName name of JSON node holding data point
37 */
38 public ChartRequestHandler(String reqType, String respType, String nodeName) {
39 super(reqType);
40 this.respType = respType;
41 this.nodeName = nodeName;
42 }
43
44 @Override
45 public void process(long sid, ObjectNode payload) {
46 ChartModel cm = createChartModel();
47 populateChart(cm, payload);
48
49 ObjectNode rootNode = MAPPER.createObjectNode();
50 rootNode.set(nodeName, ChartUtils.generateDataPointArrayNode(cm));
51 sendMessage(respType, 0, rootNode);
52 }
53
54 /**
55 * Creates the chart model using {@link #getSeries()}
56 * to initialize it, ready to be populated.
57 * <p>
58 * This default implementation returns a chart model for all series.
59 * </p>
60 *
61 * @return an empty chart model
62 */
63 protected ChartModel createChartModel() {
64 return new ChartModel(getSeries());
65 }
66
67 /**
68 * Subclasses should return the array of series with which to initialize
69 * their chart model.
70 *
71 * @return the series name
72 */
73 protected abstract String[] getSeries();
74
75 /**
76 * Subclasses should populate the chart model by adding
77 * {@link ChartModel.DataPoint datapoints}.
78 * <pre>
79 * cm.addDataPoint()
80 * .data(SERIES_ONE, ...)
81 * .data(SERIES_TWO, ...)
82 * ... ;
83 * </pre>
84 * The request payload is provided in case there are request filtering
85 * parameters.
86 *
87 * @param cm the chart model
88 * @param payload request payload
89 */
90 protected abstract void populateChart(ChartModel cm, ObjectNode payload);
91}