blob: ce840bd0b500b91f4b8ff05bb8324e7318a0c7c3 [file] [log] [blame]
Simon Huntc54cd1b2015-05-11 13:43:44 -07001/*
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 */
17
18package org.onosproject.ui.impl.topo.overlay;
19
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23
24import java.text.DecimalFormat;
25import java.util.ArrayList;
26import java.util.List;
27
28/**
29 * Base implementation of a {@link SummaryGenerator}. Provides convenience
30 * methods for compiling a list of properties to be displayed in the summary
31 * panel on the UI.
32 */
33public abstract class AbstractSummaryGenerator implements SummaryGenerator {
34 private static final String NUMBER_FORMAT = "#,###";
35 private static final DecimalFormat DF = new DecimalFormat(NUMBER_FORMAT);
36 private static final ObjectMapper MAPPER = new ObjectMapper();
37
38 private final List<Prop> props = new ArrayList<>();
39 private String iconId;
40 private String title;
41
42 /**
43 * Constructs a summary generator without specifying the icon ID or title.
44 * It is expected that the title (and optionally the icon ID) will be set
45 * later via {@link #title(String)} (and {@link #iconId(String)}), before
46 * {@link #buildObjectNode()} is invoked to generate the message payload.
47 */
48 public AbstractSummaryGenerator() {
49 }
50
51 /**
52 * Constructs a summary generator that uses the specified iconId ID and
53 * title in its generated output.
54 *
55 * @param iconId iconId ID
56 * @param title title
57 */
58 public AbstractSummaryGenerator(String iconId, String title) {
59 this.iconId = iconId;
60 this.title = title;
61 }
62
63 /**
64 * Subclasses need to provide an implementation.
65 *
66 * @return the summary payload
67 */
68 @Override
69 public abstract ObjectNode generateSummary();
70
71 /**
72 * Formats the given number into a string, using comma separator.
73 *
74 * @param number the number
75 * @return formatted as a string
76 */
77 protected String format(Number number) {
78 return DF.format(number);
79 }
80
81 /**
82 * Sets the iconId ID to use.
83 *
84 * @param iconId iconId ID
85 */
86 protected void iconId(String iconId) {
87 this.iconId = iconId;
88 }
89
90 /**
91 * Sets the summary panel title.
92 *
93 * @param title the title
94 */
95 protected void title(String title) {
96 this.title = title;
97 }
98
99 /**
100 * Clears out the cache of properties.
101 */
102 protected void clearProps() {
103 props.clear();
104 }
105
106 /**
107 * Adds a property to the summary. Note that the value is converted to
108 * a string by invoking the <code>toString()</code> method on it.
109 *
110 * @param label the label
111 * @param value the value
112 */
113 protected void prop(String label, Object value) {
114 props.add(new Prop(label, value));
115 }
116
117 /**
118 * Adds a separator to the summary; when rendered on the client, a visible
119 * break between properties.
120 */
121 protected void separator() {
122 props.add(new Prop("-", ""));
123 }
124
125 /**
126 * Builds an object node from the current state of the summary generator.
127 *
128 * @return summary payload as JSON object node
129 */
130 protected ObjectNode buildObjectNode() {
131 ObjectNode result = MAPPER.createObjectNode();
132 // NOTE: "id" and "type" are currently used for title and iconID
133 // so that this structure can be "re-used" with detail panel payloads
134 result.put("id", title).put("type", iconId);
135
136 ObjectNode pnode = MAPPER.createObjectNode();
137 ArrayNode porder = MAPPER.createArrayNode();
138
139 for (Prop p : props) {
140 porder.add(p.label);
141 pnode.put(p.label, p.value);
142 }
143 result.set("propOrder", porder);
144 result.set("props", pnode);
145
146 return result;
147 }
148
149 // ===================================================================
150
151 /**
152 * Abstraction of a property, that is, a label-value pair.
153 */
154 private static class Prop {
155 private final String label;
156 private final String value;
157
158 public Prop(String label, Object value) {
159 this.label = label;
160 this.value = value.toString();
161 }
162 }
163}