blob: db753e275ea2c51de8c6f008e1c61d0feb1a6a39 [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
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 /**
53 * Returns the event type from the specified event.
54 * If the node does not have an "event" property, "unknown" is returned.
55 *
56 * @param event message event
57 * @return extracted event type
58 */
59 public static String eventType(ObjectNode event) {
60 return string(event, "event", "unknown");
61 }
62
63 /**
Simon Huntd2747a02015-04-30 22:41:16 -070064 * Returns the sequence identifier from the specified event, or 0 (zero)
65 * if the "sid" property does not exist.
66 *
67 * @param event message event
68 * @return extracted sequence identifier
69 */
70 public static long sid(ObjectNode event) {
71 return number(event, "sid");
72 }
73
74 /**
Simon Hunt44aa2f82015-04-30 15:01:35 -070075 * Returns the payload from the specified event.
76 *
77 * @param event message event
78 * @return extracted payload object
79 */
80 public static ObjectNode payload(ObjectNode event) {
81 return (ObjectNode) event.path("payload");
82 }
83
84 /**
85 * Returns the specified node property as a number.
86 *
87 * @param node message event
88 * @param name property name
89 * @return property as number
90 */
91 public static long number(ObjectNode node, String name) {
92 return node.path(name).asLong();
93 }
94
95 /**
96 * Returns the specified node property as a string.
97 *
98 * @param node message event
99 * @param name property name
100 * @return property as a string
101 */
102 public static String string(ObjectNode node, String name) {
103 return node.path(name).asText();
104 }
105
106 /**
107 * Returns the specified node property as a string, with a default fallback.
108 *
Simon Huntd2747a02015-04-30 22:41:16 -0700109 * @param node object node
Simon Hunt44aa2f82015-04-30 15:01:35 -0700110 * @param name property name
111 * @param defaultValue fallback value if property is absent
112 * @return property as a string
113 */
114 public static String string(ObjectNode node, String name, String defaultValue) {
115 return node.path(name).asText(defaultValue);
116 }
117
Simon Huntd2747a02015-04-30 22:41:16 -0700118 /**
119 * Returns the specified node property as an object node.
120 *
121 * @param node object node
122 * @param name property name
123 * @return property as a node
124 */
125 public static ObjectNode node(ObjectNode node, String name) {
126 return (ObjectNode) node.path(name);
127 }
128
Simon Hunt44aa2f82015-04-30 15:01:35 -0700129}