Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.ui.chart; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.ui.RequestHandler; |
| 20 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.Arrays; |
| 23 | import java.util.List; |
| 24 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Message handler specifically for the chart views. |
| 27 | */ |
| 28 | public abstract class ChartRequestHandler extends RequestHandler { |
| 29 | |
| 30 | private final String respType; |
| 31 | private final String nodeName; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 32 | protected static final String LABEL = "label"; |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 33 | |
Jian Li | 1077dd7 | 2016-04-26 11:10:20 -0700 | [diff] [blame] | 34 | private static final String ANNOTS = "annots"; |
| 35 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 36 | /** |
| 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 Li | 1077dd7 | 2016-04-26 11:10:20 -0700 | [diff] [blame] | 58 | rootNode.set(ANNOTS, ChartUtils.generateAnnotObjectNode(cm)); |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 59 | sendMessage(respType, 0, rootNode); |
| 60 | } |
| 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 Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 72 | List<String> series = new ArrayList<>(); |
| 73 | series.addAll(Arrays.asList(getSeries())); |
| 74 | series.add(LABEL); |
| 75 | String[] seiresArray = new String[series.size()]; |
| 76 | return new ChartModel(series.toArray(seiresArray)); |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 77 | } |
| 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 | } |