blob: 404e95455700dc9758b4bfa01c9977f0a26100c0 [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
30 private JsonUtils() { }
31
32 /**
33 * Wraps a message payload into an event structure for the given event
34 * type and sequence ID. Generally, the sequence ID should be a copy of
35 * the ID from the client request event.
36 *
37 * @param type event type
38 * @param sid sequence ID
39 * @param payload event payload
40 * @return the object node representation
41 */
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 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 */
84 public static long sid(ObjectNode event) {
85 return number(event, "sid");
86 }
87
88 /**
Simon Hunt44aa2f82015-04-30 15:01:35 -070089 * Returns the payload from the specified event.
90 *
91 * @param event message event
92 * @return extracted payload object
93 */
94 public static ObjectNode payload(ObjectNode event) {
95 return (ObjectNode) event.path("payload");
96 }
97
98 /**
99 * Returns the specified node property as a number.
100 *
101 * @param node message event
102 * @param name property name
103 * @return property as number
104 */
105 public static long number(ObjectNode node, String name) {
106 return node.path(name).asLong();
107 }
108
109 /**
110 * Returns the specified node property as a string.
111 *
112 * @param node message event
113 * @param name property name
114 * @return property as a string
115 */
116 public static String string(ObjectNode node, String name) {
117 return node.path(name).asText();
118 }
119
120 /**
121 * Returns the specified node property as a string, with a default fallback.
122 *
Simon Huntd2747a02015-04-30 22:41:16 -0700123 * @param node object node
Simon Hunt44aa2f82015-04-30 15:01:35 -0700124 * @param name property name
125 * @param defaultValue fallback value if property is absent
126 * @return property as a string
127 */
128 public static String string(ObjectNode node, String name, String defaultValue) {
129 return node.path(name).asText(defaultValue);
130 }
131
Simon Huntd2747a02015-04-30 22:41:16 -0700132 /**
133 * Returns the specified node property as an object node.
134 *
135 * @param node object node
136 * @param name property name
137 * @return property as a node
138 */
139 public static ObjectNode node(ObjectNode node, String name) {
140 return (ObjectNode) node.path(name);
141 }
142
Simon Hunt44aa2f82015-04-30 15:01:35 -0700143}