blob: f27c6e784e41ac8cfa586eff76602979013f5198 [file] [log] [blame]
Simon Hunt44aa2f82015-04-30 15:01:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt44aa2f82015-04-30 15:01:35 -07003 *
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
17package org.onosproject.ui;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21
22/**
23 * Provides convenience methods for dealing with JSON nodes, arrays etc.
24 */
25public final class JsonUtils {
26
27 private static final ObjectMapper MAPPER = new ObjectMapper();
28
29 // non-instantiable
Simon Huntb1ced8e2016-05-27 19:34:34 -070030 private JsonUtils() {
31 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070032
33 /**
Simon Hunt8add9ee2016-09-20 17:05:07 -070034 * Composes a message structure for the given message type and payload.
Simon Hunt44aa2f82015-04-30 15:01:35 -070035 *
36 * @param type event type
37 * @param sid sequence ID
38 * @param payload event payload
39 * @return the object node representation
40 */
Simon Hunt8add9ee2016-09-20 17:05:07 -070041 @Deprecated
Simon Hunt44aa2f82015-04-30 15:01:35 -070042 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 Huntd7f7bcc2015-05-08 14:13:17 -070053 * 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 Hunt44aa2f82015-04-30 15:01:35 -070067 * 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 Huntd2747a02015-04-30 22:41:16 -070078 * 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 Hunt8add9ee2016-09-20 17:05:07 -070084 @Deprecated
Simon Huntd2747a02015-04-30 22:41:16 -070085 public static long sid(ObjectNode event) {
86 return number(event, "sid");
87 }
88
89 /**
Simon Hunt44aa2f82015-04-30 15:01:35 -070090 * 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 Huntb1ced8e2016-05-27 19:34:34 -0700102 * @param node object node
Simon Hunt44aa2f82015-04-30 15:01:35 -0700103 * @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 Huntb1ced8e2016-05-27 19:34:34 -0700113 * @param node object node
Simon Hunt44aa2f82015-04-30 15:01:35 -0700114 * @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 Huntd2747a02015-04-30 22:41:16 -0700124 * @param node object node
Simon Hunt44aa2f82015-04-30 15:01:35 -0700125 * @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 Huntd2747a02015-04-30 22:41:16 -0700133 /**
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 Huntb1ced8e2016-05-27 19:34:34 -0700144 /**
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 Hunt44aa2f82015-04-30 15:01:35 -0700154}