blob: ea560a751344cf2cdffa867c456fc329910cfb63 [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 /**
34 * Wraps a message payload into an event structure for the given event
35 * type and sequence ID. Generally, the sequence ID should be a copy of
36 * the ID from the client request event.
37 *
38 * @param type event type
39 * @param sid sequence ID
40 * @param payload event payload
41 * @return the object node representation
42 */
43 public static ObjectNode envelope(String type, long sid, ObjectNode payload) {
44 ObjectNode event = MAPPER.createObjectNode();
45 event.put("event", type);
46 if (sid > 0) {
47 event.put("sid", sid);
48 }
49 event.set("payload", payload);
50 return event;
51 }
52
53 /**
Simon Huntd7f7bcc2015-05-08 14:13:17 -070054 * Composes a message structure for the given message type and payload.
55 *
56 * @param type message type
57 * @param payload message payload
58 * @return the object node representation
59 */
60 public static ObjectNode envelope(String type, ObjectNode payload) {
61 ObjectNode event = MAPPER.createObjectNode();
62 event.put("event", type);
63 event.set("payload", payload);
64 return event;
65 }
66
67 /**
Simon Hunt44aa2f82015-04-30 15:01:35 -070068 * Returns the event type from the specified event.
69 * If the node does not have an "event" property, "unknown" is returned.
70 *
71 * @param event message event
72 * @return extracted event type
73 */
74 public static String eventType(ObjectNode event) {
75 return string(event, "event", "unknown");
76 }
77
78 /**
Simon Huntd2747a02015-04-30 22:41:16 -070079 * Returns the sequence identifier from the specified event, or 0 (zero)
80 * if the "sid" property does not exist.
81 *
82 * @param event message event
83 * @return extracted sequence identifier
84 */
85 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}