blob: a789a7436de55e30ec124b7bcc10d14c335d912d [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
35 public RequestHandler(String eventType) {
36 this.eventType = eventType;
37 }
38
39 // package private
Simon Hunta0ddb022015-05-01 09:53:01 -070040 void setParent(UiMessageHandler parent) {
Simon Huntd2747a02015-04-30 22:41:16 -070041 this.parent = parent;
42 }
43
44 /**
45 * Returns the event type that this handler handles.
46 *
47 * @return event type
48 */
49 public String eventType() {
50 return eventType;
51 }
52
53 /**
54 * Processes the incoming message payload from the client.
55 *
Simon Huntb1ced8e2016-05-27 19:34:34 -070056 * @param sid message sequence identifier
Simon Huntd2747a02015-04-30 22:41:16 -070057 * @param payload request message payload
58 */
Simon Huntc54cd1b2015-05-11 13:43:44 -070059 // TODO: remove sid from signature
Simon Huntd2747a02015-04-30 22:41:16 -070060 public abstract void process(long sid, ObjectNode payload);
61
62
Simon Huntd2747a02015-04-30 22:41:16 -070063 // ===================================================================
64 // === Convenience methods...
65
66 /**
67 * Returns implementation of the specified service class.
68 *
69 * @param serviceClass service class
70 * @param <T> type of service
71 * @return implementation class
72 * @throws org.onlab.osgi.ServiceNotFoundException if no implementation found
73 */
74 protected <T> T get(Class<T> serviceClass) {
75 return parent.directory().get(serviceClass);
76 }
77
78 /**
79 * Sends a message back to the client.
80 *
81 * @param eventType message event type
82 * @param sid message sequence identifier
83 * @param payload message payload
84 */
Simon Huntda580882015-05-12 20:58:18 -070085 // TODO: remove sid from signature
Simon Huntd2747a02015-04-30 22:41:16 -070086 protected void sendMessage(String eventType, long sid, ObjectNode payload) {
87 parent.connection().sendMessage(eventType, sid, payload);
88 }
89
90 /**
91 * Sends a message back to the client.
92 * Here, the message is preformatted; the assumption is it has its
93 * eventType, sid and payload attributes already filled in.
94 *
95 * @param message the message to send
96 */
97 protected void sendMessage(ObjectNode message) {
98 parent.connection().sendMessage(message);
99 }
100
101 /**
102 * Allows one request handler to pass the event on to another for
103 * further processing.
104 * Note that the message handlers must be defined in the same parent.
105 *
106 * @param eventType event type
107 * @param sid sequence identifier
108 * @param payload message payload
109 */
Simon Huntda580882015-05-12 20:58:18 -0700110 // TODO: remove sid from signature
Simon Huntd2747a02015-04-30 22:41:16 -0700111 protected void chain(String eventType, long sid, ObjectNode payload) {
112 parent.exec(eventType, sid, payload);
113 }
114
115 // ===================================================================
116
117
Simon Huntda580882015-05-12 20:58:18 -0700118 /**
119 * Returns the specified node property as a string.
120 *
121 * @param node message event
Simon Huntb1ced8e2016-05-27 19:34:34 -0700122 * @param key property name
Simon Huntda580882015-05-12 20:58:18 -0700123 * @return property as a string
124 */
Simon Huntd2747a02015-04-30 22:41:16 -0700125 protected String string(ObjectNode node, String key) {
126 return JsonUtils.string(node, key);
127 }
128
Simon Huntda580882015-05-12 20:58:18 -0700129 /**
130 * Returns the specified node property as a string, with a default fallback.
131 *
Simon Huntb1ced8e2016-05-27 19:34:34 -0700132 * @param node object node
133 * @param key property name
134 * @param defValue fallback value if property is absent
Simon Huntda580882015-05-12 20:58:18 -0700135 * @return property as a string
136 */
Simon Huntd2747a02015-04-30 22:41:16 -0700137 protected String string(ObjectNode node, String key, String defValue) {
138 return JsonUtils.string(node, key, defValue);
139 }
140
Simon Huntb1ced8e2016-05-27 19:34:34 -0700141 /**
142 * Returns the specified node property as a boolean. More precisely, if
143 * the value for the given key is the string "true" then this returns true,
144 * false otherwise.
145 *
146 * @param node object node
147 * @param key property name
148 * @return property as a boolean
149 */
150 protected boolean bool(ObjectNode node, String key) {
151 return JsonUtils.bool(node, key);
152 }
153
Simon Huntd2747a02015-04-30 22:41:16 -0700154}