blob: 9d3054f28787d10b416f8853673aee20f5660a64 [file] [log] [blame]
Simon Hunt44aa2f82015-04-30 15:01:35 -07001/*
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
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
Sho SHIMIZUbe63b232015-06-30 10:57:58 -070041 * @deprecated in Cardinal Release
Simon Hunt44aa2f82015-04-30 15:01:35 -070042 */
Simon Huntd7f7bcc2015-05-08 14:13:17 -070043 @Deprecated
Simon Hunt44aa2f82015-04-30 15:01:35 -070044 public static ObjectNode envelope(String type, long sid, ObjectNode payload) {
45 ObjectNode event = MAPPER.createObjectNode();
46 event.put("event", type);
47 if (sid > 0) {
48 event.put("sid", sid);
49 }
50 event.set("payload", payload);
51 return event;
52 }
53
54 /**
Simon Huntd7f7bcc2015-05-08 14:13:17 -070055 * Composes a message structure for the given message type and payload.
56 *
57 * @param type message type
58 * @param payload message payload
59 * @return the object node representation
60 */
61 public static ObjectNode envelope(String type, ObjectNode payload) {
62 ObjectNode event = MAPPER.createObjectNode();
63 event.put("event", type);
64 event.set("payload", payload);
65 return event;
66 }
67
68 /**
Simon Hunt44aa2f82015-04-30 15:01:35 -070069 * Returns the event type from the specified event.
70 * If the node does not have an "event" property, "unknown" is returned.
71 *
72 * @param event message event
73 * @return extracted event type
74 */
75 public static String eventType(ObjectNode event) {
76 return string(event, "event", "unknown");
77 }
78
79 /**
Simon Huntd2747a02015-04-30 22:41:16 -070080 * Returns the sequence identifier from the specified event, or 0 (zero)
81 * if the "sid" property does not exist.
82 *
83 * @param event message event
84 * @return extracted sequence identifier
85 */
86 public static long sid(ObjectNode event) {
87 return number(event, "sid");
88 }
89
90 /**
Simon Hunt44aa2f82015-04-30 15:01:35 -070091 * Returns the payload from the specified event.
92 *
93 * @param event message event
94 * @return extracted payload object
95 */
96 public static ObjectNode payload(ObjectNode event) {
97 return (ObjectNode) event.path("payload");
98 }
99
100 /**
101 * Returns the specified node property as a number.
102 *
103 * @param node message event
104 * @param name property name
105 * @return property as number
106 */
107 public static long number(ObjectNode node, String name) {
108 return node.path(name).asLong();
109 }
110
111 /**
112 * Returns the specified node property as a string.
113 *
114 * @param node message event
115 * @param name property name
116 * @return property as a string
117 */
118 public static String string(ObjectNode node, String name) {
119 return node.path(name).asText();
120 }
121
122 /**
123 * Returns the specified node property as a string, with a default fallback.
124 *
Simon Huntd2747a02015-04-30 22:41:16 -0700125 * @param node object node
Simon Hunt44aa2f82015-04-30 15:01:35 -0700126 * @param name property name
127 * @param defaultValue fallback value if property is absent
128 * @return property as a string
129 */
130 public static String string(ObjectNode node, String name, String defaultValue) {
131 return node.path(name).asText(defaultValue);
132 }
133
Simon Huntd2747a02015-04-30 22:41:16 -0700134 /**
135 * Returns the specified node property as an object node.
136 *
137 * @param node object node
138 * @param name property name
139 * @return property as a node
140 */
141 public static ObjectNode node(ObjectNode node, String name) {
142 return (ObjectNode) node.path(name);
143 }
144
Simon Hunt44aa2f82015-04-30 15:01:35 -0700145}