blob: cb7c9016a60ec85151ec7bd8c80eca8c6ee81903 [file] [log] [blame]
Simon Huntd2747a02015-04-30 22:41:16 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Huntd2747a02015-04-30 22:41:16 -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 */
16package org.onosproject.ui;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20
21/**
22 * Abstraction of an entity that handles a specific request from the
23 * user interface client.
24 *
Simon Hunta0ddb022015-05-01 09:53:01 -070025 * @see UiMessageHandler
Simon Huntd2747a02015-04-30 22:41:16 -070026 */
27public abstract class RequestHandler {
28
29 protected static final ObjectMapper MAPPER = new ObjectMapper();
30
31 private final String eventType;
Simon Hunta0ddb022015-05-01 09:53:01 -070032 private UiMessageHandler parent;
Simon Huntd2747a02015-04-30 22:41:16 -070033
34
Simon Hunt8add9ee2016-09-20 17:05:07 -070035 /**
36 * Constructs a request handler instance that will handle events
37 * of the specified type.
38 *
39 * @param eventType type of event that will be handled
40 */
Simon Huntd2747a02015-04-30 22:41:16 -070041 public RequestHandler(String eventType) {
42 this.eventType = eventType;
43 }
44
45 // package private
Simon Hunta0ddb022015-05-01 09:53:01 -070046 void setParent(UiMessageHandler parent) {
Simon Huntd2747a02015-04-30 22:41:16 -070047 this.parent = parent;
48 }
49
50 /**
51 * Returns the event type that this handler handles.
52 *
53 * @return event type
54 */
55 public String eventType() {
56 return eventType;
57 }
58
59 /**
60 * Processes the incoming message payload from the client.
61 *
Simon Huntd2747a02015-04-30 22:41:16 -070062 * @param payload request message payload
63 */
Simon Hunt8a0429a2017-01-06 16:52:47 -080064 public abstract void process(ObjectNode payload);
Simon Huntd2747a02015-04-30 22:41:16 -070065
66
Simon Huntd2747a02015-04-30 22:41:16 -070067 // ===================================================================
68 // === Convenience methods...
69
70 /**
Simon Hunt8add9ee2016-09-20 17:05:07 -070071 * Returns an implementation of the specified service class.
Simon Huntd2747a02015-04-30 22:41:16 -070072 *
73 * @param serviceClass service class
74 * @param <T> type of service
75 * @return implementation class
76 * @throws org.onlab.osgi.ServiceNotFoundException if no implementation found
77 */
78 protected <T> T get(Class<T> serviceClass) {
79 return parent.directory().get(serviceClass);
80 }
81
82 /**
Simon Hunt8add9ee2016-09-20 17:05:07 -070083 * Sends a message back to the client with the given event type and payload.
Simon Huntd2747a02015-04-30 22:41:16 -070084 *
85 * @param eventType message event type
Simon Huntd5b96732016-07-08 13:22:27 -070086 * @param payload message payload
87 */
88 protected void sendMessage(String eventType, ObjectNode payload) {
89 // TODO: remove sid
90 parent.connection().sendMessage(eventType, 0, payload);
91 }
92
93 /**
94 * Sends a message back to the client.
Simon Hunt8add9ee2016-09-20 17:05:07 -070095 * Here, the message is preformatted; the assumption is that it has its
Simon Hunt8a0429a2017-01-06 16:52:47 -080096 * "event" (event type) and "payload" attributes already filled in.
Simon Huntd2747a02015-04-30 22:41:16 -070097 *
98 * @param message the message to send
99 */
100 protected void sendMessage(ObjectNode message) {
101 parent.connection().sendMessage(message);
102 }
103
104 /**
105 * Allows one request handler to pass the event on to another for
106 * further processing.
107 * Note that the message handlers must be defined in the same parent.
108 *
109 * @param eventType event type
Simon Huntd2747a02015-04-30 22:41:16 -0700110 * @param payload message payload
111 */
Simon Hunt8a0429a2017-01-06 16:52:47 -0800112 protected void chain(String eventType, ObjectNode payload) {
113 parent.exec(eventType, payload);
Simon Huntd2747a02015-04-30 22:41:16 -0700114 }
115
116 // ===================================================================
117
118
Simon Huntda580882015-05-12 20:58:18 -0700119 /**
120 * Returns the specified node property as a string.
121 *
122 * @param node message event
Simon Huntb1ced8e2016-05-27 19:34:34 -0700123 * @param key property name
Simon Huntda580882015-05-12 20:58:18 -0700124 * @return property as a string
125 */
Simon Huntd2747a02015-04-30 22:41:16 -0700126 protected String string(ObjectNode node, String key) {
127 return JsonUtils.string(node, key);
128 }
129
Simon Huntda580882015-05-12 20:58:18 -0700130 /**
131 * Returns the specified node property as a string, with a default fallback.
132 *
Simon Huntb1ced8e2016-05-27 19:34:34 -0700133 * @param node object node
134 * @param key property name
135 * @param defValue fallback value if property is absent
Simon Huntda580882015-05-12 20:58:18 -0700136 * @return property as a string
137 */
Simon Huntd2747a02015-04-30 22:41:16 -0700138 protected String string(ObjectNode node, String key, String defValue) {
139 return JsonUtils.string(node, key, defValue);
140 }
141
Simon Huntb1ced8e2016-05-27 19:34:34 -0700142 /**
143 * Returns the specified node property as a boolean. More precisely, if
144 * the value for the given key is the string "true" then this returns true,
145 * false otherwise.
146 *
147 * @param node object node
148 * @param key property name
149 * @return property as a boolean
150 */
151 protected boolean bool(ObjectNode node, String key) {
152 return JsonUtils.bool(node, key);
153 }
Simon Huntd2747a02015-04-30 22:41:16 -0700154}