Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.ui.impl.topo; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | import org.onosproject.ui.topo.ButtonId; |
| 24 | import org.onosproject.ui.topo.DeviceHighlight; |
| 25 | import org.onosproject.ui.topo.Highlights; |
| 26 | import org.onosproject.ui.topo.HostHighlight; |
| 27 | import org.onosproject.ui.topo.LinkHighlight; |
| 28 | import org.onosproject.ui.topo.PropertyPanel; |
| 29 | |
| 30 | /** |
| 31 | * JSON utilities for the Topology View. |
| 32 | */ |
| 33 | public final class TopoJson { |
| 34 | private static final String DEVICES = "devices"; |
| 35 | private static final String HOSTS = "hosts"; |
| 36 | private static final String LINKS = "links"; |
| 37 | |
| 38 | private static final String ID = "id"; |
| 39 | private static final String LABEL = "label"; |
| 40 | private static final String CSS = "css"; |
| 41 | |
| 42 | private static final String TITLE = "title"; |
| 43 | private static final String TYPE = "type"; |
| 44 | private static final String PROP_ORDER = "propOrder"; |
| 45 | private static final String PROPS = "props"; |
| 46 | private static final String BUTTONS = "buttons"; |
| 47 | |
| 48 | |
| 49 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 50 | |
| 51 | private static ObjectNode objectNode() { |
| 52 | return MAPPER.createObjectNode(); |
| 53 | } |
| 54 | |
| 55 | private static ArrayNode arrayNode() { |
| 56 | return MAPPER.createArrayNode(); |
| 57 | } |
| 58 | |
| 59 | // non-instantiable |
| 60 | private TopoJson() { } |
| 61 | |
| 62 | /** |
| 63 | * Transforms the given highlights model into a JSON message payload. |
| 64 | * |
| 65 | * @param highlights the model to transform |
| 66 | * @return JSON payload |
| 67 | */ |
| 68 | public static ObjectNode json(Highlights highlights) { |
| 69 | ObjectNode payload = objectNode(); |
| 70 | |
| 71 | ArrayNode devices = arrayNode(); |
| 72 | ArrayNode hosts = arrayNode(); |
| 73 | ArrayNode links = arrayNode(); |
| 74 | |
| 75 | payload.set(DEVICES, devices); |
| 76 | payload.set(HOSTS, hosts); |
| 77 | payload.set(LINKS, links); |
| 78 | |
| 79 | highlights.devices().forEach(dh -> devices.add(json(dh))); |
| 80 | highlights.hosts().forEach(hh -> hosts.add(json(hh))); |
| 81 | highlights.links().forEach(lh -> links.add(json(lh))); |
| 82 | |
| 83 | return payload; |
| 84 | } |
| 85 | |
| 86 | private static ObjectNode json(DeviceHighlight dh) { |
| 87 | // TODO: implement this once we know what a device highlight looks like |
| 88 | return objectNode(); |
| 89 | } |
| 90 | |
| 91 | private static ObjectNode json(HostHighlight hh) { |
| 92 | // TODO: implement this once we know what a host highlight looks like |
| 93 | return objectNode(); |
| 94 | } |
| 95 | |
| 96 | private static ObjectNode json(LinkHighlight lh) { |
| 97 | return objectNode() |
| 98 | .put(ID, lh.elementId()) |
| 99 | .put(LABEL, lh.label()) |
| 100 | .put(CSS, lh.cssClasses()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Translates the given property panel into JSON, for returning |
| 105 | * to the client. |
| 106 | * |
| 107 | * @param pp the property panel model |
| 108 | * @return JSON payload |
| 109 | */ |
| 110 | public static ObjectNode json(PropertyPanel pp) { |
| 111 | ObjectNode result = objectNode() |
| 112 | .put(TITLE, pp.title()) |
| 113 | .put(TYPE, pp.typeId()) |
| 114 | .put(ID, pp.id()); |
| 115 | |
| 116 | ObjectNode pnode = objectNode(); |
| 117 | ArrayNode porder = arrayNode(); |
| 118 | for (PropertyPanel.Prop p : pp.properties()) { |
| 119 | porder.add(p.key()); |
| 120 | pnode.put(p.key(), p.value()); |
| 121 | } |
| 122 | result.set(PROP_ORDER, porder); |
| 123 | result.set(PROPS, pnode); |
| 124 | |
| 125 | ArrayNode buttons = arrayNode(); |
| 126 | for (ButtonId b : pp.buttons()) { |
| 127 | buttons.add(b.id()); |
| 128 | } |
| 129 | result.set(BUTTONS, buttons); |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | } |