Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 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 | package org.onosproject.ui; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | |
| 22 | /** |
| 23 | * Provides convenience methods for dealing with JSON nodes, arrays etc. |
| 24 | */ |
| 25 | public final class JsonUtils { |
| 26 | |
| 27 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 28 | |
| 29 | // non-instantiable |
Simon Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 30 | private JsonUtils() { |
| 31 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 34 | * Composes a message structure for the given message type and payload. |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 35 | * |
| 36 | * @param type event type |
| 37 | * @param sid sequence ID |
| 38 | * @param payload event payload |
| 39 | * @return the object node representation |
| 40 | */ |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 41 | @Deprecated |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 42 | public static ObjectNode envelope(String type, long sid, ObjectNode payload) { |
| 43 | ObjectNode event = MAPPER.createObjectNode(); |
| 44 | event.put("event", type); |
| 45 | if (sid > 0) { |
| 46 | event.put("sid", sid); |
| 47 | } |
| 48 | event.set("payload", payload); |
| 49 | return event; |
| 50 | } |
| 51 | |
| 52 | /** |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 53 | * Composes a message structure for the given message type and payload. |
| 54 | * |
| 55 | * @param type message type |
| 56 | * @param payload message payload |
| 57 | * @return the object node representation |
| 58 | */ |
| 59 | public static ObjectNode envelope(String type, ObjectNode payload) { |
| 60 | ObjectNode event = MAPPER.createObjectNode(); |
| 61 | event.put("event", type); |
| 62 | event.set("payload", payload); |
| 63 | return event; |
| 64 | } |
| 65 | |
| 66 | /** |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 67 | * Returns the event type from the specified event. |
| 68 | * If the node does not have an "event" property, "unknown" is returned. |
| 69 | * |
| 70 | * @param event message event |
| 71 | * @return extracted event type |
| 72 | */ |
| 73 | public static String eventType(ObjectNode event) { |
| 74 | return string(event, "event", "unknown"); |
| 75 | } |
| 76 | |
| 77 | /** |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 78 | * Returns the sequence identifier from the specified event, or 0 (zero) |
| 79 | * if the "sid" property does not exist. |
| 80 | * |
| 81 | * @param event message event |
| 82 | * @return extracted sequence identifier |
| 83 | */ |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 84 | @Deprecated |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 85 | public static long sid(ObjectNode event) { |
| 86 | return number(event, "sid"); |
| 87 | } |
| 88 | |
| 89 | /** |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 90 | * Returns the payload from the specified event. |
| 91 | * |
| 92 | * @param event message event |
| 93 | * @return extracted payload object |
| 94 | */ |
| 95 | public static ObjectNode payload(ObjectNode event) { |
| 96 | return (ObjectNode) event.path("payload"); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the specified node property as a number. |
| 101 | * |
Simon Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 102 | * @param node object node |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 103 | * @param name property name |
| 104 | * @return property as number |
| 105 | */ |
| 106 | public static long number(ObjectNode node, String name) { |
| 107 | return node.path(name).asLong(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the specified node property as a string. |
| 112 | * |
Simon Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 113 | * @param node object node |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 114 | * @param name property name |
| 115 | * @return property as a string |
| 116 | */ |
| 117 | public static String string(ObjectNode node, String name) { |
| 118 | return node.path(name).asText(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the specified node property as a string, with a default fallback. |
| 123 | * |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 124 | * @param node object node |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 125 | * @param name property name |
| 126 | * @param defaultValue fallback value if property is absent |
| 127 | * @return property as a string |
| 128 | */ |
| 129 | public static String string(ObjectNode node, String name, String defaultValue) { |
| 130 | return node.path(name).asText(defaultValue); |
| 131 | } |
| 132 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Returns the specified node property as an object node. |
| 135 | * |
| 136 | * @param node object node |
| 137 | * @param name property name |
| 138 | * @return property as a node |
| 139 | */ |
| 140 | public static ObjectNode node(ObjectNode node, String name) { |
| 141 | return (ObjectNode) node.path(name); |
| 142 | } |
| 143 | |
Simon Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Returns the specified node property as a boolean. |
| 146 | * |
| 147 | * @param node object node |
| 148 | * @param name property name |
| 149 | * @return property as a boolean |
| 150 | */ |
| 151 | public static boolean bool(ObjectNode node, String name) { |
| 152 | return node.path(name).asBoolean(); |
| 153 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 154 | } |