blob: 00b359560cb8625aef0c294c7fc503c29d3c5adf [file] [log] [blame]
Thomas Vachuska3553b302015-03-07 14:49:43 -08001/*
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 */
16package org.onosproject.ui;
17
Simon Hunt4c7edd32015-03-11 10:42:53 -070018import com.fasterxml.jackson.databind.ObjectMapper;
Thomas Vachuska3553b302015-03-07 14:49:43 -080019import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.osgi.ServiceDirectory;
21
22import java.util.Set;
23
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Abstraction of an entity capable of processing a JSON message from the user
29 * interface client.
30 * <p>
31 * The message is a JSON object with the following structure:
Thomas Vachuskae586b792015-03-26 13:59:38 -070032 * </p>
Thomas Vachuska3553b302015-03-07 14:49:43 -080033 * <pre>
34 * {
35 * "type": "<em>event-type</em>",
36 * "sid": "<em>sequence-number</em>",
37 * "payload": {
38 * <em>arbitrary JSON object structure</em>
39 * }
40 * }
41 * </pre>
42 */
Simon Huntd2747a02015-04-30 22:41:16 -070043@Deprecated
Thomas Vachuska3553b302015-03-07 14:49:43 -080044public abstract class UiMessageHandler {
45
46 private final Set<String> messageTypes;
47 private UiConnection connection;
48 private ServiceDirectory directory;
49
Thomas Vachuskae586b792015-03-26 13:59:38 -070050 /**
51 * Mapper for creating ObjectNodes and ArrayNodes etc.
52 */
Simon Hunt4c7edd32015-03-11 10:42:53 -070053 protected final ObjectMapper mapper = new ObjectMapper();
54
Thomas Vachuska3553b302015-03-07 14:49:43 -080055 /**
56 * Creates a new message handler for the specified set of message types.
57 *
58 * @param messageTypes set of message types
59 */
60 protected UiMessageHandler(Set<String> messageTypes) {
61 this.messageTypes = checkNotNull(messageTypes, "Message types cannot be null");
62 checkArgument(!messageTypes.isEmpty(), "Message types cannot be empty");
63 }
64
65 /**
66 * Returns the set of message types which this handler is capable of
67 * processing.
68 *
69 * @return set of message types
70 */
71 public Set<String> messageTypes() {
72 return messageTypes;
73 }
74
75 /**
76 * Processes a JSON message from the user interface client.
77 *
78 * @param message JSON message
79 */
80 public abstract void process(ObjectNode message);
81
82 /**
83 * Initializes the handler with the user interface connection and
84 * service directory context.
85 *
86 * @param connection user interface connection
87 * @param directory service directory
88 */
89 public void init(UiConnection connection, ServiceDirectory directory) {
90 this.connection = connection;
91 this.directory = directory;
92 }
93
94 /**
95 * Destroys the message handler context.
96 */
97 public void destroy() {
98 this.connection = null;
99 this.directory = null;
100 }
101
102 /**
103 * Returns the user interface connection with which this handler was primed.
104 *
105 * @return user interface connection
106 */
107 public UiConnection connection() {
108 return connection;
109 }
110
111 /**
112 * Returns the user interface connection with which this handler was primed.
113 *
114 * @return user interface connection
115 */
116 public ServiceDirectory directory() {
117 return directory;
118 }
119
120 /**
121 * Returns implementation of the specified service class.
122 *
123 * @param serviceClass service class
124 * @param <T> type of service
125 * @return implementation class
126 * @throws org.onlab.osgi.ServiceNotFoundException if no implementation found
127 */
128 protected <T> T get(Class<T> serviceClass) {
129 return directory.get(serviceClass);
130 }
131
Simon Hunt4c7edd32015-03-11 10:42:53 -0700132 /**
133 * Wraps a message payload into an event structure for the given event
134 * type and sequence ID. Generally the
135 *
Thomas Vachuskae586b792015-03-26 13:59:38 -0700136 * @param type event type
137 * @param sid sequence ID
Simon Hunt4c7edd32015-03-11 10:42:53 -0700138 * @param payload event payload
139 * @return the object node representation
140 */
141 protected ObjectNode envelope(String type, long sid, ObjectNode payload) {
Simon Hunt44aa2f82015-04-30 15:01:35 -0700142 return JsonUtils.envelope(type, sid, payload);
143 }
144
145 /**
146 * Returns the event type from the specified event.
147 *
148 * @param event the event
149 * @return the event type
150 */
151 protected String eventType(ObjectNode event) {
152 return JsonUtils.eventType(event);
Simon Hunt4c7edd32015-03-11 10:42:53 -0700153 }
154
Thomas Vachuskae586b792015-03-26 13:59:38 -0700155 /**
156 * Retrieves the payload from the specified event.
157 *
158 * @param event message event
159 * @return extracted payload object
160 */
161 protected ObjectNode payload(ObjectNode event) {
Simon Hunt44aa2f82015-04-30 15:01:35 -0700162 return JsonUtils.payload(event);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700163 }
164
165 /**
166 * Returns the specified node property as a number.
167 *
168 * @param node message event
169 * @param name property name
170 * @return property as number
171 */
172 protected long number(ObjectNode node, String name) {
Simon Hunt44aa2f82015-04-30 15:01:35 -0700173 return JsonUtils.number(node, name);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700174 }
175
176 /**
177 * Returns the specified node property as a string.
178 *
179 * @param node message event
180 * @param name property name
181 * @return property as a string
182 */
183 protected String string(ObjectNode node, String name) {
Simon Hunt44aa2f82015-04-30 15:01:35 -0700184 return JsonUtils.string(node, name);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700185 }
186
187 /**
188 * Returns the specified node property as a string with a default fallback.
189 *
190 * @param node message event
191 * @param name property name
192 * @param defaultValue fallback value if property is absent
193 * @return property as a string
194 */
195 protected String string(ObjectNode node, String name, String defaultValue) {
Simon Hunt44aa2f82015-04-30 15:01:35 -0700196 return JsonUtils.string(node, name, defaultValue);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700197 }
198
Simon Hunt44aa2f82015-04-30 15:01:35 -0700199 /**
200 * Concatenates an arbitrary number of objects, using their
201 * toString() methods.
202 *
203 * @param items the items to concatenate
204 * @return a concatenated string
205 */
206 protected static String concat(Object... items) {
207 StringBuilder sb = new StringBuilder();
208 for (Object o : items) {
209 sb.append(o);
210 }
211 return sb.toString();
212 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800213}