aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/api/src/main/java/org/onosproject/ui/JsonUtils.java
blob: 2ebb55459492283e898f4daa852aa21f420bac5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onosproject.ui;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * Provides convenience methods for dealing with JSON nodes, arrays etc.
 */
public final class JsonUtils {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    // non-instantiable
    private JsonUtils() { }

    /**
     * Wraps a message payload into an event structure for the given event
     * type and sequence ID. Generally, the sequence ID should be a copy of
     * the ID from the client request event.
     *
     * @param type    event type
     * @param sid     sequence ID
     * @param payload event payload
     * @return the object node representation
     */
    public static ObjectNode envelope(String type, long sid, ObjectNode payload) {
        ObjectNode event = MAPPER.createObjectNode();
        event.put("event", type);
        if (sid > 0) {
            event.put("sid", sid);
        }
        event.set("payload", payload);
        return event;
    }

    /**
     * Composes a message structure for the given message type and payload.
     *
     * @param type    message type
     * @param payload message payload
     * @return the object node representation
     */
    public static ObjectNode envelope(String type, ObjectNode payload) {
        ObjectNode event = MAPPER.createObjectNode();
        event.put("event", type);
        event.set("payload", payload);
        return event;
    }

    /**
     * Returns the event type from the specified event.
     * If the node does not have an "event" property, "unknown" is returned.
     *
     * @param event message event
     * @return extracted event type
     */
    public static String eventType(ObjectNode event) {
        return string(event, "event", "unknown");
    }

    /**
     * Returns the sequence identifier from the specified event, or 0 (zero)
     * if the "sid" property does not exist.
     *
     * @param event message event
     * @return extracted sequence identifier
     */
    public static long sid(ObjectNode event) {
        return number(event, "sid");
    }

    /**
     * Returns the payload from the specified event.
     *
     * @param event message event
     * @return extracted payload object
     */
    public static ObjectNode payload(ObjectNode event) {
        return (ObjectNode) event.path("payload");
    }

    /**
     * Returns the specified node property as a number.
     *
     * @param node message event
     * @param name property name
     * @return property as number
     */
    public static long number(ObjectNode node, String name) {
        return node.path(name).asLong();
    }

    /**
     * Returns the specified node property as a string.
     *
     * @param node message event
     * @param name property name
     * @return property as a string
     */
    public static String string(ObjectNode node, String name) {
        return node.path(name).asText();
    }

    /**
     * Returns the specified node property as a string, with a default fallback.
     *
     * @param node         object node
     * @param name         property name
     * @param defaultValue fallback value if property is absent
     * @return property as a string
     */
    public static String string(ObjectNode node, String name, String defaultValue) {
        return node.path(name).asText(defaultValue);
    }

    /**
     * Returns the specified node property as an object node.
     *
     * @param node object node
     * @param name property name
     * @return property as a node
     */
    public static ObjectNode node(ObjectNode node, String name) {
        return (ObjectNode) node.path(name);
    }

}